We saw in last posts, how for, while and do-while loops control the program flow and how the program exits after the condition is met. But there are two more statements, which can skip statements as needed and allow the program to come out of the loop without meeting condition criteria.
Break Statement break;
One is the break statement, which we have already used in switch-case statements. If you don't use break statement in switch-case statement, the program will keep executing other case statements, as program doesn't know where to stop. We can use break statements with for, while and do-while loops too. In the loop, break statement allows the program to come out from the immediate loop and executes next statement after loop.
Suppose we have to add all the numbers starting from 1 till the sum becomes 100. We will write a program with for loop adding all the numbers, and as soon as the sum reaches 100, the program will exit from the for loop with the help of break statement.
Class BreakExample1 (BreakExample1.java)
public class BreakExample1 { public static void main(String args[]) { int sum = 0; for(int i = 1; i < 20; i++) { sum = sum + i; if(sum >= 100) break; System.out.print(i + " "); } } }
And here is the result:
1 2 3 4 5 6 7 8 9 10 11 12 13
The program came out from the for loop as soon as the value of variable sum is greater than 100, it didn't completed all the possible iterations (i < 20).
Suppose we have asked the user to enter only single digit numbers, as soon as he enters 0 or more than 1 digit number, the program comes out from the loop and prints the last entered number. We have used here break statement with the while loop.
Class BreakExample2 (BreakExample2.java)
import java.util.Scanner; public class BreakExample2 { public static void main(String args[]) { int i, last = -1; System.out.println("Please enter single digit numbers only"); Scanner sc = new Scanner(System.in); do { i = sc.nextInt(); if((i == 0) || (i >= 10)) { System.out.println("Stop"); break; } last = i; } while(true); if(last == -1) System.out.println("Last entered number is none"); else System.out.println("Last entered number is " + last); } }
And here is the result:
Please enter single digit numbers only
4
3
2
9
11
Stop
Last entered number is 9
There is one more form of break statement - break statement with label. This kind of break statement is used when there are nested loops (loop in a loop). So, you can label all the loops, and when you specified the label with break statement, the program will come out from the specified loop or only the innermost loop if not specified. It's syntax is -
Please enter single digit numbers only
4
3
2
9
11
Stop
Last entered number is 9
There is one more form of break statement - break statement with label. This kind of break statement is used when there are nested loops (loop in a loop). So, you can label all the loops, and when you specified the label with break statement, the program will come out from the specified loop or only the innermost loop if not specified. It's syntax is -
break label;
Here in the example, I have created two labels for two different for loops. And I have used break statement with label name. The program exits from the outer loop.
Class BreakExample3 (BreakExample3.java)
8 : 4 Done
But, if I remove the label from the break statement, different results will come. Here you can see the difference.
public class BreakExample3 { public static void main(String args[]) { int sum = 0; loop1: for (int j = 10; j >= 1; j--) { loop2: for (int i = 1; i < 10; i++) { if (i == 2*j) { System.out.println(i + " : " + j); System.out.print("Done"); System.out.println(); break loop1; } } } } }Result is:
8 : 4 Done
But, if I remove the label from the break statement, different results will come. Here you can see the difference.
Class BreakExample4 (BreakExample4.java)
8 : 4
Done
6 : 3
Done
4 : 2
Done
2 : 1
Done
public class BreakExample4 { public static void main(String args[]) { int sum = 0; loop1: for (int j = 10; j >= 1; j--) { loop2: for (int i = 1; i < 10; i++) { if (i == 2*j) { System.out.println(i + " : " + j); System.out.print("Done"); System.out.println(); break; } } } } }Result is:
8 : 4
Done
6 : 3
Done
4 : 2
Done
2 : 1
Done
And if we write label after for statement but before opening braces {, the different result will come. So, you have to be careful where to put labels. And break statement can only break the labeled loop if it is enclosed by same label.
Class BreakExample5 (BreakExample5.java)
8 : 4
Done
6 : 3
Done
4 : 2
Done
2 : 1
Done
Continue Statement continue;
Class BreakExample5 (BreakExample5.java)
public class BreakExample5 { public static void main(String args[]) { int sum = 0; for (int j = 10; j >= 1; j--) loop1: { loop2: for (int i = 1; i < 10; i++) { if (i == 2*j) { System.out.println(i + " : " + j); System.out.print("Done"); System.out.println(); break loop1; } } } } }Result is same here:
8 : 4
Done
6 : 3
Done
4 : 2
Done
2 : 1
Done
Continue Statement continue;
Continue statements allow the program to skip remaining statement after it and goes to next iteration / condition expression of the loop. Th program doesn't come out from the loop until the condition is false or there is a break statement.
I got one question in 4th grade math book. I tried to write a program to solve the question using continue statement. Q: What is the least multiple of 9 that is greater than 150?
Class ContinueExample (ContinueExample.java)
Class ContinueExample (ContinueExample.java)
public class ContinueExample { public static void main(String args[]) { for(int i = 1; i <= 20; i++) { if(9*i < 150) continue; System.out.print(9*i + " "); } } }
And the results is showing all the possible multiples after 150. So, the least multiple is the smallest number - 153.
153 162 171 180
153 162 171 180
No comments:
Post a Comment