Download Java Branching Statements in Formats
QR Code for this Page
Java Branching Statements
Complete Tutorial on Java Branching Statements.

Java Branching Statements

Java Control Flow Statements

Branching Statements in Java

  • Java provides three branching statements break, continue and return.
  • Branching statements in Java are used to change the normal flow of execution based on some condition.
  • The return branching statement is used to explicitly return from a method.
  • break branching statement is used to break the loop and transfer control to the line immediate outside of loop.
  • continue branching statement is used to escape current execution and transfers control back to the start of the loop.
  • Java branching statements break and continue statements have two forms: the labeled form and the unlabeled form.

break keyword can also be used inside switch statement to break current choice. In this tutorial you can learn about Java Branching Statements like break Statement, continue Statement and return Statement. This tutorial is also example for branching statements in Java. This tutorial also available in doc and pdf formats.

What are the Branching Statements in Java ?

Java provides 3 branching statements named break, continue and return. Branching statements are used to change the normal flow of execution based on some condition. Some branching statements are used in looping statements and others used to change the flow of execution of the program.

What are the statements used for branching in Java ?

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 are break , continue , return and goto .

What are Jump Statements in Java ?

In Java, Jump statements are used to unconditionally transfer program control from one point to elsewhere in the program. Jump statements are primarily used to interrupt loop or switch-case instantly. Java supports three jump statements break, continue, and return.

How to use break Statement in Java ?

break statement is used to break the loop and transfer control to the line immediate outside of loop. In the following break statement example you can learn how to use break statement in Java. This explanation also for What is break Statement in Java ?

Java break Statement Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*  Java break Statement Example
    Save with file name BreakStatement.java */
  
public class BreakStatement
{
  public static void main(String args[])
  {
    System.out.println("Java break Statement Example ");
    int count = 10;
    //INFINIT LOOP
    while(true)
    {
      if(count == 0)
        break;
      System.out.println(count);
      count--;
    }
  }
}

How to use continue Statement in Java ?

continue statement is used to escape current execution and transfers control back to the start of the loop. In the following continue statement example you can learn how to use continue statement in Java. This explanation also for What is continue Statement in Java ?

Java continue Statement Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*  Java continue Statement Example
    Save with file name ContinueStatement.java  */
  
public class ContinueStatement
{
  public static void main(String args[])
  {
    System.out.println("Java continue Statement Example ");
    int count = 1;
    while(count<=100)
    {
      if(count <= 10)
      {
        System.out.println(count);
        count++;
        continue;
      }
      else
      {
        break;
      }
    }
  }
}

How to use return statement in Java ?

The return statement is used to explicitly return from a method. That is, it causes program control to transfer back to the caller of the method. The return statement can be used to cause execution to branch back to the caller of the method. Thus, the return statement immediately terminates the method in which it is executed. The return statement has two forms:

  • One that returns a value
  • One that does not returns a value

To return a value, simply put the value (or an expression that calculates the value) after the return keyword. This explanation also for What is return Statement in Java ? and How many forms of return Statement in Java ?. In the following return statement example you can learn how to use return branching statement in Java.

Java return Statement Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/*  Java return Statement Example
    Save with file name ReturnStatement.java  */
  
public class ReturnStatement
{
  public static void main(String args[])
  {
    System.out.println("Java return Statement Example ");
    int max = getMaximumNumber(10, 20);
    if(max==0)
      System.out.println("Equal Numbers");
    else
      System.out.println("Max Number is : " + max);
  }
  
  //METHOD
  public static int getMaximumNumber(int x, int y)
  {
    if(x > y)
      return x;
    else if(x < y)
      return y;
    else
      return 0;
  }
}

How to use Labeled break and continue Statements in Java ?

The most common to use break and continue statement labels with looping statements like for or while. A label statement must be placed just before the statement being labeled, and it consists of a valid identifier that ends with a colon : . The labeled varieties are needed only in situations where you have nested loop and need to indicate which of the nested loops you want to break. The break statement will exit out of the labeled loop, as opposed to the innermost loop. The following branching statements example shows how to use labeled break and continue statements.

Labeled break and continue statements Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*  Labeled break and continue statements Example
    Save with file name LabeledStatements.java  */
  
public class LabeledStatements
{
  public static void main(String args[])
  {
    System.out.println("Labeled break and continue statements Example");
    outer:
    for(int i = 1 ; ; i++)
    {
        for(int j = 1 ; j < 11 ; j++)
        {
          if( j > i)
          {
            System.out.println();
            continue outer;
          }
          System.out.print(" *");
        }
        if(i == 10)
          break;
    }
  }
}

How to use Unlabeled break and continue Statements in Java ?

An unlabeled break statement terminates the innermost switch, for, while, or do-while statement. But a labeled break terminates an outer statement. The following unlabeled branching statements example shows how to use unlabeled break and continue statements.

Unlabeled break and continue statements Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*  Unlabeled break and continue statements Example
    Save with file name UnlabledStatements.java */
  
public class UnlabledStatements
{
  public static void main(String args[])
  {
    System.out.println("Unlabeled break and continue statements Example");
    int even_odd_numbers[] = {1,2,4,99,7,8};
    int even_sum = 0;
    for(int i = 0; i < even_odd_numbers.length; i++)
    {
        if((even_odd_numbers[i] % 2) == 0)
        {
          even_sum += even_odd_numbers[i];
          continue;
        }
        if(even_odd_numbers[i] == 99)
        {
          break;
        }
    }
    System.out.println("Even Numbers Sum : " + even_sum);
  }
}

Java Branching Statements Summary

Java provides 3 branching statement named break, continue and return. Branching statements are used to change the normal flow of execution based on some condition. The Java return statement is used to explicitly return from a method.

Download Java Branching Statements in Formats