Mainly these keywords (break, continue and return) are use as branching statements which is use to change continuous execution of program.
Branching statements: break, continue, return
break
The break keyword is use to exit from while, do-while, for and switch statements. When a break is executed then execution of statements after break will stop and directly jump to next statement after current block of while, do-while or switch.
Example
In this example print the index of first even number found.
package statements.branching; public class BreakExample { public static void main(String[] args) { int[]numbers= {1,5,7,4,6,9,6,10,23,98,46,34}; boolean firstEven=false; int row=0; for(row=0;row<numbers.length;row++) { if(numbers[row]%2==0) { firstEven=true; break ; //break will exit from current loop //execute next statement after that } } if(firstEven) { System.out.println("First Even Found at :["+row+"]"); } } }
Output
First Even Found at :[3]
The break will exit from current loop and continue to execute next statement after the loop.
continue
The continue keyword is use inside a while, do- while, and for loop. When a continue is executed, the rest of the loop’s body is skipped and directly jump to the boolean expression for further execution.
Example
This program is to print even numbers between 1 to 20.
package statements.branching; public class ContinueExample { public static void main(String[] args) { for(int i=1;i<=20;i++) { if(i%2!=0) { //if odd number will continue not print statement for even continue; } System.out.println("Even Number :"+i); } } }
Output
Even Number :2
Even Number :4
Even Number :6
Even Number :8
Even Number :10
Even Number :12
Even Number :14
Even Number :16
Even Number :18
Even Number :20
When odd number will find out then continue statement in loop will skip next statements inside loop and go to next condition expression to check for further loop execution.
break and continue with label
The break keyword can optionally use a label to indicate which loop or switch you want a break to operate on, or which loop you want a or continue to operate on. This enables you to easily break out of nested loops or switch statements, or to continue nested loops. To do so you must label the beginning of the loop or switch statement:
Break Label example
public class BreakLabelExample { public static void main(String[] args) { int[][]numbers= {{1,5,7},{4,6},{9,6,10,23},{98,46,34}}; boolean firstEven=false; int row=0, col=0; loop: for(row=0;row<numbers.length;row++) { for(col=0; col<numbers[row].length;col++) { if(numbers[row][col]%2==0) { firstEven=true; break loop;//label break } } } if(firstEven) { System.out.println("First Even Found at :["+row+"]["+col+"]"); } } }
Output
First Even Found at :[1][0]
When the break loop statement is executed, the for loop labeled loop (the outer loop) is exited.
Continue Label Example
package statements.branching; import java.util.Arrays; public class BreakLabelExample { public static void main(String[] args) { int[][]numbers= {{1,6,7},{4,6},{9,6,10,23},{98,46,34}}; loop: for(int i=0;i<numbers.length;i++) { boolean allEven=true; for(int number:numbers[i]) { if(number%2!=0) { allEven=false; continue loop;//label continue } } if(allEven) { System.out.println("Processing the array of all even numbers :"+Arrays.toString(numbers[i])); } } } }
Output
Processing the array of all even numbers :[4, 6]
Processing the array of all even numbers :[98, 46, 34]
When continue loop statement executed, the for loop labeled loop( the outer loop) will continue.
The return Statement
The return statement returns from a method, potentially returning a value. If the method is declared as void, you must use a simple return statement :
retrun;
Otherwise, you must indicate a return value with a type that matches the return type of the method. For example, a method declared with a boolean return type could have the following statement:
return true;
Example
Program to get even numbers with in given range as starting number and ending number.
package statements.branching; import java.util.ArrayList; import java.util.List; public class ReturnExample { public static void main(String[] args) { int startNumber=5, endNumber=35; System.out.println("Even Numbers between : "+startNumber+" and "+endNumber); //calling method returning value Integer []numbers=evenNumbers(startNumber,endNumber); //calling method not returning value printNumbers(numbers); } /** * Method with retrun value */ public static Integer[] evenNumbers(int startNumber,int endNumber) { List list=new ArrayList(); int i=0; while(startNumber<=endNumber)//while loop { if(startNumber%2==0) { list.add(startNumber); } startNumber++; } //convert Arraylist to array return list.toArray(new Integer[list.size()]); } /** * Method without return value */ public static void printNumbers(Integer[]numbers) { if(numbers==null ||numbers.length==0) { System.out.println("No Numbers find out"); return; //return without value; //No statement executed after } for(int number:numbers)//for each loop System.out.println( number); } }
Output
Even Numbers between : 5 and 35
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
In this example using return with value in method evenNumbers() when returning integer array of even numbers. While in method printNumbers() having return type as void , use return statement without value because we don’t want to further execute statements of method when passing array is blank or empty.
You must log in to post a comment.