C++ Chapter 5 Repetition Structures
2 uses of break statement
1) exit early from a loop 2) Skip the remainder of a switch structure
How to choose which loop structure to use
1) if you know how many times the loop executes, use FOR 2) if you don't know how many times, but you know it executes at least once, use DO...WHILE 3) if you don't know how many times, use WHILE
3 looping structures in C++
1) while loop 2) do...while loop 3) for loop
infinite loop
a loop that continues to execute endlessly
while loop
a looping structure that continues as long as a particular condition is true
fibonacci number
a number in the Fibonacci sequence
nesting
a process that involves putting one control structure inside another
Difference between a while and a do...while loop
a while loop tests the condition first, then executes. A do...while loop executes, then checks the condition to see if it should execute again.
counter-controlled while loop
a while loop that is used when you know how many items of data are to be read; the loop will continue until the condition designated by the counter is met or evaluates to false
sentinel-controlled while loop
a while loop that uses a sentinel value to end the loop
EOF-controlled while loop
a while loop which will stop when it reaches the end of the file that it is reading
fibonacci sequence
an = an-1 + an-2
sentinel
an arbitrary value used to stop the execution of a loop
decision maker
an expression in an if statement or a loop which determines whether to execute the statement that follows it
flag variable
bool variable that controls a while loop. The loop continues until this value becomes "false"
Break statement
can be used in a switch or repetition structure and it provides immediate exit from that structure
Syntax of a do...while loop
do statement while (expression);
Syntax of for loop
for (initial; loop condition; update) statement;
for loop (indexed loop)
used to simplify the writing of counter-controlled loops; consists of an initialization statement, the loop condition, and the update statement
Syntax of while loop
while (expression) statement;
Flag-controlled while loop
while loop that is controlled by a bool variable. Will continue until the bool variable is "false"
End of File marker in Unix
Ctr + d
End of File marker in Windows
Ctrl + z
EOF stands for...
End of File
How to avoid an infinite loop
Make sure the loop's body contains statements that will eventually set the entry condition to "false"
Spot the error: while i >= 10 ------cout << i << endl;
Missing parentheses around the condition: (i >= 10)
What is the difference between a for loop and a counter-controlled while loop?
Nothing. They perform a certain action and increase a counter variable each time until the value reaches a certain point. However, for loops have a simplified syntax that make them easier and more efficient to use.
while (i >= 10); ------cout << i << endl;
Semicolon after while condition. Nullifies the loop
body of the loop
The series of statements that are executed if the loop condition evaluates to "true". Can be a single statement or a compound statement.
After a "continue" statement is executed in a for loop, what is the next statement executed?
The update condition
loop control variable (LCV)
Variable whose value determines whether the loop executes again or not. (controls the end of the loop)
When are loops used?
When it is necessary to repeat a set of statements several times.
Are fractional values allowed for loop control variables?
Yes, they are allowed, but not recommended. Different computers might produce different results.
posttest loop
a loop in which the loop condition is evaluated after executing the body of the loop
pretest loop
a loop in which the loop condition is evaluated before executing the body of the loop
When the "continue" statement is executed in a loop, what happens to the update statement?
in while and do...while loops, the update may not execute. in a for loop, the update always executes.
Software patch
piece of code written on top of an existing piece of code intended to superficially fix a bug in the original code.
Find the error: for (i = 1; i <= 10; i++); -----cout << i << " "; cout << endl;
semicolon after the for statement. Causes the action of the for statement to be empty.
divisor
suppose that m and n are integers and m is nonzero. Then m is called a _______ of n if n = mt for some integer t; that is, when m divides n, the remainder is zero
for loop control variable (indexed variable)
the variable initialized by the initial statement
Flag variable
the variable which is used to control the execution of the flag-controlled while loop
Continue statement
used in while, for, and do...while structures for the purpose of skipping the remaining statements in the loop and proceeding to the next iteration of the loop