2.
Java Branching Statements
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.
Syntax:
1. for (int; testExpression; update){
2. //Code
3. if(condition to break){
4. break;
5. }
6. }
Let's take an example to understand how the unlabeled break statement works to
terminate the loop.
UnlabeledBreakExample.java
1. class UnlabeledBreakExample {
2. public static void main(String[] args) {
3.
4. String[] arr = { "Shubham", "Anubhav", "Nishka", "Gunjan", "Akash" }
;
5. String searchName = "Nishka";
6.
7. int j;
8. boolean foundName = false;
9.
10. for (j = 0; j < arr.length; j++) {
11. if (arr[j] == searchName) {
12. foundName = true;
13. break;
14. }
15. }
16.
17. if (foundName) {
18. System.out.println("The name " + searchName + " is found at
index " + j);
19. } else {
20. System.out.println("The name " +searchName + " is not foun
d in the array");
21. }
22. }
23. }
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:
1. label:
2. for (int; testExpression; update){
3. //Code
4. for (int; testExpression; update){
5. //Code
6. if(condition to break){
7. break label;
8. }
9. }
10. }
Let's take an example to understand how the labeled break statement works to
terminate the loop.
LabeledBreakExample.java
1. class LabeledBreakExample {
2. public static void main(String[] args) {
3. int j, k;
4.
5. // Labeling the outermost loop as outerMost
6. outerMost:
7. for(j=1; j<5; j++) {
8.
9. // Labeling the innermost loop as innerMost
10. innerMost:
11. for(k=1; k<3; k++ ) {
12. System.out.println("j = " + j + " and k = " +k);
13.
14. // Terminating the outemost loop
15. if ( j == 3)
16. break outerMost;
17. }
18. }
19. }
20. }
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
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
1. control-flow-statement;
2. continue;
ContinueExample.java
1. public class ContinueExample {
2. public static void main(String[] args) {
3. //Declare variables
4. int x = 1;
5. int y = 10;
6. //Using do while loop for using contiue statement
7. do{
8. if(x == y/2){
9. x++;
10. continue;//The continue statement skips the remaining sta
tement
11. }
12. System.out.println(x);
13. x++;
14. }while(x <= y);
15. }
16. }
Output:
Explanation
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.
Syntax
1. return value;
Or,
1. return;
Note: The type of the returned value should be matched with the type of the method's declared
returned value.
ReturnExampleWithoutValue.java
1. class ReturnExampleWithoutValue {
2. //Declare calling method
3. void increment(int number)
4. {
5. if (number < 10)
6. return; //pass the control flow to where this method call
7. number++;
8. System.out.println(number);
9. }
10. public static void main(String[] args)
11. {
12. ReturnExampleWithoutValue obj = new ReturnExampleWithoutV
alue();
13. obj.increment(4);
14. System.out.println("In main");
15. obj.increment(12);
16. System.out.println("In main");
17. }
18. }
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
1. class ReturnExampleWithValue {
2. int sum(int x, int y)
3. {
4. int sum = 0;
5. sum = x + y;
6. return sum;
7. }
8. int difference(int x, int y)
9. {
10. int diff = 0;
11. diff = x - y;
12.
13. return diff;
14. }
15. public static void main(String[] args)
16. {
17. ReturnExampleWithValue obj = new ReturnExampleWithValue();
18. System.out.println("The sum of 10 and 3 is : "+ obj.sum(10, 3))
;
19. System.out.println("The difference between 10 and 3 is : "+ obj.
difference(10, 3));
20. }
21. }
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.