Branching statements in Java6 min read

Introduction to Branching statements in Java

  • The “if condition” is the most basic decision-making Branching statement in Java. It is used to determine if a statement or a block of statements will be executed or not, i.e., if a condition is true, a block of statements will be executed; otherwise, it will not.
  • A “switch” statement allows a variable to be compared against a list of values for equality. Each value is referred to as a case, and each case is verified against the variable that is being turned on.
  • The switch statement makes it simple to route execution to different areas of code based on the expression’s value. The primitive data types byte, short, char, and int can all be used in the expression.
  • Return” and “break” statements are two important branching statements in Java that are used with control statements.
  • The return statement is used to return a value from a method, and it is done explicitly.
  • The break statement terminates or breaks the loop and moves control outside of it.
  • The “continue” statement aborts the current execution and returns control to the loop’s beginning.

Different types of branching statements in Java

1) if() statement

An if statement is used to select a statement for execution depending on whether the value of a given Boolean-valued expression is true or false. There are 5 five types of if statements:

  1. if
  2. nested if
  3. if….else
  4. nested if…..else
  5. if…else…if ladder

a) if

The if condition is the most basic decision-making condition. It is used to determine if a statement or a block of statements will be executed or not, i.e., if a condition is true, a block of statements will be executed; otherwise, it will not.

if(condition){
   // Statements to execute if the condition is true
}

After examination, the condition will be either true or false. The if statement accepts boolean values and executes the block of statements behind it if the value is true. If the curly brackets { and } are not provided after if( condition ), the if statement will regard the immediate one statement to be inside its block by default. As an example,

// Java program to illustrate If statement
class IfDemo{
  public static void main(String[] args){
    int num = 5;
    if (num < 10)
      System.out.println("5 is less than 10"); // This statement will be executed as the if condition considers one statement by default
    System.out.println("I am Not in if"); //This is outside the if condition
  }
}

Output:

5 is less than 10
I am Not in if

b) nested if

An if statement that is the target of another if or otherwise is known as a nested if. An if statement inside an if statement is referred to as nested if statements. Yes, we may stack if statements within if statements in Java. We can put an if statement inside another if statement.

Syntax:

if (firstCondition){
  // Executes when firstCondition is true
  if (secondCondition){
    // Executes when secondCondition is true
  }
}

Example:

// Java program to illustrate nested-if statement
class NestedIfDemo{
  public static void main(String[] args){
    int num = 5;
    if (num == 5){
      // First if statement
      if (num < 10)
        System.out.println("num is smaller than 10");
      // Nested - if statement
      // Will only be executed if the above statement is true
      if (i < 20)
        System.out.println("num is smaller than 20 too");
      else
        System.out.println("num is greater than 20");
    }
  }
}

Output:

num is smaller than 10
num is smaller than 20 too

c) if-else

If a condition is true, the if statement tells us that a group of statements will be executed, and if the condition is false, it will not. But what if the condition is false and we want to do something else? This is when the if-else statement comes in. When the condition is false, we can combine the else statement with the if statement to execute a block of code.

Syntax:

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

Example:

// Java program to illustrate if-else statement
class IfElseDemo{
  public static void main(String[] args){
    int num = 5;
    if (num < 10)
      System.out.println("num is smaller than 10");
    else
      System.out.println("num is greater than 10");
  }
}

Output:

num is smaller than 10

d) nested if…..else

When an if-else statement is embedded inside an if statement then the program control structure is called a nested if-else. If the outer if statement’s condition is false, the statement attached to its else(optionally expressed) part is performed instead of the nested if-else statement.

  • The statement connected to the if of the nested if-else is only executed when
    • The outer if statement’s condition is true, and
    • This nested if statement’s condition is also true.
    • The statement with its associated else is performed if the condition of this nested if is false.
//nested if-else example
public class NestedIfElse{
  public static void main(String[] args){
    if(99<100){
      if(99<98)
        System.out.println("99 is less than 100 & 99 is also less than 98");
      else
        System.out.println("99 is less than 100 but 99 is not less than 98");
    }else 
      System.out.println("99 is not less than 100");
  }
}

Output:

99 is less than 100 but 99 is not less than 98

e) if…else…if ladder

A user can choose from a variety of alternatives here. From the top-down, the if statements are executed. The statement linked with that if is performed as soon as one of the conditions controlling the if is true, and the rest of the ladder is bypassed. The final else line will be executed if none of the conditions are true.

Syntax:

if (condition){
  statement1;
  statement2;
}else if (condition){
  statement1;
  statement2;
}else if (condition){
  .
  .
}else{
  statement;
}

Example:

// Java program to illustrate if-else-if ladder
class ifelseifDemo{
  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

2) switch() statement

This is a multi-way selection facility in Java. The switch() statement is mostly used in processing menus.

Syntax :

switch (expression){
  case value1:
    statement1;
    break;
  case value2:
    statement2;
    break;
  .
  .
  case valueN:
    statementN;
    break;
  default:
    statementDefault;
}

Break statement: Break is most commonly used in Java to end a sequence in a switch statement or to escape a loop. In this example, the break statement is used to end a statement sequence inside the switch(). The break statement is not required if this parameter is left blank; execution will go to the next case.

Example:

// Java program to illustrate switch-case
class SwitchCaseDemo{
  public static void main(String args[]){
    int i = 9;
    switch (i){
      case 0:
        System.out.println("i is zero.");
        break;
      case 1:
        System.out.println("i is one.");
        break;
      case 2:
        System.out.println("i is two.");
        break;
      default:
        System.out.println("i is greater than 2.");
    }
  }
}

Output:

i is greater than 2.

3) return statement

The return statement is used to return from a method directly. That is, it causes program control to be returned to the method’s caller.

// Java program to illustrate using return
class Return{
  public static void main(String args[]){
    boolean t = true;
    System.out.println("Before the return.");
    if (t)
      return;
    // Compiler will bypass every statement after return
    System.out.println("This won't execute.");
  }
}

Output:

Before the return.

Share:

Leave a Reply