Code Repetition
Programs often need to do the same thing many times. Say you want to print a countdown from 5 to 1:
While Loops
A while loop repeats a block of code as long as a condition is true. Use it when you don't know in advance how many times something should repeat, for example:
Counters
A counter is a variable, usually an int, that tracks how many times something happens. Each time the event occurs, you increment (increase) or decrement (decrease) it.
Accumulators
An accumulator is a numeric variable (e.g. int) that builds up a result, usually a total.
For Loops
A for loop repeats a block of code a set number of times. Use it when you know how many times to repeat before the loop starts, for example:
Breaking Loops
The break the statement allows you to terminate a loop prematurely (e.g. while, do while, and for loops)
Practice (While)
1. Spot the Problems
Practice (For)
1. Sum of Evens