To perform the operation with conditional statements if statements are used . These statements controls the flow of execution of java program based upon conditions .
The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in java.
1) Simple if , statement
2) if - else , statement
3) Nested - if , statement
4) else - if ladder

  • Simple if statement

    It is the simple conditional statements which executes only true block of statements. If statements only accept boolean expression as condition.
    Syntax:

    if(boolean expression/text condition)
    {
    //true block;
    }
    rest of code;
    

    Simple if statement
    Example)
    drag_handle
     // example of simple  if statement
    class Test {
        public static void main(String[] args) {
            int x=5;
            if(x>3){
                System.out.println("This is true block");
            }
        }
    }
    
    Output:
    This is true block

    Let us see the another example of simple if block.

    // example of simple  if statement
    class Test {
        public static void main(String[] args) {
            int x=5;
            int y=6;
            int z=6;
            if((x+y+z)<=(x*y+5)){
                System.out.println("This is true block");
            }
        }
    }

    Output:
    This is true block
  • if ..else statement

    This conditional statement checks both true as well as false block. if the condition becomes true than it executes true block other wise it executes false block of statement.

    if ..else statement
    Example)
    drag_handle
     //example of if else statement
    
    class Test {
    
        public static void main(String[] args) {
            int x = 6;
            if (x > 6) {
                System.out.println("This is true block");
            } else {
                System.out.println("This is false block");
            }
        }
    }
    
    Output:
    This is false block
  • nested if...else statements

    whenever any if else statement is use inside another it else statement than it is called nested if..else statement.
    Syntax:

     if(condtion 1 /boolean expression)
    {
        if(condition 2 /expression)
        {
         //statment to be execute if both condtion1 and condtion 2 becomes true
        }else
        {
         //statment to be execute if both condtion1 =true and condtion =false
        }
    }
    else
    {
    // we can put if ...else block here also depending upon our requirement
    }

    Example)
    drag_handle
     //example of nested if else statement
    
    class Test {
    
        public static void main(String[] args) {
    
            int a = 5;
            int b = 6;
            int c = 1;
            if (a > b) {
                if (a > c) {
                    System.out.println("a is greatest one");
                } else {
                    System.out.println("c is greatest one");
                }
    
            } else if (b > c) {
                System.out.println("b is greatest");
            } else {
                System.out.println("c is greatest..");
    
            }
        }
    }
    
    Output:
    b is greatest

What next?

The next topic is Switch Statement in java

Share this page