When we need to execute a set of statements based on a condition then we need to use control flow statements. For example, if a number is greater than zero then we want to print “Positive Number” but if it is less than zero then we want to print “Negative Number”. In this case we have two print statements in the program, but only one print statement executes at a time based on the input value. We will see how to write such type of conditions in the java program using control statements.

n this tutorial, we will see four types of control statements that you can use in java programs based on the requirement: In this tutorial we will cover following conditional statements:

a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement

If statement

If statement consists a condition, followed by statement or a set of statements as shown below:

if(condition){
  Statement(s);
}

The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body are completely ignored.

Example of if statement
public class IfStatementExample {

   public static void main(String args[]){
      int num=70;
      if( num < 100 ){
	  /* This println statement will only execute,
	   * if the above condition is true
	   */
	  System.out.println("number is less than 100");
      }
   }
}

Output:

number is less than 100
Nested if statement in Java

When there is an if statement inside another if statement then it is called the nested if statement.
The structure of nested if looks like this:

if(condition_1) {
   Statement1(s);

   if(condition_2) {
      Statement2(s);
   }
}

Statement1 would execute if the condition_1 is true. Statement2 would only execute if both the conditions( condition_1 and condition_2) are true.

Example of Nested if statement
public class NestedIfExample {

   public static void main(String args[]){
        int num=70;
	if( num < 100 ){ 
           System.out.println("number is less than 100"); 
           if(num > 50){
	      System.out.println("number is greater than 50");
	   }
	}
   }
}

Output:

number is less than 100
number is greater than 50
If else statement in Java

This is how an if-else statement looks:

if(condition) {
   Statement(s);
}
else {
   Statement(s);
}

The statements inside “if” would execute if the condition is true, and the statements inside “else” would execute if the condition is false.

Example of if-else statement
public class IfElseExample {

   public static void main(String args[]){
     int num=120;
     if( num < 50 ){
	System.out.println("num is less than 50");
     }
     else {
	System.out.println("num is greater than or equal 50");
     }
   }
}

Output:

num is greater than or equal 50

 

3 thoughts on “If..else Statement

  1. Some great use cases. Some times too many if statements can make sure code messy, Is there an alternative solution to it?

Leave a Reply

Your email address will not be published. Required fields are marked *