Java If-else Statement

The Java if statement is used to test the condition. It checks boolean condition: true or false.

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.

There are various types of if statement in java.
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement

a) if statement

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

Syntax:
if(condition){  
//code to be executed  
}  

The statements gets executed only when the given condition is true. If the condition is false then the statements inside if statement body will not be executed.

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

number is less than 300

b) nested if statement

When there is an if statement inside another if statement then it is called the nested if statement.

Syntax
if(condition_1) {
   Statement1(s);

   if(condition_2) {
      Statement2(s);
   }
}
Example:
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

c) 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:
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:
public class IfElseExample {  
public static void main(String[] args) {  
    int number=13;  
    if(number%2==0){  
        System.out.println("even number");  
    }else{  
        System.out.println("odd number");  
    }  
  }  
}
Output:

odd number

d) if-else-if Statement

The if-else-if statement executes one condition from multiple statements. In this statement we have only one “if” and one “else”, however we can have multiple “else if”. It is also known as if else if ladder.

Syntax:
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);
}
NOTE: In if-else-if statement, as soon as the condition is met, the corresponding set of statements get executed, rest gets ignored. If none of the condition is met then the statements inside “else” gets executed.
Example:
public class Sample {

    public static void main(String args[]) {
        int a = 30, b = 30;

        if (b > a) {
            System.out.println("b is greater");
        }
        else if(a > b){
            System.out.println("a is greater");
        }       
        else {
            System.out.println("Both are equal");
        }
    }
}
Output:

Both are equal

Related Post

New Features XCode 12 – IDE Supported Configurations Xcode 12 beta includes SDKs for iOS 14 iPadOS 14 tvOS 14 watchOS 7 macOS 11 The Xcode 12 beta release supports o...
Comments in C Comments in C Comments in C language are used to provide information about lines of code. It is widely used for documenting code. There are 2 types o...
Dart Libraries | Dart Tutorial Dart Libraries Dart has the following libraries, which are included in all Dart platforms: dart:core This library is automatically imported into ev...
C Language – Introduction Why C? There are three important aspects of any language so as C has : The way it stores data, The way it operates on this data and How th...
Setting Class Path for Java Setting Class Path for Java In this tutorial we are study how we can set the java JDK class path. Java is an open source software, it is is freely...
How to use Flutter most usable Widgets | Flutter t... We need some basic widgets for creating our first flutter app. Widgets describe what their view should look like given their current configuration and...
Author

Write A Comment