A.
-
While statement makes us able to execute loops as long as the expression is evaluated to true, because every time it executes the control goes back to the top of the while statement and then it executes again, and it will only halt once the expression is already evaluated to false, and that’s the time the loop will exit.
-
The value needs to be true in order for the loop to continue.
-
It is a loop that keeps executing forever because the expression that is given always evaluates to true which is any non-zero value.
-
Each time a loop executes, it is called iteration.
B.
-
It is ideal to use a for loop when we know exactly how many times we need to iterate, because it lets us easily define, initialize, and change the value of loop variables after each iteration.
-
for (init-statement; condition-expression; end-expression)
statement
example:
for (int count{ 20 }; count >= 10; count -=3) std::cout << count << ' ';
// -> 20 17 14 11 -
Off-by-one errors occur when the loop iterates one too many or one too few times. This happens because of putting wrong relational operator in the conditional expression for example by using > instead of >=.