chapter 5 control structures ii repetition
play

Chapter 5: Control Structures II (Repetition) C++ Programming 1 - PowerPoint PPT Presentation

Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Chapter 5: Control Structures II (Repetition) C++ Programming 1 Dr. Adriana Badulescu Kallas Introduction to Computer Science &


  1. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Chapter 5: Control Structures II (Repetition) C++ Programming 1

  2. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Objectives Explore how to construct and use Learn about repetition count-controlled, Examine break and (looping) control sentinel-controlled, continue statements structures flag-controlled, and EOF-controlled repetition structures Discover how to form Learn how to avoid Learn how to debug and use nested control bugs by avoiding loops structures patches C++ Programming 2

  3. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Why Is Repetition Needed? • Repetition allows you to efficiently use variables • Can input, add, and average multiple numbers using a limited number of variables • For example, to add five numbers: – Declare a variable for each number, input the numbers and add the variables together – Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers C++ Programming 3

  4. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Read and Add Numbers � 2 Numbers START int Sum; INPUT N1 int N1; INPUT N2 cin >> N1; COMPUTE Sum=N1+N2; int N2; cin >> N2; OUPUT Sum Sum = N1 + N2; STOP cout << Sum; C++ Programming 4

  5. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Read and Add Numbers START � 3 Numbers INPUT N1 int Sum; int N1; INPUT N2 cin >> N1; INPUT N3 int N2; cin >> N2; COMPUTE Sum=N1+N2+N3; int N3; cin >> N3; OUPUT Sum Sum = N1 + N2 + N3; STOP cout << Sum; C++ Programming 5

  6. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Read and Add Numbers � 3 Numbers Sum so far � 3 Numbers int Sum=0; int Sum; int N1; int N1; cin >> N1; N1 cin >> N1; Sum = Sum + N1; int N2; int N2; cin >> N2; cin >> N2; N2 int N3; Sum = Sum + N2; cin >> N3; int N3; Sum = N1 + N2 + N3; cin >> N3; N3 cout << Sum; Sum = Sum + N3; cout << Sum; C++ Programming 6

  7. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Read and Add Numbers int Sum=0; int Sum=0; int N1; cin >> N1; N1 int N; REPEAT Sum = Sum + N1; cin >> N; 3 TIMES int N2; Sum = Sum + N; cin >> N2; N2 Sum = Sum + N2; cout << Sum; int N3; cin >> N3; N3 Sum = Sum + N3; cout << Sum; C++ Programming 7

  8. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Read and Add Numbers int Sum=0; int Sum=0; int N; int N; REPEAT REPEAT cin >> N; cin >> N; 3 1000 TIMES TIMES Sum = Sum + N; Sum = Sum + N; cout << Sum; cout << Sum; C++ Programming 8

  9. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I while Looping (Repetition) Structure • The general form of the while statement is: while ( Expression ) Statement ; while is a reserved word • Statement can be simple or compound • Expression acts as a decision maker and is usually a logical expression • Statement is called the body of the loop • The parentheses are part of the syntax C++ Programming 9

  10. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I while Looping (Repetition) Structure true false Expression while ( Expression ) Statement ; Statement • Infinite loop : continues to execute endlessly – Avoided by including statements in loop body that assure exit condition is eventually false C++ Programming 10

  11. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I while Looping (Repetition) Structure i=25; i=0; while (i<=20) while (i<=20) { { cout << i << “ ”; cout << i << “ ”; i=i+5; i=i+5; } } cout << “\n”; cout << “\n”; Sample run: Sample run: 0 5 10 15 20 0 times 5 times C++ Programming 11

  12. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I while Looping (Repetition) Structure i=0; i=0; while (i>=-20) while (i<=20) { { cout << i << “ ”; cout << i << “ ”; i=i+5; i=i+5; } } cout << “\n”; cout << “\n”; Sample run: Sample run: _ 0 5 10 15 20 INFINITE 5 times C++ Programming 12

  13. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I while Looping (Repetition) Structure i=0; i=0; while (i<=20); while (i<=20) { { cout << i << “ ”; cout << i << “ ”; i=i+5; i=i+5; } } cout << “\n”; cout << “\n”; Sample run: Sample run: _ 0 5 10 15 20 INFINITE times 5 times C++ Programming 13

  14. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I while Looping (Repetition) Structure i=0; i=0; while (i<20) while (i<=20) { { cout << i << “ ”; cout << i << “ ”; i=i+5; i=i+5; } } cout << “\n”; cout << “\n”; Sample run: Sample run: 0 5 10 15 20 Random number 5 times or INFINITE C++ Programming 14

  15. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I while Looping (Repetition) Structure i=0; i=0; while (i<20) while (i<=20) { { cout << i << “ ”; cout << i << “ ”; i=i+5; i=i+5; } } cout << “\n”; cout << “\n”; Sample run: Sample run: 0 0 0 0 0 0 0 0 0 …. 0 5 10 15 20 INFINITE 5 times C++ Programming 15

  16. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Case 1: Counter-Controlled while Loops • If you know exactly how many pieces of data need to be read //initialize the counter counter = 0; //test the counter while (counter < N) { //loop statements Statements; //update the counter counter++; } C++ Programming 16

  17. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Counter-Controlled while Loops counter = 0 counter = 0; true false while ( counter < N) counter < N { Statement; Statement counter++; } counter=counter +1 C++ Programming 17

  18. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Read and Add Numbers int Sum=0; int Sum=0; int counter=0; while (counter<5) { int N; int N; REPEAT REPEAT cin >> N; cin >> N; 5 5 TIMES TIMES Sum = Sum + N; Sum = Sum + N; counter++; cout << Sum; } cout << Sum; C++ Programming 18

  19. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Example: Sum of N Numbers C++ Programming 19

  20. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Case 2: Sentinel-Controlled while Loops • Sentinel variable is tested in the condition • Loop ends when sentinel is encountered //initialize the loop control variable cin >> variable; //test the loop control variable while (variable != SENTINEL) { //loop statements Statements; //update the loop control variable cin >> variable; } C++ Programming 20

  21. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Sentinel-Controlled while Loops INPUT variable cin >> variable; while ( variable!= true false variable!= variable!= SENTINEL) SENTINEL SENTINEL { Statement; Statement cin >> variable; } INPUT variable C++ Programming 21

  22. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Example: Sentinel-Controlled Sum C++ Programming 22

  23. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Example: Letter to Telephone Digits C++ Programming 23

  24. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Case 3: Flag-Controlled while Loops � A flag-controlled while loop uses a bool variable to control the loop //initialize the loop control variable found = false; //test the loop control variable while (!found) { //loop statements Statements; //update the loop control variable if (expression) found = true; } C++ Programming 24

  25. Dr. Adriana Badulescu Kallas Introduction to Computer Science & Programming Programming Fundamentals I Flag-Controlled while Loops found=false found=false; while (!found) true false { !found Statement; if (expression) Statement found=true; } true false expression found=true C++ Programming 25

Recommend


More recommend