Figure 7-20 Problem specification, IPO chart information, and C++ instructions for the modified stockprice programThe program initializes thenumDayscounter variable to the number 1, which corresponds to the firstday. It also updates the variable by 1 (day) each time the loop instructions are processed. The initializingand updating of the counter variable in counter-controlled loops are comparable to the priming andupdate reads, respectively, in loops controlled by a sentinel value.Thewhile (numDays < 6)clause indicates that the loop instructions should be repeated as long as (orwhile) the number in thenumDayscounter variable is less than 6. The clause could also be written aswhile (numDays <= 5).In either case, the loop will stop when the numDays variable contains the number6, which occurs after the loop instructions are processed five times. Figure 7-21 shows the corresponding
flowchart, and Figure 7-22 shows a completed desk-check table using the following five stock prices:78.75, 80.05, 81.35, 79.95, and 80.10. Figure 7-22 also contains a sample run of the program.Figure 7-21 Flowchart for the modified stock price program
Figure 7-22 Completed desk-check table and sample run for the modified stock price programThe for StatementBesides using thewhilestatement to code pretest loops, you can also use the forstatement.However,the most common use of the for statement is to code counter-controlled pretest loops because itprovides a more compact and clearer way of writing that type of loop. As Figure 7-23 shows, thestatement’sforclause contains three arguments separated by two semicolons; the first and thirdarguments are optional.Figure 7-23 How to use the for statementIn mostforclauses, theinitializationargument creates and initializes a counter variable that thecomputer uses to keep track of the number of times the loop body instructions are processed. Thevariable is local to the for statement, which means it can be used only within the statement’s loop body.The variable will be removed from the computer’s internal memory when the loop ends.Theconditionargument in the for clause specifies the condition that must be true for the loop bodyinstructions to be processed. The condition must be a Boolean expression, which is an expression thatevaluates to either true or false. The expression can contain variables, constants, functions, arithmeticoperators, comparison operators, and logical operators. The loop stops when its condition evaluates tofalse. The for clause’supdateargument usually contains an expression that updates the counter variable
specified in theinitializationargument. Following theforclause is the loop body, which contains the oneor more statements that you want the computer to repeat. If the loop body contains more than onestatement, the statements must be entered as a statement block by enclosing them in a set of braces({}). However, you can also include the braces even when the loop body contains only one statement, asshown in Example 2 in Figure 7-23.
Upload your study docs or become a
Course Hero member to access this document
Upload your study docs or become a
Course Hero member to access this document
End of preview. Want to read all 33 pages?
Upload your study docs or become a
Course Hero member to access this document
Term
Fall
Professor
Marieta D. Cayabas
Tags
Control flow