1. What can we do with the while statement?
While loops execute until the expression evaluates to false. They are useful for repeating some procedure over arrays, lists, etc…
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
True / 1
3. What is an infinite loop?
The is a loop where the condition never evaluates to false. Generally discouraged in some programming languages but for some internet protocols constant refresh is necessary.
4. What is an iteration?
It is a single trip through a loop from beginning to end.
1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
For loops are more compact and efficient and don’t have the problem of the iterator such as “count” potentially losing scope.
2. How would you write a for-loop in code?
A typical example…
for (int i=0: i <= 100; i++)
{
std::cout << i << ‘\n’;
… statements
}
3. What is an off-by-one error?
This is when a loop either executes one too many times or one two few. Usually caused by edge conditions in evaluator expressions involving ><=. Some languages such as Pascal will give you a range error if the compiler options is selected in Delphi compiler options. In C++ you will have to be more careful.