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.
The break 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.
1) Unlabeled break statement
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.
Let's take an example to understand how the unlabeled break statement works to terminate the loop.
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.
Let's take an example to understand how the labeled break statement works to terminate the loop.
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
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.
In the above program, we use a do-while loop. We declare two variables x and y. The do-while loop executes until the x<=y. In the do block of the loop, we check whether the x is equal to y/2 or not. If the condition is matched, the statement skips the print and increment statement and continue the loop.
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.
Note: The type of the returned value should be matched with the type of the method's declared returned value.
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.
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.