FIRST PART
1. What can we do with the while statement?
With a While statement we can loop through a condition as long as itâs âTrueâ, is this condition is met the âbodyâ of the loop (statement) will be executed until the condition becomes âFalseâ.
2. What value needs the statement in the brackets evaluate to in order for the loop to continue?
âTrueâ.
3. What is an infinite loop?
An infinite loop is a loop that never terminates, unless theres some user input (Intentional infinite loop) or the program is forced to shut down.
4. What is an iteration?
Each time a loop executes, itâs called an iteration. If the loop executes N time, it has N iterations.
SECOND PART
1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
The for loop can be used when we have a obvious loop variable and we know how many times we need to iterate. If we donât know how many times we need the function to iterate, we should use the while loop.
2. How would you write a for-loop in code?
for(int counter=0; counter < 10; ++counter) {
cout << counter << endl;
}
3. What is an off-by-one error?
A off-by-one error happens when the loop executes one too many or one too few, and the loop doesnât give the desired result. This happens when the conditional expression has an error.