# 1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops.

## Presentation on theme: "1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops."— Presentation transcript:

1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops

2 Two Types of Loops 1. Count controlled loops Repeat a statement or block a specified number of times 2. Event-controlled loops Repeat a statement or block until a condition within the loop body changes that causes the repetition to stop

3 While Statement SYNTAX while (Expression) {.. // loop body. } Loop body can be a single statement, a null statement, or a block

4 When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the loop body WHILE LOOP FALSE TRUE body statement Expression

5 Count-controlled loops contain n An initialization of the loop control variable n An expression to test if the proper number of repetitions has been completed n An update of the loop control variable to be executed with each iteration of the body 1. Count-Controlled Loops

6 Count-controlled Loop Example int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count

7 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 4

8 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT count 4

9 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 count 4

10 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 count 3

11 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 count 3

12 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 count 3

13 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 count 2

14 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 count 2

15 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 2 count 2

16 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 2 count 1

17 Count-controlled Loop int count; count = 4; while(count > 0) TRUE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 2 count 1

18 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 2 1 count 1

19 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 2 1 count 0

20 Count-controlled Loop int count; count = 4; while(count > 0) FALSE { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 2 1 count 0

21 Count-controlled Loop int count; count = 4; while(count > 0) { cout << count << endl; count --; } cout << “Done” << endl; OUTPUT 4 3 2 1 Done count 0

22 2. Types of Event-Controlled Loops 1. Sentinel controlled Keep processing data until a special value that is not a possible data value is entered to indicate that processing should stop 2. End-of-file controlled Keep processing data as long as there is more data in the file 3. Flag controlled Keep processing data until the value of a flag changes in the loop body 22

23 A Sentinel-controlled Loop l Requires a “priming read” l A priming read is the reading of one set of data before the loop to initialize the variables in the expression

24 // Sentinel controlled loop total = 0; cout << “Enter a blood pressure(-1 to stop) ”; cin >> thisBP; // Priming read while(thisBP != -1)// While not sentinel { total = total + thisBP; cout << “Enter a blood pressure(-1 to stop)”; cin >> thisBP; } cout << total;

25 End-of-File Controlled Loop l Uses the fact that a file goes into the fail state when you try to read a data value beyond the end of the file to control the loop

26 total = 0; myInfile >> thisBP; // Priming read while(myInfile) // While last read successful { total = total + thisBP; myInfile >> thisBP; // Read another } cout << total; // End-of-file controlled loop

27 int count; char previous; char current; count = 0; inFile.get(previous);// Priming reads inFile.get(current); while(inFile) { if((current == ‘=‘) && (previous == ‘!’)) count++; previous = current; // Update inFile.get(current);// Read another }

28 initialize outer loop while (outer loop condition) {... initialize inner loop while(inner loop condition) { inner loop processing and update }... } Nested Loops 28

29 Designing Nested Loops l Analyze the algorithm. l Begin with outer loop l Functional Decomposition  When you get to where the inner loop appears, make it a separate module and we can come back to its design later

30 Do-While Loop When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the Do-while statement Statement Expression DO WHILE FALSE TRUE

31 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response has been input // && response == ‘y’ or ‘n’ { do { cin >> response; // Skips leading whitespace if ((response != ‘y’) && (response != ‘n’)) cout << “Please type y or n : “; } while ((response != ‘y’) && (response != ‘n’)); } Example of Do-While 31

32 Do-While Loop vs. While Loop l POST-TEST loop (exit-condition) l The looping condition is tested after executing the loop body l Loop body is always executed at least once l PRE-TEST loop (entry-condition) l The looping condition is tested before executing the loop body l Loop body may not be executed at all

33 For Loop SYNTAX for (initialization; test expression; update) { Zero or more statements to repeat }

34 Example of Repetition num int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT ?

35 Example of Repetition int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; num OUTPUT 1

36 Example of Repetition num OUTPUT 1 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; true

37 Example of Repetition num int num; for (num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 1 1Potato

38 Example of Repetition num OUTPUT 2 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato

39 Example of Repetition num OUTPUT 2 true 1Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

40 Example of Repetition num int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 2 1Potato 2Potato

41 Example of Repetition num OUTPUT 3 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato 2Potato

42 Example of Repetition num OUTPUT 3 true 1Potato 2Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

43 Example of Repetition num int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; OUTPUT 3 1Potato 2Potato 3Potato

44 Example of Repetition num OUTPUT 4 int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl; 1Potato 2Potato 3Potato

45 Example of Repetition num OUTPUT 4 false 1Potato 2Potato 3Potato int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

46 Example of Repetition num When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement 4 false int num; for(num = 1; num <= 3; num++) cout << num << “Potato” << endl;

47 Example of Repetition num int counter; for (counter= 0; counter< 5; counter++) cout << “HELLO”; cout << “counter”<< counter; OUTPUT ?

Download ppt "1 What is a loop? A loop is a repetition control structure that causes a single statement or block to be executed repeatedly Loops."

Similar presentations