Topic 1 CS1A Review P4 Repetition Chapter 6 in the shrinkwrap Repetition Structures What if I want some instructions to run over and over again Topic 1 CS1A Review Repetition 2 of 27 1 Repetition Structures Repetition When a set of instructions need to be executed more than 1 time Run a select set instructions repeatedly until some condition is false Conditions again are based on a Boolean Expression The computer evaluates a Boolean Expression and executes the code until that condition is FALSE Topic 1 CS1A Review Repetition 3 of 27 The Loop Control Variable LCV The LCV is what controls when our loop will execute and when it will exit FOR ANY LOOP WE MUST 1 Initialize the LCV 2 Compare or check the LCV in some conditional statement 3 Change the LCV WARNING You must change the LCV or else your loop will run forever That is called an infinite loop Topic 1 CS1A Review Repetition 4 of 27 2 3 Basic Repetition Structures For Loop Part of a program is executed a given number of times While Loop Part of a program is executed while some condition is true While some condition is true execute these instructions Do While Loop Part of a program is executed at least one time and then repeats until some condition is false Now we will move onto the While Loop Topic 1 CS1A Review Repetition 9 of 27 While Loop What if we don t know how many times we need to run our loop We use the while loop the LCV is modified dynamically within the loop The LCV needs to be initialized before entering the loop The condition is tested at the top of the loop making it a pre test loop if the condition evaluates to TRUE the loop is entered if the condition evaluates to FALSE the loop is bypassed The LCV MUST be updated within the loop typically just before the end of the loop otherwise you will run into an infinite loop situation Topic 1 CS1A Review Repetition 10 of 27 3 Flowchart for While Loop Note on the flow chart the arrow goes above the loop1 WHILE condition F 2 If the condition is TRUE it executes the loop body and returns to step 1 If the condition is FALSE it exits the loop and does not execute the loop body it moves on to the statement immediately following the loop T loop body Out of loop statement s Topic 1 CS1A Review Repetition First it evaluates the condition The loop body will continue to be executed as long as the condition is true What will happen if we don t change the LCV in here 11 of 27 Coding a While Loop Event controlled loop a loop that terminates based on a condition and a sentinel value this loop executes an unspecified number of times Does a while loop always have to execute Syntax initialize the LCV while condition loop statements change the LCV Topic 1 CS1A Review Repetition 12 of 27 4 1 Initialize The Loop Control Variable Must be initializced While loop Example cout Enter an integer type 0 to exit cin num 2 Check while num 0 The LCV is compared w the sentinel value cout num 12 endl cout Enter another integer type 0 to exit cin num cout Out of loop endl What does this do WARNING You must change the LCV so that you don t have an infinite loop Topic 1 CS1A Review Repetition Example 2 Example While loop Write a program that will accept a set of integers from a user sum the set of integers count how many integers were input It will stop accepting integers when a 0 is reached Output the total number of integers given as input the sum First let s draw the flowchart INPUT num Sentinel Value WHILE num 0 F T OUTPUT num 12 INPUT num 3 CHANGE the LCV in the loop OUTPUT out of loop 13 of 27 BEGIN sum 0 countInt 0 1 Initialize INPUT num WHILE num 0 2 Check F T sum sum num countInt countInt 1 3 Change INPUT num OUTPUT countInt OUTPUT sum END 14 of 27 5 Example 2 Code BEGIN sum 0 include iostream using namespace std countInt 0 INPUT num WHILE num 0 int main declaration section with data table F int num int sum T sum sum num int countInts countInt countInt 1 INPUT num IN integer value CALC OUT running total of integers CALC OUT total number of inputs INITIALIZATION SECTION Irunning total sum counter countInts sum 0 countInts 0 OUTPUT countInt OUTPUT sum End 15 of 27 cout Enter an integer type 0 to exit cin num INITIALIZE the LCV by reading the 1st Input BEGIN sum 0 PROCESSING This loop calculates running total of integers input counts the of inputs while num 0 CHECK the LCV against the SENTINEL VALUE 0 countInt 0 INPUT num WHILE num 0 PROCESSING Add integer num to the running total sum before reading a new value Increment the counter sum sum num countInts countInts 1 F j T sum sum num countInt countInt 1 cout Enter another integer type 0 to exit cin num CHANGE the LCV by reading the next Input INPUT num OUTPUT countInt What would we OUTPUT sum have to change to calc the avg END OUTPUT counter countInts and running total sum cout n nThe total number of integers was cout countInts cout nThe sum of the integers is sum endl 16 of 27 6 int num1 num2 num3 num1 2 num1 num2 num3 num2 num1 2 num3 num1 num2 while num1 15 cout num1 num2 num3 endl if num1 6 LAB EXERCISE Output num1 num1 num2 What will the output be for this code segment else num1 num1 num3 num2 num2 num3 cout num3 num2 num1 endl cout Out of loop 17 of 27 When would we use them As a counter Count the of inputs Running totals Sum a of inputs When you don t know how many times you need to loop Example Sum a number of integers Output the sum and the total number of integers given as input Topic 1 CS1A Review Repetition 18 of 27 7 3 Basic Repetition Structures For Loop Part of a program is executed a given number of times While Loop Part of a program is executed while some condition is true While some condition is true execute these instructions Do While Loop Part of a program is executed at least one time and then repeats until some condition is false Now we will move onto the Do While Loop Topic 1 CS1A Review Repetition 19 of 27 Do While Loop The LCV can be initialized in the loop The loop statements are executed BEFORE the condition is tested The condition is tested at the bottom of the loop making it a post test loop if the condition evaluates to TRUE the loop statements are executed again if the condition evaluates to FALSE the loop exits the statements are run at least 1X The LCV MUST be updated within the loop typically at …
View Full Document
Unlocking...