Loops Flashcards
In C, every loop has a _
controlling expression
If the expression is true
the loop continues to execute
Three iteration statements
while, do-while, for
repeatedly executes a target statement as long as a given condition is true
While loop
while loop syntax
Good!
These three things must be coordinated in order for the loop to work properly
initialized, tested, and updated
This is used in three ways
Loop Control Variable
In a while loop, if the condition is false, then
the loop will not execute
It is the post-test loop
do-while loop
This checks its condition at the bottom of the loop
do-while loop
This is guaranteed to execute at least one time
do-while loop
Do-while syntax
Wow!
While vs Do-While
while (first) - may execute 0 times
do-while (last) - must execute at least one time
similarities of while and do-while
one statement executed
initialization before loop
update during loop
A loop statements that is best for when you can determine in
advance how many times you need to execute the loop (counting loop).
For loop