logo

Java If-else 문

그만큼 자바 if 문 상태를 테스트하는 데 사용됩니다. 그것은 확인한다 부울 상태: 진실 또는 거짓 . Java에는 다양한 유형의 if 문이 있습니다.

  • if 문
  • if-else 문
  • if-else-if 사다리
  • 중첩된 if 문

자바 if 문

Java if 문은 조건을 테스트합니다. 이는 다음을 실행합니다. 차단하면 조건이 참이면.

통사론:

 if(condition){ //code to be executed } 
자바의 if 문

예:

 //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'); } } } 
지금 테스트해보세요

산출:

Age is greater than 18 

Java if-else 문

Java if-else 문도 조건을 테스트합니다. 그것은 차단하면 조건이 참이면 그렇지 않으면 다른 블록 실행됩니다.

통사론:

 if(condition){ //code if condition is true }else{ //code if condition is false } 
자바의 if-else 문

예:

 //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'); } } } 
지금 테스트해보세요

산출:

odd number 

윤년 예:

4와 400으로 나누어 떨어지면 1년은 윤년입니다. 그러나 100으로 나누어 떨어지지는 않습니다.

npm 설치 명령
 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'); } } } 

산출:

LEAP YEAR 

삼항 연산자 사용

if...else 문의 작업을 수행하기 위해 삼항 연산자(? :)를 사용할 수도 있습니다. 상태를 확인하는 간단한 방법입니다. 조건이 참이면 ? 반환됩니다. 그러나 조건이 거짓이면 :의 결과가 반환됩니다.

예:

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

산출:

odd number 

Java if-else-if 래더 문

if-else-if 래더 문은 여러 문에서 하나의 조건을 실행합니다.

통사론:

 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 자바의 래더 문

예:

 //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){>

POSITIVE, NEGATIVE 또는 ZERO를 확인하는 프로그램:

 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 중첩 if 문

중첩된 if 문은 다음을 나타냅니다. if 블록 내의 다른 if 블록 . 여기서 내부 if 블록 조건은 외부 if 블록 조건이 true인 경우에만 실행됩니다.

통사론:

하위 문자열 함수 java
 if(condition){ //code to be executed if(condition){ //code to be executed } } 

예:

 //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;); } } }} 
지금 테스트해보세요

산출:

You are eligible to donate blood 

예 2:

 //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;); } } } 
지금 테스트해보세요

산출:

 You are not eligible to donate blood