Continue Break: 2. Java Branching Statements
Continue Break: 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.
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. }
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.
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. }
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.
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.
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.