A while loop will loop continuously, and infinitely, until the condition inside the parenthesis, () becomes false. Something must change the tested variable, or the while loop will never exit. This could be in your code, such as an incremented variable, or an external condition, such as testing a sensor.
If there is only one statement, the curly braces can be omitted.
- condition: a boolean expression that evaluates to true or false.
In the example above, the code in the loop will run, over and over again, as long as a variable (i) is less than 5.
The result on Serial Monitor:
※ NOTES AND WARNINGS:
- When you know exactly how many times you want to loop through a block of code, use the for loop instead of a while loop.
- The following while loop loops forever: