Loops Flashcards
What are the 3 major structures of flow of control?
Sequences, choice, and repetition
What is the while loop?
keep executing the code statements over and over, as long as the boolean expression evaluates true
What is the condition?
The boolean expression in a loop that determines whether a loop should continue or exit
What is start/initialization code?
initialize variables that you will use to keep track of when to stop your loop
What is upkeep code?
changes the variables used in the condition so that the loop stops at some point
What are for loops?
loops that let you write count-controlled loops in a condensed form
What is the formula for a for loop?
for (initialization; expression; upkeep) {body}
What is the order of execution for a for loop?
- initialization: done only once
- condition: checked before each loop through
- upkeep: done at the end of each loop through
- body: executed each time through the loop, if the condition is true
- statements after loop: will be executed as soon as condition is false
What kind of variables can you use as the counter in your for loops?
Don’t use floating-point data types, as they are not exact.
What is the scope of the iterator variable in for loops?
The for loop and nothing else. That means you can use the same variable name in multiple loops that are not nested
What is a do-while loop?
a “bottom-tested” while loop; runs code once before checking your condition
How can you choose which loop to use?
for loop: if you know exactly how many times the loop should iterate (don’t over use)
while loop: if you need the possibility of the loop not happening
do-while loop: if you need the loop to be done at least once
What are keywords that you can use to control the flow of a loop?
break: immediately terminates the loop it’s in
continue: immediately terminates the current iteration of the loop it’s in