Let's take a common use case where we need to execute code while the player is alive, and then debug when that's no longer the case:
- Create an initializer variable, called playerLives, of the int type, and set it to 3.
- Declare a while loop with the condition checking whether playerLives is greater than 0 (that is, the player is still alive).
- Inside the while loop, debug something to let us know the character is still kicking, then decrement playerLives by 1 using the -- operator.
Increasing and decreasing a value by 1 is called incrementing and decrementing, respectively (-- will decrease a value by 1, and ++ will increase it by 1).
- Add a debug log after the while loop curly brackets to print something when our lives run out:
With playerLives starting out at 3, the while loop will execute three times. During each loop, the debug log, "Still alive!", fires, and a life is subtracted...