Create loop logic
Advantages of looping
Makes programming efficient
Write one set of instructions to operate multiple, separate sets of data
Less time required for designing and coding, less errors, shorter compile time
Loop: structure that repeats actions while some condition remains true
Use a loop control variable
Loop control variable: variable that determines whether loop execution will continue
As long as a condition remains true, the statement in a while loop's body will continue
executing
Control number of repetitions (3 important steps for functionality)
Loop control variable initialized before entering loop
Loop control variable tested
Variable is altered in body of loop so the tested condition that starts the loop will
eventually be false
Repetitions controlled by:
Counter - used to create a definite counter-controlled loop
Sentinel value - used to create indefinite loop (user determines loop execution)
Definite loop: executes a predetermined number of times
Indefinite loop: performed different number of times each time, user decides how many
times
Counter-controlled loop: Program counts repetitions
Loop control variables altered by
Incrementing ==> count = count + 1
Decrementing ==> count = count - 1
Can also add or subtract bigger values, like for every second number or year
Counter: any numeric variable that counts the number of times an event has occurred,
usually starts with 0
Why start counter with 0: arrays start counting at 0, application numbering start with 0,
contains number of times loop has executed which might be useful
Create nested loops
Needed when values of two or more variables repeat to produce every combination of values
Nested loops: loops within loops
Outer loop: loop that contains the other loop
Inner loop: the loop that is contained
Each loop has its own control variable