SlideShare une entreprise Scribd logo
1  sur  11
Télécharger pour lire hors ligne
Lecture05(Control Structure PART-II)
June 27, 2010

       Today's Outline
           Looping statements in C
            • for loop
            • while loop
            • do-while loop
           Jump Statements in C
            • continue
            • break
            • goto


  Md. Mahbub Alam        Structured Programming Language (CSE-   1
                                          1121)
for loop
General Format:
     for( initialization; condition; increment/decrement )
     {
               statements; //body of the loop
      }


                                           Init




                                                         false
                                        Condition


                                                  true


                                       Body of loop



                                         Inc / Dec




Md. Mahbub Alam       Structured Programming Language (CSE-      2
                                       1121)
for loop (Cont.)
Example:

            int sum = 0, count;
            for(count = 1; count <= 5; count++)
                sum = sum + count; //body of the loop
            printf("%d", sum);




Md. Mahbub Alam         Structured Programming Language (CSE-   3
                                         1121)
while loop
General Format:
       while( condition )
       {                                                       Condition
                                                                             false



           statements; //body of the loop
                                                                     true
        }
                                                              Body of loop



      int sum = 0, count;
      count = 1;
      while( count <= 5)
      {
          sum = sum + count;
          count++;
      }
      printf("%d", sum);

 Md. Mahbub Alam      Structured Programming Language (CSE-                  4
                                       1121)
do while loop
Format:
    do
    {
                                                              Body of loop
       statements; //body of the loop
    }while( condition );
                                                                             true
                                                               Condition

   int sum = 0, count;
                                                                     false
   count = 1;
   do
   {
       sum = sum + count;
       count ++;
   }while( count <= 5);
   printf("%d", sum);

 Md. Mahbub Alam      Structured Programming Language (CSE-                         5
                                       1121)
loop (cont.)
• Loop
   – Group of instructions computer executes repeatedly while some
     condition remains true
• Counter-controlled repetition
   – Definite repetition:
   – Known in advance how many times loop will execute
   – Control variable used to count repetitions
• Sentinel-controlled repetition
   – Indefinite repetition
   – Used when number of repetitions not known in advance
   – Sentinel value indicates "end of data"



 Md. Mahbub Alam      Structured Programming Language (CSE-          6
                                       1121)
continue
Skips the remaining statements in the body of a while, for or do…while
statement and proceeds with the next iteration of the loop
 – while and do…while
     • Loop-continuation test is evaluated immediately after the continue
       statement is executed
 – for
     • Increment/ decrement expression is executed first, then the loop-
       continuation test is evaluated
                    int i, sum = 0;
                    for(i=0; ;i++)
                    {
                        if(i%2==1)
                             continue;
                        sum += i;
                    }
  Md. Mahbub Alam        Structured Programming Language (CSE-      7
                                          1121)
break
• Causes immediate exit from a while, for, do…while or switch
  statement.
• Program execution continues with the first statement after the control
  structure.
• Common uses of the break statement
        - Escape early from a loop
        - Skip the remainder of a switch statement

            int i, sum=0;
            for(i=1; ; i++)
            {
               sum += i;
               if(sum >= 10)
                   break;
            }
    Md. Mahbub Alam           Structured Programming Language (CSE-   8
                                               1121)
goto Statement
Format:
        goto label;
                                   main()
         .........
                                   {
         .........
                                      double x,y;
        label:
                                      read:
          statements;
                                      scanf("%lf",&x);
                                      if(x<0)
        label:                          goto read;
         statements;                  y=sqrt(x);
          .........                   printf("%lf",y);
          .........                }
        goto label;


 Md. Mahbub Alam        Structured Programming Language (CSE-   9
                                         1121)
Any Question?




Md. Mahbub Alam   Structured Programming Language (CSE-   10
                                   1121)
Thank You All




Md. Mahbub Alam    Structured Programming Language (CSE-   11
                                    1121)

