Science Lab Safety and Lab Equipment Quiz Review Quiz: Tuesday, September 20 th.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
-
Upload
phillip-hunt -
Category
Documents
-
view
223 -
download
2
Transcript of Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
PowerPoint Presentation
Project 1 Due Date: September 25th
Quiz 4 is due September 28thQuiz 5 is due October2th
1Control Structures II (Repetition)2Why Is Repetition Needed?Repetition allows efficient use of variablesCan input, add, and average multiple numbers using a limited number of variablesFor example, to add five numbers:Declare a variable for each number, input the numbers and add the variables togetherCreate a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers
3while Looping (Repetition) StructureSyntax of the while statement:
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
4while Looping (Repetition) Structure (contd.)
5while Looping (Repetition) Structure (contd.)
6The body of the while executes when the expression evaluates to trueThe expression checks whether a variable called loop control variable (LCV) satisfies certain conditionPrevious example i was LCVLCV should be initialized and eventually make the expression evaluate to false(We do this by updating LCV)
7Generally while loops are written in the following form:/*initialize loop control variable*/while (expression){ /*expression tests LCV*/
//update LCV
}
8while Looping (Repetition) Structure (contd.)
9Case 1: Counter-Controlled while LoopsWhen you know exactly how many times the statements need to be executed Use a counter-controlled while loop
10ExampleCh5_CounterControl.cpp11Case 2: Sentinel-Controlled while LoopsSentinel variable is tested in the condition Loop ends when sentinel is encountered
12ExampleCh5_SentinelControl.cpp13Case 3: Flag-Controlled while LoopsFlag-controlled while loop: uses a bool variable to control the loop
14ExampleCh5_FlagControlledLoop.cpp15Case 3: Flag-Controlled while LoopsFlag-controlled while loop: uses a bool variable to control the loop
16ExampleCh5_FlagControlledLoop.cpp17Case 4: EOF-Controlled while LoopsEnd-of-file (EOF)-controlled while loop: when it is difficult to select a sentinel value
18eof FunctionThe function eof can determine the end of file statuseof is a member of data type istreamSyntax for the function eof:
Ch5_EOFControlledLoop.cpp
19
for Looping (Repetition) Structurefor loop: called a counted or indexed for loopSyntax of the for statement:
The initial statement, loop condition, and update statement are called for loop control statements
20for Looping (Repetition) Structure (contd.)
21for Looping (Repetition) Structure (contd.)
22for Looping (Repetition) Structure (contd.)
23for Looping (Repetition) Structure (contd.)The following is a semantic error:
24The following is a legal (but infinite) for loop:for (;;) cout