«Back to Home

Core Java

Topics

if-else Statement In Java

if-else Statements

There are many types of if statement in Java, which are,
  • if statement.
  • if-else statement.
  • if-else-if statement.
  • Nested if statement.
if statement

If statement is very basic of all control flow statements. It is used to test the condition, if the condition is right .Hence, it returns the output TRUE. It returns only boolean value true or false.
 
Syntax

if(condition){
// statement.
}
 
Let’s understand with the flowchart, given below.

55
 
Let’s see an example.
 
Code 
  1. public class IfStatement1 {  
  2.     public static void main(String[] args) {  
  3.         int no = 25;  
  4.         if (no > 18) {  
  5.             System.out.println("Number is greater than 18");  
  6.         }  
  7.     }  
  8. }  
32 
 
Output

33

if-else statement

If – else statement is also used to test the condition but it provides a secondary path of the execution. It returns if statement when the condition is right and when the condition is wrong. Hence, it returns else statement.
 
Syntax

If (condition) {
// statement code.
} else {
//statement
}
 
Let’s understand with the flowchart, given below.

56
 
Let’s see an example.
 
Code
  1. public class IfelseStatement2 {  
  2.     public static void main(String[] args) {  
  3.         int num = 15;  
  4.         if (num % 2 == 0) {  
  5.             System.out.println("this number is even");  
  6.         } else {  
  7.             System.out.println("this number is odd");  
  8.         }  
  9.     }  
  10. }  
35
 
Output

36

if-else-if statement

if-else-if statement checks the one condition from the multiple statements. It is a different statement from others.
 
Syntax 

if(condition1){
// code.
} else if (condition2) {
//code.
}
Else if (condition3){
//code.
}
else {
//code.
}
 
Let’s understand with the flowchart, given below.

57
 
Let’s see an example, given below.
 
Code 
  1. public class IfelseifStatement3 {  
  2.     public static void main(String[] args) {  
  3.         int marks = 80;  
  4.         if (marks < 50) {  
  5.             System.out.println("C grade");  
  6.         } else if (marks >= 50 && marks < 60) {  
  7.             System.out.println("C+ grade");  
  8.         } else if (marks >= 60 && marks < 70) {  
  9.             System.out.println("B grade");  
  10.         } else if (marks >= 70 && marks < 80) {  
  11.             System.out.println("B+ grade");  
  12.         } else if (marks >= 80 && marks < 90) {  
  13.             System.out.println("A grade");  
  14.         } else if (marks >= 90 && marks < 100) {  
  15.             System.out.println("A+ grade");  
  16.         } else {  
  17.             System.out.println("Unfounded");  
  18.         }  
  19.     }  
  20. }  
38
 
Output

39

Nested if Statement

In nested if statement, we can use one if statement or else if statement inside another if or else if statement. In other words, nested means one statement inside the other statement.
 
Syntax

if (condition1){
// code.
If (condition2) {
//code.
}
}
 
Let’s understand with the flowchart, given below.

40
 
Let’s see an example, given below.
 
Code
  1. public class NestedifStatement4 {  
  2.     public static void main(String[] args) {  
  3.         int x = 20;  
  4.         int y = 10;  
  5.         if (x == 20) {  
  6.             if (y == 10) {  
  7.                 System.out.println("X=20 and y=10");  
  8.             }  
  9.         }  
  10.     }  
  11. }  
41
 
Output

42
 
Summary

Thus, we learnt that if statement is very basic of all the control flow statements, it is used to test the condition and also learnt its four types in Java.