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.
After running the program, modify it so that it also outputs
the value of variable i on each iteration.
After that, modify the program so that it outputs the first 123 squares
of natural numbers. Since the program needs to output a long list,
make the program run faster. To verify that the list is correct,
check that the last number in the list ends with digits 129.
This program increases the variable i
until the Esc key is pressed.
Run the program, and then use the Escape key to end it.
Change the println statement in the loop into
a following statement:
println "i: ", i.atmost(13);
Then, try to figure out what is the purpose of the getter
atmost.
Then, try changing the inserted statement to use the getter
atleast instead of atmost.
* * *
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.
After running the program, modify it so that it prints out the square
of variable i on the end of program execution.
This program draws
a disc at the horizontal position i.
The result is a simple animation.
After running and studying the program, change the
third argument (the literal 100) of the disc
function call to some other value and observe how it affects the output.
Try values 20 and 200.
Exercise 1
Modify the program so that the disc increases in size while moving.
After that, change the second argument (i.e. literal 200)
to some other value and observe how it affects the output.
Try values 600 and 300.
Exercise 2
Modify the program so that the disc moves vertically,
in a direction from the top to the bottom.
This program changes the color of the disc when an
appropriate key is pressed.
* * *
Note for touchscreen users only:
try with:
isPressed("touch1") (one-finger touch)
and
isPressed("touch2") (two-finger touch)
.
This program moves a disc when the left or right arrow key is pressed.
Movement is explained in more detail in chapter on animations.
Exercise 1
Try insering an if statement
in order to prevent the x coordinate getting smaller than 50.
This will prevent the disc from moving outside the left side of screen.
Solution
Insert this statement before the disc function call:
if x<50
: x=50;
Exercise 2
Reload the original program.
Try using the getter atleast
to prevent the x coordinate getting smaller than 50.
Hint
The getter atleast cannot change the
value of the variable x by itself.
Using a statement of assignment is the only way to change the value of a variable.
Solution
Insert this statement before the disc function call:
x = x.atleast(15);
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.
Run the program and test it. Then, use the Escape key to end it.
After executing the program, modify it by using an
if
statement so that the program displays a
text "A key is pressed" while any key is pressed.
Solution
Insert this statement in the middle of the loop:
if isPressed(lastKeyName())
: println "A key is Pressed";
The condition of the while loop
is always true, therefore the loop never ends.
That makes an infinite loop.
It cannot ever end, so the program keeps executing forever.
The 'Exit' button can end such programs immediately.
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.