Presentation is loading. Please wait.

Presentation is loading. Please wait.

Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.

Similar presentations


Presentation on theme: "Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input."— Presentation transcript:

1 Loop

2 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input a number, Num Write num While Num != 0 Write “Done” –The body of the loop is executed repeatedly until the user enters a 0. At that point the loop is exited, and the statement that follows the loop is executed. –Note the indentation.

3 Pre-test and Post-test Loops  This is a Post-test loop, the condition occurs after the body of the loop is executed. Do Prompt for and input a number, Num Write num While Num != 0 Write “Done”  This is a pre-test loop, the condition appears a the top Input Num While Num != 0 Write Num Input Num End While

4 Trace (Walk through) a Loop It is impossible to see what a loop is doing without tracing it to see how it works. Suppose the user enters 3, 1, -1. Input Number While Number > 0 Write Number ^ 2 Input Number End While NumberOutput 3 9 1

5 Introduction to Loops Execute a block of statement(s) repetitiously as long as the condition is true (non-zero) a) counter loop b) conditional loop It requires 3 steps to design a loop: 1) Initial the condition before the loop 2) Design the condition (Boolean expression) for the loop 3) Design the body of the loop. 3a)The action you want to repeat 3b)Update the condition

6 3.2 Counter-controlled Loops The body of the loop is executed a fixed number of times (N times) where N is known prior to entering the loop for the first time. Prompt for an input the positive integer N Set Count = 1 While Count <= N Write Count, Count ^ 2 Set Count = Count + 1 End While Suppose the user enters 4. NCountOutput 41 1 1 22 4 33 9 44 16

7 Built-in Counter-controlled Loops Most programming languages contain a statement that makes is easy to construct a counter-controlled loop. For Counter = InitialValue Step Increment To LimitValue Body of loop End For

8 Examples: For Count = 1 Step 1 To 5 Write Count, Count ^ 2 End For For N = 1 Step 2 To 20 Write N End For CountOutput 11 1 22 4 33 9 44 16 55 25 NOutput135 …1719

9 3.3 Applications of Repetition Structures Sentinel controlled loops to input data. A loop will exit when a sentinel value is entered. A sentinel value is an end-of-data marker The value chosen for the sentinel should be a value that could not be mistaken for actual input data If the data entered is known to be positive integers, the programmer may ask the user to enter –1 when there is no more data, and the loop should exit

10 Sentinel controlled loop example Suppose that the input data for a program that computes employee salaries consists of the number of hours worked and the rate of pay Write “Enter the hours worked” Write “Enter –1 when you are done: Input Hours While hours != -1 Write “Enter the rate of pay.” Input Rate Set Salary = Hours * Rate Write Hours, Rate, Salary Write “Enter the hours worked.” Write “Enter –1 when you are done.” Input Hours End While Test Data 4010 3812 Trace (Walkthrough) Hours Rate Salary Output Enter the hours worked Enter –1 when you are done. 40Enter the rate of pay 10 400 40 10 400 Enter the hours worked Enter –1 when you are done. 38 Enter the rate of pay 12 456 38 12 456 Enter the hours worked Enter –1 when you are done.

11 Data Validation Users may enter erroneous data by mistake Programs should include statements that check, or validate that the value is in a proper range, and request the user to re-enter invalid data. Write “Enter a positive number ->” Input Num While Num ” Input Num End While … the program continues with a valid number in the variable Num

12 Demonstrate a counter loop Star t initialize the Num to 1 Num <= 10 Output Num and Num*Num Stop Increment Num by 1 No T 1)initial Num to 1 2)While Num is less or equal to 10 2a)Write Num and Num*Num 2b)increment Num by 1 End while Yes

13 C++ Coding using while loop // This program displays the numbers 1 through 10 and // their squares. #include using namespace std; int main() { int Num = 1; 1 cout << "Number Number Squared\n"; cout << "-------------------------\n"; while (Num <= 10) 2 { cout << Num << "\t\t" << (Num * Num) << endl; 3a Num++; 3b } return 0; }

14 C++ Coding using For loop // This program displays the numbers 1 through 10 and // their squares. #include using namespace std; int main() { int Num; cout << "Number Number Squared\n"; cout << "-------------------------\n"; for (Num = 1; Num <= 10; Num++) { cout << Num << "\t\t" << (Num * Num) << endl; } return 0; }

15 Demonstrate a counter loop Star t initialize the Num to 1 Num <= 10 Output Num and Num*Num Stop Increment Num by 1 T 1)initial Num to 1 2)Do 2a)Write Num and Num*Num 2b)increment Num by 1 While Num is less or equal to 10 Yes No

16 C++ Coding using D0 While loop // This program displays the numbers 1 through 10 and // their squares. #include using namespace std; int main() { int Num = 1; cout << "Number Number Squared\n"; cout << "-------------------------\n"; do { cout << Num << "\t\t" << (Num * Num) << endl; Num++; } while (Num <= 10) return 0; }

17 Program Output Number Number Squared ------------------------- 1 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100

18 In C++, there are feature that interrupt the flow of the loop break The break statement causes a loop to terminate early. Break out from the loop immediately continue The continue statement causes a loop to stop its current iteration and begin the next one.

19 // break.cpp #include using namespace std; int main() { int i; for(i = 1; i <= 10; i++) { if (i == 5) break; cout << i << endl; } return 0; } Output 1 2 3 4

20 // continue.cpp #include using namespace std; int main() { int i; for(i = 1; i <= 10; i++) { if (i == 5) continue; cout << i << endl; } return 0; } Output 1 2 3 4 6 7 8 9 10

21 3.4 Nested Loops A loop may be contained entirely within another loop. Outer loop. Inner loop. For OutCount = 1 Step 1 To 2 For InCount = 1 Step 1 to 3 Write “Outer:”, OutCount, “Inner:”, InCount End For (Incount) Write End For (OutCount) Trace the Nested Loop Outer:1 Inner:1 Outer:1 Inner:2 Outer:1 Inner:3 Outer:2 Inner:1 Outer:2 Inner:2 Outer:2 Inner:3

22 Nesting other Kinds of Loops Any style of loop may be nested. Each nested loop must be indented to indicate which statements are controlled by which looping construct. Do Other code here While More code here End While While

23 // nestloop.cpp #include using namespace std; int main() { int i,j; cout << "BEGIN\n"; for(i = 1; i <= 3; i++) { cout << " Outer loop: i = " << i << endl; for(j = 1; j <= 4; j++) cout << " Inner loop: j = " << j << endl; } cout << "END\n"; return 0; } BEGIN Outer loop: i = 1 Inner loop: j = 1 Inner loop: j = 2 Inner loop: j = 3 Inner loop: j = 4 Outer loop: i = 2 Inner loop: j = 1 Inner loop: j = 2 Inner loop: j = 3 Inner loop: j = 4 Outer loop: i = 3 Inner loop: j = 1 Inner loop: j = 2 Inner loop: j = 3 Inner loop: j = 4 END

24 Pseudocode Language (Ch 3) In this chapter we added relational operators and repetition. InputAssignment Input Variable Set Variable = 10 OutputArithmetic Operations Write “literal text”, Variable ( ) ^ * / + - Relational Operators = <> >= <= Repetition While conditionRepeat … End WhileUntil condition For Counter = InitialValue Step Increment To LimitValue End For Create a module Call a sub-module ModuleName Call ModuleName …. End


Download ppt "Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input."

Similar presentations


Ads by Google