Page 204 - CA_Blue( J )_Class10
P. 204
9.4.2 Using the break Statement in Inner Loop
To use the break statement in inner loop, the break statement should be written in the body of the inner loop as shown
in the following code:
1 class innerexit
2 {
3 public static void main()
4 { int i ,j;
5 for(i=1;i<=5;i++)
6 { for(j=1;j<=i;j++)
7 { if(i%2==0)
8 break;
9 System.out.print(j+":"); }
10 System.out.println();
11 }
12 }
13 }
Output: 1:
1:2:3:
1:2:3:4:5:
Explanation: When the value of “i” is an even number, the condition becomes true and the break statement executes
and the control comes out of the inner loop and the next iteration of the outer loop will execute.
9.5 USING THE CONTINUE STATEMENT IN NESTED LOOP
The continue statement is used for executing the next iteration of the loop without executing the rest of the loop
statements. Placing the continue statement in the correct position will allow the control to execute the next iteration
of the inner loop or outer loop.
9.5.1 Using the continue Statement in the Outer Loop
To use the continue statement in the outer loop, the continue statement should be written in the body of the outer
loop as shown in the following code:
1 class outercontinue
2 {
3 public static void main()
4 { int i ,j;
5 for(i=1;i<=5;i++)
6 { for(j=1;j<=i;j++)
7 { System.out.print(j+":"); }
202202 Touchpad Computer Applications-X

