Branching Statements(if ,If-else,nested if,if else if )

Branching Statements

Decision Making in programming is similar to decision making in real life. In programming also we face some situations where we want a certain block of code to be executed when some condition is fulfilled.

  1. If
  2. If-else
  3. nested-if
  4. if -else-if
  5. switch case
  6. jump-brake,continue, return
If Statement:-
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.
    Syntax:
        if(condition)
        {  
            Statements;
        }  


Flowchart:-

Examples:-

class Example 

{  

public static void main(String[] args) 

{  

       int age=20;  

        if(age>18)

{  

         System.out.println("Age is greater than 18");  

     }  

}  

}  

Output: 

Age is greater than 18

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)
                {  
                        Statements;
                }
               else
                {  
                        Statements;
                }  
Flowchart:-
Example:-
class Example 
{  
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

Nested-if:-
A nested if is an if statement that is the target of another if or else. Nested if statements means an if statement inside an if statement.

Syntax:

if (condition 1) 

{

if (condition 2) 

        {

             Statements;

        }

}

Flowchart:-


Example:-

class Demo
{
    public static void main(String args[])
    {
        int i = 10;
  
        if (i == 10)
        {
                      if (i < 15)
                            {
                 System.out.println("i is smaller than 15");
  
                               if (i < 12)
{
                         System.out.println("i is smaller than 12 too");
             }else
                                                    {
                System.out.println("i is greater than 15");
                                                     }
                               }
                               }
        }
}
Output:-
    i is smaller than 15
    i is smaller than 12 too

if -else-if:-
Here, a user can decide among multiple options.
The if statements are executed from the top down. 
As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

Syntax:-

        if (condition)

        {

            statement;

    }else if (condition)

        {

            statement;

            .

            .

        }else

    {

    statement;

    }

Flowchart:-



Example:-

class Demo

{

    public static void main(String args[])

    {

        int i = 20;

  

        if (i == 10)

            System.out.println("i is 10");

        else if (i == 15)

            System.out.println("i is 15");

        else if (i == 20)

            System.out.println("i is 20");

        else

            System.out.println("i is not present");

    }

}


Output:-
i is 20

Comments

Popular posts from this blog

Java Package

Life cycle of a Thread and creating thread class

Inheritance in Java