Branching Statements in Java

By | April 5, 2020

In this tutorial, we will learn about Branching Statements in Java. Branching Statements helps us to set a flow of program according to our requirements. Suppose you want to execute a piece of code only in case of a certain situation. Then in this kind of situations, you can use branching statements to control the flow of the program. Java provides us with a variety of branching statements. you can use any according to your requirements.

List of Branching Statements:

  • if statement
  • if-else statement
  • else-if statement
  • switch statement

if statement: – we use if statement in case we want to run some piece of code only if a certain condition is true. Check out the following syntax to define if statement in Java.

if(condition){
  //body statement(s)
}

To understand the flow of the if statement. Please check out following flow chart diagram.

Branching Statements in Java

Example Program:

public class example {  
      public static void main(String[] args) {  
      int num=10;  
      if(num<20){  
        System.out.print("Yes Num is Less Than 20");  
      }  
    }  
}

if-else statement: – We know we use if statement in case we want to run a piece of code only if a certain condition is true. But if statement do nothing in case of the false condition. But in case you want to run code for both situations for true and false you can use if-else statement. Please check out the following syntax to define if-else statement in Java.

if(condition){
  //code in case condition is true
}else{
  //code in case condition is false
}

To understand the flow of the if-else statement. Please check out following flow chart diagram.

Branching Statements in Java

Example Program:

public class example {  
      public static void main(String[] args) {  
      int num=20;  
      if(num%2==0){  
        System.out.print("Num is Even");  
       }else{
        System.out.print("Num is Odd");
       }  
    }  
}  

else-if statement: – We use the else-if statement in case we have multiple conditions and we want to run a specific piece of code according to true condition. Please check out the following syntax to define else-if statement in Java.

if(condition){
  //statements  if above condition is true
}else if(condition){
  //statements if above condition is true
}else if(condition){
  //statements if above condition is true
}else{
  //statements if all above conditions are false
}

To understand the flow of the else-if statement. Please check out following flow chart diagram.

Branching Statements in Java

Example Program:

public class example {  
      public static void main(String[] args) {  
      int marks=80;  
      if(marks>=80){  
        System.out.print("You Got Grade A");  
       }else if(marks>=60){
        System.out.print("You Got Grade B");
       }else if(marks>=40){
        System.out.print("You Got Grade C");
       }else{
        System.out.print("Fail in this Subject");
       }
    }  
}  

switch statement: – we use the switch statement in case we have multiple choices. Please check out the following syntax to define switch statement in Java.

switch(choice variable(value)){
  case 1:
    //statement(s) for case 1
    break;
  case 2:
    //statement(s) for case 2
    break;
  case n:
    //statement(s) for case n
    break;
  default:
    //statement(s) for default case
}

To understand the flow of the switch statement. Please check out following flow chart diagram.

Branching statements in Java

Example Program:

public class example {  
      public static void main(String[] args) {  
      int dayNum=1;  
      switch(dayNum){
           case 1:
           System.out.println("Monday");
           break;
           case 2:
           System.out.println("Tuesday");
           break;
           case 3:
           System.out.println("wednesday");
           break;
           case 4:
           System.out.println("Thursday");
           break;
           case 5:
           System.out.println("Friday");
           break;
           case 6:
           System.out.println("Saturday");
           break;
           case 7:
           System.out.println("Sunday");
           break;
           default:
           Syatem.out.println("Enter Only From 1-7");
       }
    }  
}