Presentation is loading. Please wait.

Presentation is loading. Please wait.

LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.

Similar presentations


Presentation on theme: "LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement."— Presentation transcript:

1 LESSON 5 Loop Control Structure

2 Loop Control Structure  Operation made over and over again.  Iterate statement

3 Repetition Statements 1. while loop 2. do-while loop 3. for loop

4 The while loop Syntax while (condition) { statement1; statement2; }

5 While loop flowchart statement true false condition evaluated

6 The while Loop Example #include using namespace std; int main() { int i=1; while (i<=3) { cout << i; i++; } getch(); return 0; } 1 ii<=3 1 2 1<=3 3 2<=3 4 3<=3 12123 4<=3

7 The do…while loop Syntax do { statement1; statement2; } while (condition);

8 do…while true condition evaluated statement false

9 do...while Loop Example int x=1; do{ cout<<x; x++; }while ( x<=3); 1 xx<=3 1 22<=3 12 33<=3 44<=3 123 true condition evaluated statement false

10 The for Statement  A for statement has the following syntax: for ( initialization ; condition ; increment ){ statement; } The initialization is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration

11 3. for Loop statement true condition evaluated false increment initialization

12 for Loop Example for (int x =1; x<=3; x++){ cout<<x; } 1 xx<=3 11<=3 22<=3 12 33<=3 statement true condition evaluated false increment initialization 44<=3 123

13 What is the output of the program? #include using namespace std; int main() { for(int i=1; i<=5; i++){ cout <<i ; } getch(); return 0; } 1 ii<=5 11<=5 22<=5 33<=5 44<=5 55<=5 66<=5 12123123412345

14 What is the output of the program? #include using namespace std; int main() { for(int i=1;i<=10;i+=2){ cout << i; } getch(); return 0; } 1 ii<=10 11<=10 33<=10 5 77<=10 99<=10 1111<=10 13135135713579

15 What is the output of the program? #include using namespace std; int main() { for(int i=1;i<=10;i+=3){ cout << i; } getch(); return 0; } 1 ii<=10 11<=10 44<=10 77<=10 1010<=10 1313<=10 1414714710

16 What is the output of the program? #include using namespace std; int main() { int i=1; int sum=0; do{ sum=sum+i; i++; }while (i<=5); cout <<“Sum =” <<sum; getch(); return 0; } Sum=15 ii<=5 1 22<=5 33<=5 44<=5 55<=5 66<=5 sum 0 1 3 6 10 15

17 Backup

18 What is the output of the code? int x=0; while (x<10) { cout<< x<<“ ”; x=x+1; }

19 What is the output of the code? int i; i=10; while (i>=1) { cout<< i<<“ ”; i--; }

20 What is the output of the program? #include using namespace std; int main() { int sum=0; int i=1; while (i<=10) { sum=sum+i; i+=2; } cout <<“Sum =” <<sum; getch(); retun 0; } Sum=25 ii<=10 1 3 1<=10 5 3<=10 7 5<=10 9 7<=10 11 9<=10 sum 0 1 4 9 16 2511<=10

21 What is the output of the program? #include using namespace std; int main() { int i=1; do{ i++; cout << “Oman \n”; }while (i<=5); getch(); return 0; } Oman ii<=5 1 22<=5 33<=5 44<=5 55<=5 66<=5 Oman


Download ppt "LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement."

Similar presentations


Ads by Google