while
Description
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.
Syntax
If there is only one statement, the curly braces can be omitted.
Parameter Values
- condition: a boolean expression that evaluates to true or false.
Example Code
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:
See Also
- Language : break
- Language : continue
- Language : do...while
- Language : else
- Language : for
- Language : goto
- Language : if
- Language : return
- Language : switch...case
- Example : While Loop
※ ARDUINO BUY RECOMMENDATION
Arduino UNO R3 | |
Arduino Starter Kit |
Additionally, some links direct to products from our own brand, DIYables.