logo

Java If-else pareiškimas

The Java jei teiginys naudojamas būklei patikrinti. Tai tikrina loginis sąlyga: tiesa arba klaidinga . „Java“ yra įvairių „if“ teiginių tipų.

  • jei teiginys
  • jei-kitaip teiginys
  • jei-kita-jei kopėčios
  • įdėtas if teiginys

Java if pareiškimas

„Java if“ sakinys patikrina sąlygą. Jis vykdo jei blokas jei sąlyga teisinga.

Sintaksė:

 if(condition){ //code to be executed } 
if teiginys java

Pavyzdys:

 //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'); } } } 
Išbandykite dabar

Išvestis:

įterpimo rūšiavimo algoritmas
Age is greater than 18 

Java if-else pareiškimas

Java if-else sakinys taip pat išbando sąlygą. Jis vykdo jei blokas jei sąlyga teisinga kitaip kitas blokas yra įvykdytas.

Sintaksė:

 if(condition){ //code if condition is true }else{ //code if condition is false } 
if-else teiginys java

Pavyzdys:

 //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'); } } } 
Išbandykite dabar

Išvestis:

odd number 

Keliamųjų metų pavyzdys:

Metai yra šuolis, jei dalijasi iš 4 ir 400. Bet ne iš 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'); } } } 

Išvestis:

LEAP YEAR 

Naudojant trinarinį operatorių

Taip pat galime naudoti trinarinį operatorių (? :) norėdami atlikti if...else teiginio užduotį. Tai trumpas būdas patikrinti būklę. Jei sąlyga teisinga, rezultatas ? yra grąžinamas. Bet jei sąlyga klaidinga, grąžinamas rezultatas :.

Pavyzdys:

 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); } } 

Išvestis:

java sąrašo laukelis
odd number 

Java if-else-if kopėčių pareiškimas

If-else-if kopėčių sakinys vykdo vieną sąlygą iš kelių teiginių.

Sintaksė:

 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 } 
if-else-if kopėčių sakinys java

Pavyzdys:

 //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 &amp;&amp; marks=60 &amp;&amp; marks=70 &amp;&amp; marks=80 &amp;&amp; marks=90 &amp;&amp; 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&gt;0){ System.out.println(&apos;POSITIVE&apos;); }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&gt;=18){ if(weight&gt;50){ System.out.println(&apos;You are eligible to donate blood&apos;); } } }} </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&gt;=18){ if(weight&gt;50){ System.out.println(&apos;You are eligible to donate blood&apos;); } else{ System.out.println(&apos;You are not eligible to donate blood&apos;); } } else{ System.out.println(&apos;Age must be greater than 18&apos;); } } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> You are not eligible to donate blood </pre> <hr></0){></pre></100){>

Programa, skirta patikrinti TEIGIAMU, NEIGIAMU arba NULIS:

 public class PositiveNegativeExample { public static void main(String[] args) { int number=-13; if(number&gt;0){ System.out.println(&apos;POSITIVE&apos;); }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&gt;=18){ if(weight&gt;50){ System.out.println(&apos;You are eligible to donate blood&apos;); } } }} </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&gt;=18){ if(weight&gt;50){ System.out.println(&apos;You are eligible to donate blood&apos;); } else{ System.out.println(&apos;You are not eligible to donate blood&apos;); } } else{ System.out.println(&apos;Age must be greater than 18&apos;); } } } </pre> <span> Test it Now </span> <p>Output:</p> <pre> You are not eligible to donate blood </pre> <hr></0){>

Java Neted if teiginys

Įdėtas if teiginys reiškia if blokas kitame if bloke . Čia vidinė jei bloko sąlyga vykdoma tik tada, kai išorinė, jei bloko sąlyga yra teisinga.

Sintaksė:

 if(condition){ //code to be executed if(condition){ //code to be executed } } 

Pavyzdys:

 //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&gt;=18){ if(weight&gt;50){ System.out.println(&apos;You are eligible to donate blood&apos;); } } }} 
Išbandykite dabar

Išvestis:

You are eligible to donate blood 

2 pavyzdys:

 //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&gt;=18){ if(weight&gt;50){ System.out.println(&apos;You are eligible to donate blood&apos;); } else{ System.out.println(&apos;You are not eligible to donate blood&apos;); } } else{ System.out.println(&apos;Age must be greater than 18&apos;); } } } 
Išbandykite dabar

Išvestis:

 You are not eligible to donate blood