- What can we do with the while statement?
A while statement will execute code repeatedly as long as a given condition is met.
- What value needs the statement in the brackets evaluate to in order for the loop to continue?
The value needs to verfiy the statement in order to loop another time. If the value is not verifying the statement (false) anymore, the programm will exit the while loop.
- What is an infinite loop?
An infinite loop will never invalidate the statement an therefore go on forever.
- What is an iteration?
An iteration is a repetition.
1. When is it a good idea to use a for statement (do a for loop) instead of the while loop we learned previously?
When we are f. e. dealing with an index of an array, means we already have a fixed number of iterations. The while loop will continue until a certain condition is met. For loop will repeat For a certain amount of iterations. It actually says is in the name. lol
2. How would you write a for-loop in code?
for(
int i=0; /setting counter to 0/
i<100; /setting condition, as long as counter is smaller than 100 repeat/
i++; /at the end of each loop increase counter by 1/
3. What is an off-by-one error?
off-by-one error refers to differences in indexing arrays or vectors. In C languages indexing starts with 0. In other languages indexing might start with 1. So if you put in your “condition-to-meet” in a loop, make sure to consider the indexing rules. if you for example have an array with 5 entries, your condition last entry will be indexed 4 in C! If not considered correctly the loop might iterate on time too much, or too less.