Decision-Making if-else statement in Java

 If Statement

In Java, the "if" statement is used to evaluate a condition. The control of the program is diverted depending upon the specific condition. The condition of the If statement gives a Boolean value, either true or false. In Java, there are four types of if-statements given below.

  1. Simple if statement
  2. if-else statement
  3. if-else-if ladder
  4. Nested if-statement

Let's understand the if-statements one by one.

1). Simple if statement:

It is the most basic statement among all control flow statements in Java. It evaluates a Boolean expression and enables the program to enter a block of code if the expression evaluates to true.

Syntax of if statement is given below.

  1. if(condition) {    
  2. statement 1//executes when condition is true   
  3. }    

Consider the following example in which we have used the if statement in the java code.

Student.java

Student.java

  1. public class Student {    
  2. public static void main(String[] args) {    
  3. int x = 10;    
  4. int y = 12;    
  5. if(x+y > 20) {    
  6. System.out.println("x + y is greater than 20");    
  7. }    
  8. }      
  9. }     

Output:

x + y is greater than 20

2). if-else statement

The if-else statement is an extension to the if-statement, which uses another block of code, i.e., else block. The else block is executed if the condition of the if-block is evaluated as false.

Syntax:

  1. if(condition) {    
  2. statement 1//executes when condition is true   
  3. }  
  4. else{  
  5. statement 2//executes when condition is false   
  6. }  

Consider the following example.

Student.java

  1. public class Student {  
  2. public static void main(String[] args) {  
  3. int x = 10;  
  4. int y = 12;  
  5. if(x+y < 10) {  
  6. System.out.println("x + y is less than      10");  
  7. }   else {  
  8. System.out.println("x + y is greater than 20");  
  9. }  
  10. }  
  11. }  

Output:

x + y is greater than 20

3). if-else-if ladder:

The if-else-if statement contains the if-statement followed by multiple else-if statements. In other words, we can say that it is the chain of if-else statements that create a decision tree where the program may enter in the block of code where the condition is true. We can also define an else statement at the end of the chain.

Syntax of if-else-if statement is given below.

  1. if(condition 1) {    
  2. statement 1//executes when condition 1 is true   
  3. }  
  4. else if(condition 2) {  
  5. statement 2//executes when condition 2 is true   
  6. }  
  7. else {  
  8. statement 2//executes when all the conditions are false   
  9. }  

Consider the following example.

Student.java

  1. public class Student {  
  2. public static void main(String[] args) {  
  3. String city = "Delhi";  
  4. if(city == "Meerut") {  
  5. System.out.println("city is meerut");  
  6. }else if (city == "Noida") {  
  7. System.out.println("city is noida");  
  8. }else if(city == "Agra") {  
  9. System.out.println("city is agra");  
  10. }else {  
  11. System.out.println(city);  
  12. }  
  13. }  
  14. }  

Output:

Delhi

4). Nested if-statement

In nested if-statements, the if statement can contain a if or if-else statement inside another if or else-if statement.

Syntax of Nested if-statement is given below.

  1. if(condition 1) {    
  2. statement 1//executes when condition 1 is true   
  3. if(condition 2) {  
  4. statement 2//executes when condition 2 is true   
  5. }  
  6. else{  
  7. statement 2//executes when condition 2 is false   
  8. }  
  9. }  

Consider the following example.

Student.java

  1. public class Student {    
  2. public static void main(String[] args) {    
  3. String address = "Delhi, India";    
  4.     
  5. if(address.endsWith("India")) {    
  6. if(address.contains("Meerut")) {    
  7. System.out.println("Your city is Meerut");    
  8. }else if(address.contains("Noida")) {    
  9. System.out.println("Your city is Noida");    
  10. }else {    
  11. System.out.println(address.split(",")[0]);    
  12. }    
  13. }else {    
  14. System.out.println("You are not living in India");    
  15. }    
  16. }    
  17. }    

Output:

Delhi

Java If-else Statement

The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

Java if Statement

The Java if statement tests the condition. It executes the if block if condition is true.

Syntax:

  1. if(condition){  
  2. //code to be executed  
  3. }  
if statement in java

Example:

  1. //Java Program to demonstate the use of if statement.  
  2. public class IfExample {  
  3. public static void main(String[] args) {  
  4.     //defining an 'age' variable  
  5.     int age=20;  
  6.     //checking the age  
  7.     if(age>18){  
  8.         System.out.print("Age is greater than 18");  
  9.     }  
  10. }  
  11. }  

Test it Now

Output:

Age is greater than 18

Java if-else Statement

The Java if-else statement also tests the condition. It executes the if block if condition is true otherwise else block is executed.

Syntax:

  1. if(condition){  
  2. //code if condition is true  
  3. }else{  
  4. //code if condition is false  
  5. }  

if-else statement in java

