C++ Chapter 5
What is the order of program control in a while loop?
1. Test the expression 2. If the expression has a non-zero (true) value A. Execute the statement following the parentheses B. Go back to Step 1 else exit the while statement and execute the next executable statement following the while statement
Null statement format
;
Nested loop
A loop inside the body of another loop.
Post-test loop
A loop that evaluates a condition at the end of the repeating section of code
Pre-test loop
A loop that evaluates a condition before any statements in the loop are executed
What do accumulators do?
A variable is given the job of holding the "total" of several values.
Describe the flow of control using a loop within a loop.
An outer loop does not finish until the inner loop is finished for each value of the outer loop
What are the basic constructs necessary for loops of repetition?
Expression, input, output, something to initialize, something to end it
Explain how you know which type of loop to use in your program (for, while, or do...while loops)?
For is usually used in a nested loop as an inner loop. While is a pre-test loop. Do-while is a post test loop
Break statement
Forces an immediate break, or exit, from the switch, while, for, and do-while statements
Infinte loop
One loop that energy ends; program just keeps displaying numbers until you realized it isn't working as you expected
Repetition
Repeating a sequence of instructions a certain number of times, or until some specific result is achieved
Fixed-count loop
The condition is used to keep track of how many repetitions have occurred; A counter is used to control the number of loop iterations
How and why are counters used?
The counter should stop at the user defined total
Variable-condition loop
The tested condition doesn't depend on a count being reached, but on a variable that can change interactively with each pass through the loop
Loop
The transfer of control back to the start of a while statement to reevaluate the expression
Why is repetition needed in programming?
Used when a program needs to repeatedly process one or more instructions until some condition is met, at which time the loop ends.
Which type of loop (that we studied) can be nested within loops?
While loops, for loops, do..while loops
While statement
a general repetition statement that can be used in a variety of programming situations
Format of a break statement
break;
Continue statement format
continue;
do-while statement formate
do { text += "The number is " + i; i++; } while (i < 10);
for statement format
for (initializing list; expression; altering lost) statement;
do-while must execute ________
one time (atleast once)
Sentinels values must be ______________________
selected so as not to conflict with legitimate data values
While statement format
while (expression) statement;
Sentinel
- Data values used to signal the start or end of data series - A special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop
Compare fixed repetition and variable condition loops.
- Fixed count loops - repeat a predefine number of times. - - Variable count loops - repeat an unspecified number of times.
for statement
- Function: statement executed while expression has non-zero (true) value - Performs the same functions as the while statements but uses a different form. The for statements format is easier to use than the while statement equivalent
Components of a for statement
- Initializing list: initial value of expression - Expression: a valid C++ expression - Altering list: statements executed at end of each for loop to alter value of expression **are optional BUT semicolons must always be present
Altering list
- Provides the increment value that is added or subtracted from the counter in each iteration of the loop - The last item inside for statement parentheses. All are executed by the for statement at the end of the loop
Interactive for Loops
- Same effect as using can object within a while loop - Provides interactive user input
Continue statement
- The next ideation of the loop begins immediately - Can be included in a switch statement
do-while statement
- The statement used to code a post-test loop in C++ - A loop statement that executes a statement or statements once, then repeats the execution as long as a given conditional expression evaluates to a truthy value.
Null statement
- All statements must be terminated by a semicolon - A do-nothing statement used where a statement is required syntactically, but no action is called for