A for loop has initialization, a continuation condition and an update expression. The condition is checked before each iteration. Here the accumulator begins at zero and adds each integer from one through five.
Read the example
The complete program appears below. Read it first, predict the output, then run it.
Expected output
Total: 15
Practise
Change the limit to 10. Then add only even numbers by increasing the counter by two.
Common mistake
An incorrect < or <= condition causes off-by-one errors. Keep counter bounds within the range of int.