5.4 Counting
Counting up with a while loop
- The programmer can use a variable to count the number of iterations, called a loop variable
- A common error is to forget to include the loop variable update (e.g., i = i +1) at the end of the loop, causing an unintended infinite loop.
Counting down with a while loop
- The loop body executes when i is 5, 4, 3, 2, and 1, but does not execute when i reaches 0.
Shorthand operators
- Covered in 2.5/6/7
- The item on the right can be an expression, so num *= x + y is shorthand for num = num * (x + y)