For example, you could make your program display a the value of a counter while the count is less than or equal to say, 10.
Example While statement:
Explanation of code
- We started by declaring a variable called myBankBalance and setting it to 0
- We then opened a while loop, inserting our condition between brackets. Our condition checks if the current value of the myBankBalance variable is less than or equal to 10.
- This is followed by code to execute while the condition is true. In this case, we are simply, outputting the current value of myBankBalance, preceded by some text. This code is placed within curly braces.
- We then increment the value by 1.
- When the browser reaches the closing curly brace, if the condition is still true, it goes back to the first curly brace and executes the code again. Of course, by now, the myBankBalance variable has been incremented by 1. If the condition is not true (i.e. the variable is greater than 10), it exits from the loop.