Branching Statements in Java
Branching Statements in Java
Branching statements are the statements used to jump the flow of execution from one part of a
program to another. The branching statements are mostly used inside the control statements. Java has
mainly three branching statements, i.e., continue, break, and return. The branching statements allow us
to exit from a control statement when a certain condition meet.
In Java, continue and break statements are two essential branching statements used with the control
statements. The break statement breaks or terminates the loop and transfers the control outside the
loop. The continue statement skips the current execution and pass the control to the start of the loop.
The return statement returns a value from a method and this process will be done explicitly.
1. Break Statement
2. Continue Statement
3. Return Statement
The labeled and unlabeled break statement are the two forms of break statement in Java. The break
statement is used for terminating a loop based on a certain condition. Let's understand each form
of break statement one by one with their examples.
The unlabeled break statement is used to terminate the loop that is inside the loop. It is also used to
stop the working of the switch statement. We use the unlabeled break statement to terminate all the
loops available in Java.
Syntax:
}
}
Let's take an example to understand how the unlabeled break statement works to terminate the
loop.
UnlabeledBreakExample.java
class UnlabeledBreakExample {
}
}
if (foundName) {
}
}
}
Output:
Explanation
In the above program, we search for a name in an array of type string. The break keyword is used in
the for loop using a conditional statement. When the condition is met for the search name,
the break statement exit us from the loop and pass the control flow to the outside of the loop.
2) Labeled break statement
Labeled break statement is another form of break statement. If we have a nested loop in Java and use
the break statement in the innermost loop, then it will terminate the innermost loop only, not the
outermost loop. The labeled break statement is capable of terminating the outermost loop.
Syntax:
label:
//Code
//Code
}
}
}
Let's take an example to understand how the labeled break statement works to terminate the loop.
LabeledBreakExample.java
class LabeledBreakExample {
// Labeling the outermost loop as outerMost
outerMost:
// Labeling the innermost loop as innerMost
innerMost:
// Terminating the outemost loop
}
}
}
}
Output:
}
}
}
Output:
Explanation
In the above program, we have created nested for loop . In the innermost loop, we set a condition to break the outermost loop. When the condition is met, the break statement terminates that loop whose label is associated with the
break keyword.
The continue statement is another branching statement used to immediately jump to the next iteration of the loop. It is a special type of loop which breaks current iteration when the condition is met and start the loop with the next
iteration. In simple words, it continues the current flow of the program and stop executing the remaining code at the specified condition.
When we have a nested for loop, and we use the continue statement in the innermost loop, it continues only the innermost loop. We can use the continue statement for any control flow statements like for , while , and do-while .
Syntax
control-flow-statement;
continue ;
ContinueExample.java
//Declare variables
//Using do while loop for using contiue statement
do {
x++;
}
System.out.println(x);
x++;
}
}
Output:
The return Statement
The return statement is also a branching statement, which allows us to explicitly return value from a
method. The return statement exits us from the calling method and passes the control flow to where
the calling method is invoked. Just like the break statement, the return statement also has two forms,
i.e., one that passes some value with control flow and one that doesn't.
Syntax
return value;
Or,
return ;
Note: The type of the returned value should be matched with the type of the method's declared
returned value.
ReturnExampleWithoutValue.java
class ReturnExampleWithoutValue {
//Declare calling method
{
number++;
System.out.println(number);
}
{
obj.increment( 4 );
obj.increment( 12 );
}
}
Output:
Explanation
In the above code, we create a class having the increment() method. In this method, we check whether
the number smaller than 10 or not. If the number is less than 10, the return statement passes the
control flow to where the method calls and doesn't execute the increment and print statement.
ReturnExampleWithValue.java
class ReturnExampleWithValue {
{
sum = x + y;
}
{
diff = x - y;
}
public static void main(String[] args)
{
}
}
Output:
Explanation
In the above program, we create two methods that return the integer value. The first method returns
the sum of numbers and the second method returns the difference between the numbers. In both
methods, an integer value is associated with the return statement to pass the value with the control
flow to where the method calls. In the main method, both the methods are called from the print
statement, so the print statement directly prints the value returned from the methods.