1 / 22

Chapter 9 Repetition Structure (Loop)

Chapter 9 Repetition Structure (Loop). Prepared by: Lec . Ghader Kurdi. Introduction. A Repetition structure represents certain parts of the code that will be executed repeatedly, or not at all based on the current state (condition ). In C++, there are 3 forms of implementing repetition:

kyoko
Télécharger la présentation

Chapter 9 Repetition Structure (Loop)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 9Repetition Structure(Loop) Prepared by: Lec. GhaderKurdi

  2. Introduction • A Repetition structure represents certain parts of the code that will be executed repeatedly, or not at all based on the current state (condition). • In C++, there are 3 forms of implementing repetition: • While Loop While a loop is a group of instructions that the computer executes repeatedly until a terminating condition is satisfied. • Do-while Loop • For Loop

  3. While Loop

  4. While Loop (cont.) • Initialization of the control variable which will be used to test the condition is applied only once. • If the conditionis true, the statement and the increment are executed, then the whole thing is done again. • The statement and the increment are executed repeatedly until the conditionbecomes false. • If the condition starts out false, the while-body will never be executed. • Increment is the statement that makes change on the condition, otherwise, the loop will continue running (Infinite loop).

  5. Example void main() { intcount = 1; while (count <=2) { cout << "Welcome to C++!"; count++; } } Outputs:

  6. Example void main() { int count = 0; while (count < 2) { cout << "Welcome to C++!"; } } Outputs :

  7. Example void main() { int count = 0; while (count < 2) { cout << "Welcome to C++!"; count++; } } Outputs:

  8. Example void main() { intcount = 2; while (count < 2) { cout << "Welcome to C++!"; } } Outputs:

  9. Do-While Loop do { …any number of statements… // action } while (condition) ;

  10. Do-While Loop (cont.) • The do-while statement performs the test after executing the body. • As long as the test is true, the body of the program will be executed again. • In all cases, the body will be executed at least once, even if the condition was wrong because testing happened at the end.

  11. Example void main() { int count = 1; do { cout << "Welcome to C++!"; count++; } while (count <= 2); } Outputs:

  12. Example void main() { int count = 1; do { cout << "Welcome to C++!"; count++; } while (count > 2); } Outputs:

  13. Example #include <iostream.h> void main () { do { cout<< "\nYou can't stop me!"; }while (1); // infinite do-while } Outputs:

  14. Example #include <iostream.h> void main () { do { cout<< "You can't stop me!\n"; }while (0); // infinite do-while } Outputs:

  15. Do-While Loop (cont.) The difference between while and do-while • The testing happens before executing the body in the while structure. • So if the condition is false from the beginning, the body will not be executed at all.

  16. For Loop for ( initialize; condition; increment ) { statements ; }

  17. For Loop • For structure is composed of 4 parts: Initialize: initializes the loop control variable, and done only once. Condition: that is the condition that determines whether the loop should continue executing or not. Statements: the body of the loop. The increment: is a statement that changes the value of the initialization either by adding, subtracting, or any math operation. But usually it is either adding or subtracting. • It should be noticed that there is no semicolon after the increment. • This "for structure" structure is better to be used when the number of iterations is known.

  18. Example void main() { inti; for (i = 0; i < 2; i++) { cout << "Welcome to C++!"; } } Outputs:

  19. Example #include <iostream> intmain () { for (int n=10; n>0; n--) { cout<< n << ", "; } cout<< «Done!\n"; } Outputs:

  20. Example #include <iostream> intmain () { intn = 10; while (n>0) { cout<< n << ", "; --n; } cout<< "Done!\n"; } Outputs:

  21. Exercises • Request the user to type positive numbers until either a zero or a negative is typed, and then show the user how many positives were typed in. • Write a program that prints the cubes of the numbers from 1 to 10. Here’s the output from the program:   1     1           2     8   3     27   4     64   5     125   6     216   7     343   8     512   9     729   10   1000

  22. For loops can always be re-written as while loops, and vice-versa. Are the following two programs equivalent, and what is their output? Explain your answer, and run the programs to check. Program (a) Program (b) #include <iostream> intmain() { intcount = 1; while (count <= 5) { cout<< count << "\n"; count++; } return 0; } #include <iostream> intmain() { intcount ; for (count = 0; count < 5; count++) { cout<< count << "\n"; } return 0; }

More Related