Java

If, If..Else Statement In Java

Use control flow statements when a group of statements has to be executed based on a condition. For instance, we want to print “Positive Number” if a number is more than zero and “Negative Number” if it is less than zero. Although the program in this instance has two print statements, only one of them is actually executed at a time depending on the input value. We’ll examine how to use control statements to create these kinds of conditions in a Java application.

Based on the following requirements, this tutorial will demonstrate four different sorts of control statements you may use in Java programs: We will cover the following conditional statements in this tutorial:

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

If statement

If a condition is met, a statement or group of statements will follow as illustrated below:

if(condition){
  Statement(s);
}

Only when the stated condition is true will the assertions be carried out. The assertions contained in the if statement body are entirely disregarded if the condition is untrue.

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

The term “nested if statement” refers to an if statement that is included within another if statement.
The nested if structure is as follows:

if(condition_1) {
   Statement1(s);

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

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

An if-else sentence appears like follows:

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

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

if-else-if Statement

When we need to check several conditions, we utilize the if-else-if statement. There is only one “if” and one “else” in this sentence, however there may be more than one “else if.” It also goes by the name “if else if ladder.” It appears as follows:

if(condition_1) {
   /*if condition_1 is true execute this*/
   statement(s);
}
else if(condition_2) {
   /* execute this if condition_1 is not met and
    * condition_2 is met
    */
   statement(s);
}
else if(condition_3) {
   /* execute this if condition_1 & condition_2 are
    * not met and condition_3 is met
    */
   statement(s);
}
.
.
.
else {
   /* if none of the condition is true
    * then these statements gets executed
    */
   statement(s);
}

The most crucial thing to keep in mind is that in an if-else-if statement, the relevant set of words is performed as soon as the condition is satisfied, while the remainder is disregarded. The statements included in “otherwise” are carried out if none of the conditions are satisfied.

Example of if-else-if

public class IfElseIfExample {

   public static void main(String args[]){
	int num=1234;
	if(num <100 && num>=1) {
	  System.out.println("Its a two digit number");
	}
	else if(num <1000 && num>=100) {
	  System.out.println("Its a three digit number");
	}
	else if(num <10000 && num>=1000) {
	  System.out.println("Its a four digit number");
	}
	else if(num <100000 && num>=10000) {
	  System.out.println("Its a five digit number");			
	}
	else {
	  System.out.println("number is not between 1 & 99999");			
	}
   }
}

Output:

Its a four digit number

Related Articles

Leave a Reply

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

Back to top button