SlideShare a Scribd company logo
1 of 25
Syntax for break and continue statements




                                           1
Objective

• On completion of this lecture , you would be able
  to know
   • break and continue statements




                                                      2
Recap

• In the previous class, we have discussed


• various iterative statements




                                             3
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
Unlabeled break

• This is used to control the program flow in a
  iterative statement structure / selective statement
  structure.

• Syntax :

  break;




                                                    5
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
break Statement                  Contd..




         test condition     Break




         End of the loop




Fig. 13.1 break statement


                                              7
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
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
Labeled break

• The labeled break causes the execution to resume
  at the end of the labeled block

 Syntax:

      break label;




                                                     10
Labeled break             Contd..

int n;
  read_data;
  while (…) {
         …
         for(…)         {
         ….
         break read_data;
         …              }    }
  if( n<0) // this statement is executed immediately
  after the break
  {      …        }


                                                          11
The continue statement

• The continue statement sends control to the top of
  the loop in which it occurs




                                                   12
The continue statement


while ( expression ) {      do {                  for ( expr1; expr2; expr3 ) {


           ...                         ...                             ...
         ...                          ...                              ...
      continue                     continue                         continue

           ...                         ...                             ...
           ...                         ...                             ...
                                                      } /* for */
  } /* while */               } while (expression);
                          Fig. 13.2 continue statement
                                                                             13
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
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
Labeled continue statement



•   Skips remaining statements in nested-loop body
•   Proceeds to the beginning of the specified
    labeled block




                                                 16
Summary

• In this class , we have discussed, the following
  statements
   •   break
   •   continue
   •   Labeled break
   •   Labeled continue




                                                     17
Quiz

1.Continue statement can be used in control
  structure. [True/False]




                                              18
Quiz

1.Continue statement can be used in control
  structure. [True/False]




                                              19
Quiz

2.Break statement can be used in control structures.

                                 [True/false]




                                                       20
Quiz

2.Break statement can be used in control structures.

                                 [True/false]




                                                       21
Quiz

3.Break violates rules of structured programming

                                [True/false]




                                                   22
Quiz

3.Break violates rules of structured programming

                                [True/false]




                                                   23
Frequently Asked Questions

•    Differentiate between break and continue
    statements.

• Explain about break and continue statement.




                                                24
 
                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

More Related Content

Similar to 9 cm604.13

Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetitionOnline
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRKrishna Raj
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsIt Academy
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptxAdrianVANTOPINA
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsEng Teong Cheah
 
Python decision making_loops part7
Python decision making_loops part7Python decision making_loops part7
Python decision making_loops part7Vishal Dutt
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdfKirubelWondwoson1
 
control statements
control statementscontrol statements
control statementsAzeem Sultan
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#Prasanna Kumar SM
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loopsManzoor ALam
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structuresayshasafdarwaada
 

Similar to 9 cm604.13 (20)

Control structures repetition
Control structures   repetitionControl structures   repetition
Control structures repetition
 
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KRLoops IN COMPUTER SCIENCE STANDARD 11 BY KR
Loops IN COMPUTER SCIENCE STANDARD 11 BY KR
 
C sharp chap4
C sharp chap4C sharp chap4
C sharp chap4
 
C language 2
C language 2C language 2
C language 2
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
 
Python decision making_loops part7
Python decision making_loops part7Python decision making_loops part7
Python decision making_loops part7
 
Chapter 3 - Flow of Control Part II.pdf
Chapter 3  - Flow of Control Part II.pdfChapter 3  - Flow of Control Part II.pdf
Chapter 3 - Flow of Control Part II.pdf
 
control statements
control statementscontrol statements
control statements
 
Decision making and loop in C#
Decision making and loop in C#Decision making and loop in C#
Decision making and loop in C#
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Iteration
IterationIteration
Iteration
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
Control structures in c
Control structures in cControl structures in c
Control structures in c
 
Loops c++
Loops c++Loops c++
Loops c++
 
Computer programming 2 Lesson 8
Computer programming 2  Lesson 8Computer programming 2  Lesson 8
Computer programming 2 Lesson 8
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 

More from myrajendra

Fundamentals
FundamentalsFundamentals
Fundamentalsmyrajendra
 
Data type
Data typeData type
Data typemyrajendra
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1myrajendra
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflowmyrajendra
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc driversmyrajendra
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc apimyrajendra
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1myrajendra
 
Dao example
Dao exampleDao example
Dao examplemyrajendra
 
Sessionex1
Sessionex1Sessionex1
Sessionex1myrajendra
 
Internal
InternalInternal
Internalmyrajendra
 
3. elements
3. elements3. elements
3. elementsmyrajendra
 
2. attributes
2. attributes2. attributes
2. attributesmyrajendra
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to htmlmyrajendra
 
Headings
HeadingsHeadings
Headingsmyrajendra
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbcmyrajendra
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

9 cm604.13

  • 1. Syntax for break and continue statements 1
  • 2. Objective • On completion of this lecture , you would be able to know • break and continue statements 2
  • 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
  • 18. Quiz 1.Continue statement can be used in control structure. [True/False] 18
  • 19. Quiz 1.Continue statement can be used in control structure. [True/False] 19
  • 20. Quiz 2.Break statement can be used in control structures. [True/false] 20
  • 21. Quiz 2.Break statement can be used in control structures. [True/false] 21
  • 22. Quiz 3.Break violates rules of structured programming [True/false] 22
  • 23. Quiz 3.Break violates rules of structured programming [True/false] 23
  • 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

  1. 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 &gt;= 95) { messageBox.show(&quot;You are an honor student&quot;); } else { } In this book, we use if-then statements whenever appropriate.
  2. Chapter 6 -
  3. Chapter 6 -