What if we want the
for loop to log
1, and then
0? With simple modifications to the expressions, we can make our loop run backward!
To run a backward
for loop, we must:
- Set the iterator variable to the highest desired value in the initialization expression.
- Set the stopping condition for when the iterator variable is less than the desired amount.
- The iterator should decrease in intervals after each iteration.
for loop that loops backwards printing
0 to the console. Use the
>= comparison operator in your stopping condition and the
-- operator in your iteration statement.