The Java ja paziņojums izmanto, lai pārbaudītu stāvokli. Tas pārbauda Būla nosacījums: taisnība vai viltus . Java valodā ir dažādi if paziņojuma veidi.
- ja paziņojums
- ja-cits paziņojums
- ja-citādi-ja kāpnes
- ligzdotas if paziņojums
Java if paziņojums
Java if priekšraksts pārbauda nosacījumu. Tas izpilda ja bloks ja nosacījums ir patiess.
Sintakse:
if(condition){ //code to be executed }
Piemērs:
konvertēšana no virknes uz int java
//Java Program to demonstate the use of if statement. public class IfExample { public static void main(String[] args) { //defining an 'age' variable int age=20; //checking the age if(age>18){ System.out.print('Age is greater than 18'); } } }Izmēģiniet to tūlīt
Izvade:
Age is greater than 18
Java if-else paziņojums
Java if-else priekšraksts arī pārbauda nosacījumu. Tas izpilda ja bloks ja nosacījums ir patiess citādi cits bloks tiek izpildīts.
Sintakse:
if(condition){ //code if condition is true }else{ //code if condition is false }
Piemērs:
//A Java Program to demonstrate the use of if-else statement. //It is a program of odd and even number. public class IfElseExample { public static void main(String[] args) { //defining a variable int number=13; //Check if the number is divisible by 2 or not if(number%2==0){ System.out.println('even number'); }else{ System.out.println('odd number'); } } }Izmēģiniet to tūlīt
Izvade:
odd number
Garā gada piemērs:
Gads ir lēciens, ja dalās ar 4 un 400. Bet ne ar 100.
public class LeapYearExample { public static void main(String[] args) { int year=2020; if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){ System.out.println('LEAP YEAR'); } else{ System.out.println('COMMON YEAR'); } } }
Izvade:
LEAP YEAR
Trīskāršā operatora izmantošana
Varam izmantot arī trīskāršu operatoru (? :), lai izpildītu if...else priekšraksta uzdevumu. Tas ir īss veids, kā pārbaudīt stāvokli. Ja nosacījums ir patiess, rezultāts ? tiek atgriezta. Bet, ja nosacījums ir nepatiess, tiek atgriezts rezultāts :.
Piemērs:
public class IfElseTernaryExample { public static void main(String[] args) { int number=13; //Using ternary operator String output=(number%2==0)?'even number':'odd number'; System.out.println(output); } }
Izvade:
odd number
Java if-else-if kāpņu paziņojums
Ja-else-if kāpņu paziņojums izpilda vienu nosacījumu no vairākiem priekšrakstiem.
saraksts uz java
Sintakse:
if(condition1){ //code to be executed if condition1 is true }else if(condition2){ //code to be executed if condition2 is true } else if(condition3){ //code to be executed if condition3 is true } ... else{ //code to be executed if all the conditions are false }
Piemērs:
//Java Program to demonstrate the use of If else-if ladder. //It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+. public class IfElseIfExample { public static void main(String[] args) { int marks=65; if(marks=50 && marks=60 && marks=70 && marks=80 && marks=90 && marks<100){ system.out.println('a+ grade'); }else{ system.out.println('invalid!'); } < pre> <p>Output:</p> <pre>C grade </pre> <p> <strong>Program to check POSITIVE, NEGATIVE or ZERO:</strong> </p> <pre> public class PositiveNegativeExample { public static void main(String[] args) { int number=-13; if(number>0){ System.out.println('POSITIVE'); }else if(number<0){ system.out.println('negative'); }else{ system.out.println('zero'); } < pre> <p>Output:</p> <pre>NEGATIVE </pre> <h2>Java Nested if statement</h2> <p>The nested if statement represents the <em>if block within another if block</em> . Here, the inner if block condition executes only when outer if block condition is true. </p> <p> <strong>Syntax:</strong> </p> <pre> if(condition){ //code to be executed if(condition){ //code to be executed } } </pre> <p> <strong>Example:</strong> </p> <pre> //Java Program to demonstrate the use of Nested If Statement. public class JavaNestedIfExample { public static void main(String[] args) { //Creating two variables for age and weight int age=20; int weight=80; //applying condition on age and weight if(age>=18){ if(weight>50){ System.out.println('You are eligible to donate blood'); } } }} </pre> <span> Test it Now </span> <p>Output:</p> <pre>You are eligible to donate blood </pre> <p> <strong>Example 2:</strong> </p> <pre> //Java Program to demonstrate the use of Nested If Statement. public class JavaNestedIfExample2 { public static void main(String[] args) { //Creating two variables for age and weight int age=25; int weight=48; //applying condition on age and weight if(age>=18){ if(weight>50){ System.out.println('You are eligible to donate blood'); } else{ System.out.println('You are not eligible to donate blood'); } } else{ System.out.println('Age must be greater than 18'); } } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> You are not eligible to donate blood </pre> <hr></0){></pre></100){>
Programma, lai pārbaudītu POZITĪVU, NEGATĪVU vai NULLI:
public class PositiveNegativeExample { public static void main(String[] args) { int number=-13; if(number>0){ System.out.println('POSITIVE'); }else if(number<0){ system.out.println(\'negative\'); }else{ system.out.println(\'zero\'); } < pre> <p>Output:</p> <pre>NEGATIVE </pre> <h2>Java Nested if statement</h2> <p>The nested if statement represents the <em>if block within another if block</em> . Here, the inner if block condition executes only when outer if block condition is true. </p> <p> <strong>Syntax:</strong> </p> <pre> if(condition){ //code to be executed if(condition){ //code to be executed } } </pre> <p> <strong>Example:</strong> </p> <pre> //Java Program to demonstrate the use of Nested If Statement. public class JavaNestedIfExample { public static void main(String[] args) { //Creating two variables for age and weight int age=20; int weight=80; //applying condition on age and weight if(age>=18){ if(weight>50){ System.out.println('You are eligible to donate blood'); } } }} </pre> <span> Test it Now </span> <p>Output:</p> <pre>You are eligible to donate blood </pre> <p> <strong>Example 2:</strong> </p> <pre> //Java Program to demonstrate the use of Nested If Statement. public class JavaNestedIfExample2 { public static void main(String[] args) { //Creating two variables for age and weight int age=25; int weight=48; //applying condition on age and weight if(age>=18){ if(weight>50){ System.out.println('You are eligible to donate blood'); } else{ System.out.println('You are not eligible to donate blood'); } } else{ System.out.println('Age must be greater than 18'); } } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> You are not eligible to donate blood </pre> <hr></0){>
Java Nested if paziņojums
Ligzdotais if paziņojums apzīmē if bloks citā if blokā . Šeit iekšējais ja bloka nosacījums tiek izpildīts tikai tad, ja ārējais, ja bloka nosacījums ir patiess.
Sintakse:
if(condition){ //code to be executed if(condition){ //code to be executed } }
Piemērs:
pārvērst virkni par datumu
//Java Program to demonstrate the use of Nested If Statement. public class JavaNestedIfExample { public static void main(String[] args) { //Creating two variables for age and weight int age=20; int weight=80; //applying condition on age and weight if(age>=18){ if(weight>50){ System.out.println('You are eligible to donate blood'); } } }}Izmēģiniet to tūlīt
Izvade:
You are eligible to donate blood
2. piemērs:
//Java Program to demonstrate the use of Nested If Statement. public class JavaNestedIfExample2 { public static void main(String[] args) { //Creating two variables for age and weight int age=25; int weight=48; //applying condition on age and weight if(age>=18){ if(weight>50){ System.out.println('You are eligible to donate blood'); } else{ System.out.println('You are not eligible to donate blood'); } } else{ System.out.println('Age must be greater than 18'); } } }Izmēģiniet to tūlīt
Izvade:
You are not eligible to donate blood
0){>100){>