loops Flashcards
Counter
A counter is a variable that is incremented or decremented each time a loop repeats
can be used to control execution of the loop (also known as the loop control variable
Must be initialized before entering loop
The do -while loop
do-while: a posttest loop - execute the loop, then test the expression
General Formal:
do
statement; // or block in { }
while (expression) ;
Note that a semicolon is required after
(expression)
notes on do-while loops
Loop always executes at least one
Execution continues as long as expression is true, stops repetition when expression becomes false
Useful in menu-driven programs to bring user back to menu to make another choice
The for Loop
A loop commonly must iterate a specific number of times, such as 10 times. Though achievable with a while loop, that situation is so common that a special kind of loop exists. A for loop is a loop with three parts at the top: a loop variable initialization, a loop expression, and a loop variable update. A for loop describes iterating a specific number of times more naturally than a while loop.
Useful for counter-controlled loop
General Format:
for (initialization; test; update)
statement; // or block in { }
No semicolon after the update expression or after the )
for Loop - mechanics
for(initialization; test; update)
statement; // or block in { }
1) Perform initialization
2) Evaluate test expression
-If true, execute statement
If false, terminate look execution
3) Execute update, then re-evaluate test expression
When to use the for Loop
In any situation that clearly requires
an initialization
a false condition to stop the loop
an update to occur at the end of each iteration
The for Loop is a Pretest Loop
The for loop tests its test expression before each iteration, so it is a pretest loop
Each time through a loop’s statements is called an
iteration.
A while loop is a program construct
that repeatedly executes a list of sub-statements (known as the loop body) while the loop’s expression evaluates to true. Each execution of the loop body is called an iteration. Once entering the loop body, execution continues to the body’s end, even if the expression would become false midway through.
What is the increment operator?
The statement i = i + 1 is so common that the language supports the shorthand ++i, with ++ known as the increment operator.
What is the decrement operator?
– is the decrement operator, –i means i = i - 1).
Actually two increment operators exist: ++i (pre-increment) and i++ (post-increment).
++i increments before evaluating to a value, while i++ increments after. Ex: If i is 5, outputting ++i outputs 6, while outputting i++ outputs 5 (and then i becomes 6). This material primarily uses ++i for simplicity and safety, although many programmers use i++, especially in for loops.
Choosing among for and while loops
Generally, a programmer uses a for loop when the number of iterations is known (like loop 5 times, or loop numItems times), and a while loop otherwise.
for Number of iterations is computable before the loop, like iterating N times.
while Number of iterations is not (easily) computable before the loop, like iterating until the input is ‘q’.