while Loop and Keyboard
A while loop repeats the following block of statements while the given condition is true.
This program prints the square of each natural number i while the square is smaller than 300.
The condition of the while loop is checked only at the beginning of each iteration.
This program increases the variable i until the Esc key is pressed.
* * *
Note for touchscreen users only: you can try changing the condition of the while loop into not isPressed("touch").
This is a variation of the previous program. It clears the screen by calling the clearScr function on each iteration of the while loop.
This program also displays an informative message and draws a disc at the horizontal position i.
The result is a simple animation.
This program changes the color of the disc when an appropriate key is pressed.
* * *
Note for touchscreen users only:
isPressed("touch1") (one-finger touch)
isPressed("touch2") (two-finger touch)
This program moves the disc when the left and right arrow keys are pressed.
This program displays the name of the last key pressed.
This program can be used to find out the names of keyboard keys.
This program also displays whether a key is pressed or not.
This program uses an infinite loop. It will never end by itself.
The 'Exit' button will immediately end such programs.
Run the program, and then use the 'Exit' button to terminate the program.
Note that the final statement will never be executed, therefore it can be deleted from the source code.
This program uses a do-while loop. The condition of a do-while loop is verified only at the end of each iteration. Therefore, a do-while loop is guaranteed to perform at least one iteration.
A do-while loop repeats a sequence of statements. A statement block is not required.
A do-while loop statement is a plain statement, because it must always end with a semicolon (;) symbol when it is not a part of a statement list. In a statement list, a do-while statement can end with a colon (:).