The document discusses break and continue statements in Java. It explains that break exits the current loop or switch block, while continue skips the rest of the current loop iteration. Labeled breaks and continues allow skipping to the label. Examples show break exiting a for loop early, and continue skipping a value in a for loop. The document also provides sample quiz questions and frequently asked questions about differentiating between break and continue statements.
3. Recap
⢠In the previous class, we have discussed
⢠various iterative statements
3
4. break statement
â˘The break statement causes the program flow to
exit from the body of the loop
â˘In Java break statement is available in two types
â˘Unlabeled break
â˘Labeled break
4
5. Unlabeled break
⢠This is used to control the program flow in a
iterative statement structure / selective statement
structure.
⢠Syntax :
break;
5
6. Unlabeled break Contd..
⢠In switch statement we use break, it causes the
program flow exit from the body of the switch
⢠If break is used in loop, control goes to the
statement after the loop
⢠If break is used in nested loops , control goes to
the next outer loop
6
7. break Statement Contd..
test condition Break
End of the loop
Fig. 13.1 break statement
7
8. Example
class testbr {
public static void main(string args[]){
for (int j =10;j<=100; j=j+5){
if (j==50) break;
System.out.println(j);
}
System.out.println(âBreak enabledâ);
}
}
8
9. Example Contd..
⢠In the above program , for loop is designed for 10
to100 with 5 increment, the break statement
causes it to terminate before the final value of
100, when j equals to 50
9
10. Labeled break
⢠The labeled break causes the execution to resume
at the end of the labeled block
Syntax:
break label;
10
11. Labeled break Contd..
int n;
read_data;
while (âŚ) {
âŚ
for(âŚ) {
âŚ.
break read_data;
⌠} }
if( n<0) // this statement is executed immediately
after the break
{ ⌠}
11
12. The continue statement
⢠The continue statement sends control to the top of
the loop in which it occurs
12
13. The continue statement
while ( expression ) { do { for ( expr1; expr2; expr3 ) {
... ... ...
... ... ...
continue continue continue
... ... ...
... ... ...
} /* for */
} /* while */ } while (expression);
Fig. 13.2 continue statement
13
14. The continue statement Contd..
⢠In while and do while the continue statement
causes control to be transferred directly to the
conditional expression that control the loop
⢠In for loop , control go to iteration position and
then to the conditional expression
14
15. Continue Example
for (int k=1; k<=10; k++)
{
if (k = =5)
continue;
System.out.println(k);
}
Value 5 is not printed
Output : because of continue
1 2 3 4 6 7 8 9 10 statement
15
16. Labeled continue statement
⢠Skips remaining statements in nested-loop body
⢠Proceeds to the beginning of the specified
labeled block
16
17. Summary
⢠In this class , we have discussed, the following
statements
⢠break
⢠continue
⢠Labeled break
⢠Labeled continue
17
24. Frequently Asked Questions
⢠Differentiate between break and continue
statements.
⢠Explain about break and continue statement.
24
25. Â
swings
 Struts
jdbc
hibernate
home
java previous question papers
 OCT/NOV-2012 QUESTION PAPER
April / May 2012Â c-09
October/ November-2011 c-09
April/ May 2011 c-09
April/ May 2011 c-05
25
Editor's Notes
Chapter 6 - Notice that the ifâthen statement is not necessary, because we can write any ifâthen statement using ifâthenâelse by including no statement in the else block. For instance, the sample ifâthen statement can be written as if (testScore >= 95) { messageBox.show("You are an honor student"); } else { } In this book, we use if-then statements whenever appropriate.