What is the ‘if else’ statement in Java and how to use it?

Java 8Object Oriented ProgrammingProgramming

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

Syntax

if(Boolean_expression) {
   // Executes when the Boolean expression is true
}else {
   // Executes when the Boolean expression is false
}

If the boolean expression evaluates to true, then the if the block of code will be executed, otherwise else block of code will be executed.


Example

public class Test {
   public static void main(String args[]) {
      int x = 30;
      if( x < 20 ) {
         System.out.print("This is if statement");
       }else {
          System.out.print("This is else statement");
      }
   }
}

Output

This is else statement
raja
Published on 08-Feb-2018 13:00:41
Advertisements