Contenu connexe

Tendances

Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
bsdeol28
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
ErumShammim
 

Tendances (19)

5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
Presentation on nesting of loops
Presentation on nesting of loopsPresentation on nesting of loops
Presentation on nesting of loops
 
Loops in c
Loops in cLoops in c
Loops in c
 
Loops in c
Loops in cLoops in c
Loops in c
 
Loops in C Programming Language
Loops in C Programming LanguageLoops in C Programming Language
Loops in C Programming Language
 
C Language - Switch and For Loop
C Language - Switch and For LoopC Language - Switch and For Loop
C Language - Switch and For Loop
 
Looping Statement And Flow Chart
 Looping Statement And Flow Chart Looping Statement And Flow Chart
Looping Statement And Flow Chart
 
Statements and Conditions in PHP
Statements and Conditions in PHPStatements and Conditions in PHP
Statements and Conditions in PHP
 
Loops in c programming
Loops in c programmingLoops in c programming
Loops in c programming
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Looping
LoopingLooping
Looping
 
Chapter 03 conditional statements
Chapter 03   conditional statementsChapter 03   conditional statements
Chapter 03 conditional statements
 
Control statements
Control statementsControl statements
Control statements
 
Iteration
IterationIteration
Iteration
 
Nested loop in C language
Nested loop in C languageNested loop in C language
Nested loop in C language
 
Functuon
FunctuonFunctuon
Functuon
 
Different loops in C
Different loops in CDifferent loops in C
Different loops in C
 
Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2Web Application Development using PHP Chapter 2
Web Application Development using PHP Chapter 2
 

En vedette

Trip attraction rate
Trip attraction rateTrip attraction rate
Trip attraction rate
S. M. Rahat Rahman
 

En vedette (20)

09-Runway Configuration ( Highway and Airport Engineering Dr. Sherif El-Badawy )
09-Runway Configuration ( Highway and Airport Engineering Dr. Sherif El-Badawy )09-Runway Configuration ( Highway and Airport Engineering Dr. Sherif El-Badawy )
09-Runway Configuration ( Highway and Airport Engineering Dr. Sherif El-Badawy )
 
Traffic Concepts (Transportation Engineering)
Traffic Concepts (Transportation Engineering)Traffic Concepts (Transportation Engineering)
Traffic Concepts (Transportation Engineering)
 
Trip attraction rate
Trip attraction rateTrip attraction rate
Trip attraction rate
 
Lecture 5: Railway Engineering ( هندسة السكك الحديدية ) Dr. Alaa Gabr
Lecture 5:  Railway Engineering ( هندسة السكك الحديدية ) Dr. Alaa GabrLecture 5:  Railway Engineering ( هندسة السكك الحديدية ) Dr. Alaa Gabr
Lecture 5: Railway Engineering ( هندسة السكك الحديدية ) Dr. Alaa Gabr
 
Route Choice (Transportation Engineering)
Route Choice (Transportation Engineering)Route Choice (Transportation Engineering)
Route Choice (Transportation Engineering)
 
Signalized Intersections (Transportation Engineering)
Signalized Intersections (Transportation Engineering)Signalized Intersections (Transportation Engineering)
Signalized Intersections (Transportation Engineering)
 
Speed report from panthpoath to russel square by pronob ghosh buet 1204011
Speed report from panthpoath to russel square by pronob ghosh buet 1204011Speed report from panthpoath to russel square by pronob ghosh buet 1204011
Speed report from panthpoath to russel square by pronob ghosh buet 1204011
 
Vehicle Dynamics (Transportation Engineering)
Vehicle Dynamics (Transportation Engineering)Vehicle Dynamics (Transportation Engineering)
Vehicle Dynamics (Transportation Engineering)
 
Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)Lecture02(constants, variable & data types)
Lecture02(constants, variable & data types)
 
