Provide Best Programming Tutorials

if-else Statement In Java

The if-then-else Statement

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t. But what if we want to do something else if the condition is false. Here comes the else statement. We can use the else statement with if statement to execute a block of code when the condition is false.

if (condition){
    // Executes this block if condition is true
}else{
    //Executes this block if condition is false
}

The output below will be a<b is false since a<b is false.

public static void showIfElse() {
       int a = 20;
       int b = 100;
       if (a < b) {
           System.out.println("a is less than b!");
       } else {
           System.out.println("a is greater than b!");
       }
   }

This Post Has One Comment

  1. Very good article I like it!

Leave a Reply to kobe73er Cancel reply

Close Menu