Computer Applications
Distinguish between the following:
break statement and labelled break statement
Java Nested for Loops
4 Likes
Answer
break statement | labelled break statement |
---|---|
break statement is used to take the control out of the loop control structure immediately enclosing it. | Labelled break statement transfers program control out of the code block whose label is specified as its target. |
Syntax:break; | Syntax:break <label>; |
For example, for (int i = 1; i <= 3; i++) { for(int j = 1; j <= 2; j++) { if (i == 2) { break; } System.out.println("Iteration value of inner loop: " + j); } System.out.println(); } | For example, outer: for (int i = 1; i <= 3; i++) { for(int j = 1; j <= 2; j++) { if (i == 2) { System.out.println("Terminating the Outer-loop"); break outer; } System.out.println("i =" + i +" ; j =" + j); } |
Answered By
2 Likes
Related Questions
Write a program in Java to display the following patterns.
1 2 3 4 5 2 3 4 5 3 4 5 4 5 5 4 5 3 4 5 2 3 4 5 1 2 3 4 5
Distinguish between the following:
continue statement and labelled continue statement
Write a program in Java to display the following patterns.
1 2 3 4 5 1 2 3 4 1 2 3 1 2 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5
What will be the value of sum after each of the following nested loops is executed?
int sum = 0; for (int i = 0; i <= 10; i++) for (int j = 0; j <= 10; j++) sum += i;