Traffic Detection Systems (Transportation Engineering)
Traffic Detection Systems (Transportation Engineering)Traffic Detection Systems (Transportation Engineering)
Traffic Detection Systems (Transportation Engineering)
 
Lecture03(c expressions & operators)
Lecture03(c expressions & operators)Lecture03(c expressions & operators)
Lecture03(c expressions & operators)
 
Lec 15 Transit Operations (Transportation Engineering Dr.Lina Shbeeb)
Lec 15 Transit Operations (Transportation Engineering Dr.Lina Shbeeb)Lec 15 Transit Operations (Transportation Engineering Dr.Lina Shbeeb)
Lec 15 Transit Operations (Transportation Engineering Dr.Lina Shbeeb)
 
03-Properties of Asphalt Traditional ( Highway and Airport Engineering Dr. Sh...
03-Properties of Asphalt Traditional ( Highway and Airport Engineering Dr. Sh...03-Properties of Asphalt Traditional ( Highway and Airport Engineering Dr. Sh...
03-Properties of Asphalt Traditional ( Highway and Airport Engineering Dr. Sh...
 
Lecture 04 Capacity for TWSC (Traffic Engineering هندسة المرور & Dr. Usama Sh...
Lecture 04 Capacity for TWSC (Traffic Engineering هندسة المرور & Dr. Usama Sh...Lecture 04 Capacity for TWSC (Traffic Engineering هندسة المرور & Dr. Usama Sh...
Lecture 04 Capacity for TWSC (Traffic Engineering هندسة المرور & Dr. Usama Sh...
 
Intelligent Transportation Systems (Transportation Engineering)
Intelligent Transportation Systems (Transportation Engineering)Intelligent Transportation Systems (Transportation Engineering)
Intelligent Transportation Systems (Transportation Engineering)
 
Freeway & Highway LOS (Transportation Engineering)
Freeway & Highway LOS (Transportation Engineering)Freeway & Highway LOS (Transportation Engineering)
Freeway & Highway LOS (Transportation Engineering)
 
Geometric Design (Transportation Engineering)
Geometric Design (Transportation Engineering)Geometric Design (Transportation Engineering)
Geometric Design (Transportation Engineering)
 
Soil Mechanics ميكانيكا التربة & Eng. Ahmed S. Al-Agha
Soil Mechanics ميكانيكا التربة & Eng. Ahmed S. Al-AghaSoil Mechanics ميكانيكا التربة & Eng. Ahmed S. Al-Agha
Soil Mechanics ميكانيكا التربة & Eng. Ahmed S. Al-Agha
 
Parking design in front of CE building BUET by Pronob ghosh 1204011
Parking design in front of CE building BUET by Pronob ghosh 1204011Parking design in front of CE building BUET by Pronob ghosh 1204011
Parking design in front of CE building BUET by Pronob ghosh 1204011
 
10-Intersection Control ( Transportation and Traffic Engineering Dr. Sheriff ...
10-Intersection Control ( Transportation and Traffic Engineering Dr. Sheriff ...10-Intersection Control ( Transportation and Traffic Engineering Dr. Sheriff ...
10-Intersection Control ( Transportation and Traffic Engineering Dr. Sheriff ...
 

Similaire à Lecture05(control structure part ii)

Repetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docxRepetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docx
sodhi3
 

Similaire à Lecture05(control structure part ii) (20)

Loops in c
Loops in cLoops in c
Loops in c
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Lecture04(control structure part i)
Lecture04(control structure part i)Lecture04(control structure part i)
Lecture04(control structure part i)
 
loops and iteration.docx
loops and iteration.docxloops and iteration.docx
loops and iteration.docx
 
L8
L8L8
L8
 
Lo43
Lo43Lo43
Lo43
 
09
0909
09
 
Session 3
Session 3Session 3
Session 3
 
bai giang java co ban - java cơ bản - bai 2
bai giang java co ban - java cơ bản - bai 2bai giang java co ban - java cơ bản - bai 2
bai giang java co ban - java cơ bản - bai 2
 
06 Loops
06 Loops06 Loops
06 Loops
 
Looping
LoopingLooping
Looping
 
Repetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docxRepetition Control and IO ErrorsPlease note that the mate.docx
Repetition Control and IO ErrorsPlease note that the mate.docx
 
Loops c++
Loops c++Loops c++
Loops c++
 
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
 
Lab 6
Lab 6Lab 6
Lab 6
 
12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Core java
Core javaCore java
Core java
 
Loops in c++
Loops in c++Loops in c++
Loops in c++
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes2nd year computer science chapter 12 notes
2nd year computer science chapter 12 notes
 

Dernier

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Dernier (20)

Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 

Lecture05(control structure part ii)

  • 1. Lecture05(Control Structure PART-II) June 27, 2010 Today's Outline Looping statements in C • for loop • while loop • do-while loop Jump Statements in C • continue • break • goto Md. Mahbub Alam Structured Programming Language (CSE- 1 1121)
  • 2. for loop General Format: for( initialization; condition; increment/decrement ) { statements; //body of the loop } Init false Condition true Body of loop Inc / Dec Md. Mahbub Alam Structured Programming Language (CSE- 2 1121)
  • 3. for loop (Cont.) Example: int sum = 0, count; for(count = 1; count <= 5; count++) sum = sum + count; //body of the loop printf("%d", sum); Md. Mahbub Alam Structured Programming Language (CSE- 3 1121)
  • 4. while loop General Format: while( condition ) { Condition false statements; //body of the loop true } Body of loop int sum = 0, count; count = 1; while( count <= 5) { sum = sum + count; count++; } printf("%d", sum); Md. Mahbub Alam Structured Programming Language (CSE- 4 1121)
  • 5. do while loop Format: do { Body of loop statements; //body of the loop }while( condition ); true Condition int sum = 0, count; false count = 1; do { sum = sum + count; count ++; }while( count <= 5); printf("%d", sum); Md. Mahbub Alam Structured Programming Language (CSE- 5 1121)
  • 6. loop (cont.) • Loop – Group of instructions computer executes repeatedly while some condition remains true • Counter-controlled repetition – Definite repetition: – Known in advance how many times loop will execute – Control variable used to count repetitions • Sentinel-controlled repetition – Indefinite repetition – Used when number of repetitions not known in advance – Sentinel value indicates "end of data" Md. Mahbub Alam Structured Programming Language (CSE- 6 1121)
  • 7. continue Skips the remaining statements in the body of a while, for or do…while statement and proceeds with the next iteration of the loop – while and do…while • Loop-continuation test is evaluated immediately after the continue statement is executed – for • Increment/ decrement expression is executed first, then the loop- continuation test is evaluated int i, sum = 0; for(i=0; ;i++) { if(i%2==1) continue; sum += i; } Md. Mahbub Alam Structured Programming Language (CSE- 7 1121)
  • 8. break • Causes immediate exit from a while, for, do…while or switch statement. • Program execution continues with the first statement after the control structure. • Common uses of the break statement - Escape early from a loop - Skip the remainder of a switch statement int i, sum=0; for(i=1; ; i++) { sum += i; if(sum >= 10) break; } Md. Mahbub Alam Structured Programming Language (CSE- 8 1121)
  • 9. goto Statement Format: goto label; main() ......... { ......... double x,y; label: read: statements; scanf("%lf",&x); if(x<0) label: goto read; statements; y=sqrt(x); ......... printf("%lf",y); ......... } goto label; Md. Mahbub Alam Structured Programming Language (CSE- 9 1121)
  • 10. Any Question? Md. Mahbub Alam Structured Programming Language (CSE- 10 1121)
  • 11. Thank You All Md. Mahbub Alam Structured Programming Language (CSE- 11 1121)