Example:

  1. //A Java Program to demonstrate the use of if-else statement.  
  2. //It is a program of odd and even number.  
  3. public class IfElseExample {  
  4. public static void main(String[] args) {  
  5.     //defining a variable  
  6.     int number=13;  
  7.     //Check if the number is divisible by 2 or not  
  8.     if(number%2==0){  
  9.         System.out.println("even number");  
  10.     }else{  
  11.         System.out.println("odd number");  
  12.     }  
  13. }  
  14. }  

Test it Now

Output:

odd number

Leap Year Example:

A year is leap, if it is divisible by 4 and 400. But, not by 100.

  1. public class LeapYearExample {    
  2. public static void main(String[] args) {    
  3.     int year=2020;    
  4.     if(((year % 4 ==0) && (year % 100 !=0)) || (year % 400==0)){  
  5.         System.out.println("LEAP YEAR");  
  6.     }  
  7.     else{  
  8.         System.out.println("COMMON YEAR");  
  9.     }  
  10. }    
  11. }    

Output:

LEAP YEAR

Using Ternary Operator

We can also use ternary operator (? :) to perform the task of if...else statement. It is a shorthand way to check the condition. If the condition is true, the result of ? is returned. But, if the condition is false, the result of : is returned.

Example:

  1. public class IfElseTernaryExample {    
  2. public static void main(String[] args) {    
  3.     int number=13;    
  4.     //Using ternary operator  
  5.     String output=(number%2==0)?"even number":"odd number";    
  6.     System.out.println(output);  
  7. }    
  8. }    

Output:

odd number

Java if-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

  1. if(condition1){  
  2. //code to be executed if condition1 is true  
  3. }else if(condition2){  
  4. //code to be executed if condition2 is true  
  5. }  
  6. else if(condition3){  
  7. //code to be executed if condition3 is true  
  8. }  
  9. ...  
  10. else{  
  11. //code to be executed if all the conditions are false  
  12. }  

if-else-if ladder statement in java

Example:

  1. //Java Program to demonstrate the use of If else-if ladder.  
  2. //It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.  
  3. public class IfElseIfExample {  
  4. public static void main(String[] args) {  
  5.     int marks=65;  
  6.       
  7.     if(marks<50){  
  8.         System.out.println("fail");  
  9.     }  
  10.     else if(marks>=50 && marks<60){  
  11.         System.out.println("D grade");  
  12.     }  
  13.     else if(marks>=60 && marks<70){  
  14.         System.out.println("C grade");  
  15.     }  
  16.     else if(marks>=70 && marks<80){  
  17.         System.out.println("B grade");  
  18.     }  
  19.     else if(marks>=80 && marks<90){  
  20.         System.out.println("A grade");  
  21.     }else if(marks>=90 && marks<100){  
  22.         System.out.println("A+ grade");  
  23.     }else{  
  24.         System.out.println("Invalid!");  
  25.     }  
  26. }  
  27. }  

Output:

C grade

Program to check POSITIVE, NEGATIVE or ZERO:

  1. public class PositiveNegativeExample {    
  2. public static void main(String[] args) {    
  3.     int number=-13;    
  4.     if(number>0){  
  5.     System.out.println("POSITIVE");  
  6.     }else if(number<0){  
  7.     System.out.println("NEGATIVE");  
  8.     }else{  
  9.     System.out.println("ZERO");  
  10.    }  
  11. }    
  12. }    

Output:

NEGATIVE

Java Nested if statement

The nested if statement represents the if block within another if block. Here, the inner if block condition executes only when outer if block condition is true.

Syntax:

  1. if(condition){    
  2.      //code to be executed    
  3.           if(condition){  
  4.              //code to be executed    
  5.     }    
  6. }  

Java Nested If Statement

Example:

  1. //Java Program to demonstrate the use of Nested If Statement.  
  2. public class JavaNestedIfExample {    
  3. public static void main(String[] args) {    
  4.     //Creating two variables for age and weight  
  5.     int age=20;  
  6.     int weight=80;    
  7.     //applying condition on age and weight  
  8.     if(age>=18){    
  9.         if(weight>50){  
  10.             System.out.println("You are eligible to donate blood");  
  11.         }    
  12.     }    
  13. }}  

Test it Now

Output:

You are eligible to donate blood

Example 2:

  1. //Java Program to demonstrate the use of Nested If Statement.    
  2. public class JavaNestedIfExample2 {      
  3. public static void main(String[] args) {      
  4.     //Creating two variables for age and weight    
  5.     int age=25;    
  6.     int weight=48;      
  7.     //applying condition on age and weight    
  8.     if(age>=18){      
  9.         if(weight>50){    
  10.             System.out.println("You are eligible to donate blood");    
  11.         } else{  
  12.             System.out.println("You are not eligible to donate blood");    
  13.         }  
  14.     } else{  
  15.       System.out.println("Age must be greater than 18");  
  16.     }  
  17. }  }  

Test it Now

Output:

You are not eligible to donate blood


Comments

Popular Topics

What is JSP. How to create one JSP application in Eclipse

How to develop a RESTful webservice in java ?

Data Types in PL/SQL

What is JDBC ?

What is Hibernate in java ?

If you have any query or suggestions, Please feel free to write us

Name

Email *

Message *