Branching Statements in Java

Branching Statements in Java with Examples

In this article, I am going to discuss the Branching Statements in Java with Examples. Please read our previous article, where we discussed Looping Statements in Java with examples. At the end of this article, you will understand what are Branching Statements in Java and its type with examples.

What are Branching Statements in Java?

Branching statements allow the flow of execution to jump to a different part of the program. The common branching statements used within other control structures include: break, continue, and return.

Types of Branching Statement in Java:

In Java, there are three Branching Statements. They are as follows:

  1. Break Statement
  2. Continue Statement
  3. Return Statement
Break statement in Java:

Using break, we can force immediate termination of a loop, bypassing the conditional expression and any remaining code in the body of the loop.

Note: Break, when used inside a set of nested loops, will only break out of the innermost loop.

Syntax: break label;

Sample Program for break statement in Java:
class BreakStatementDemo 
{ 
    public static void main(String args[]) 
    { 
        // Initially loop is set to run from 0-9 
        for (int i = 0; i < 10; i++) 
        { 
            // terminate loop when i is 5. 
            if (i == 5) 
                break; 
  
            System.out.println("i: " + i); 
        } 
        System.out.println("Loop complete."); 
    } 
}

Output:
Branching Statements in Java with Examples

Continue Statement in Java:

The continue statement is used to skip a part of the loop and continue with the next iteration of the loop. It can be used in combination with for and while statements.

Sample Program for Continue statement in Java:
class ContinueStatementDemo 
{ 
    public static void main(String args[]) 
    { 
        for (int i = 0; i < 10; i++) 
        { 
            // If the number is even 
            // skip and continue 
            if (i%2 == 0) 
                continue; 
  
            // If number is odd, print it 
            System.out.print(i + " "); 
        } 
    } 
}

Output: 1 3 5 7 9

Return Statement in Java:

This is the most commonly used branching statement of all. The return statement exits the control flow from the current method and returns to where the method was invoked. The return statement can return a value or may not return a value. To return a value, just put the value after the return keyword.

But remember data type of the returned value must match the type of the method’s declared return value. And so when a method is declared void, use return without a return value.

Sample Program for return statement:
class ReturnStatementDemo
{ 
    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.

In the next article, I am going to discuss Methods in Java with examples. Here, in this article, I try to explain Branching Statements in Java with examples and I hope you like this article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

2 thoughts on “Branching Statements in Java”

Leave a Reply

Your email address will not be published. Required fields are marked *