Iteration
A for loop
executes statements of a statement block multiple times.
Run the given program by clicking on the
Run button, or by pressing the
F8 key. [F8:Run]
A statement block consists of statements
enclosed inside braces {} (i.e. curly brackets).
It can be said that a for loop repeats, or iterates,
the execution of the following statement block.
In this example, the number of iterations is 7.
The statement sleepMs(600)
slows down execution of the program.
After running the program, try changing the number 600 (called: the argument of
the sleepMs function) to values 200 and 1800
to see how it affects program execution.
The loop control variable, named
i in this example,
holds the number of the current iteration.
[F8:Run]
The example prints out the value of the loop control
variable i, in each iteration.
A for statement should not be ended by a semicolon
because a for statement is not a plain statement.
A for statement is a block-ended statement.
The function sleepMs
makes the program pause execution for a short amount of time.
The argument of the sleepMs function
equals 500 in this program; it specifies
the sleep time in milliseconds.
[F8:Run]
sleepMs(500) pauses the execution
for a half of a second.
After running the program, try changing the name of variable
i to some other name.
In this example, the loop control variable is named
k.
[F8:Run]
Name of a variable is a primary expression.
Primary expressions can be combined to form more complex expressions.
In this example, the variable k is a part of
compound expression k*3.
Numbers are also primary expressions. In this example, the number 3
is a primary expression, which is a part of compound expression k*3.
After running and examining the program,
try adding a statement
sleepMs(200);
as the last statement of the loop
(i.e. just below the last println statement).
Then try out the program.
After that, add a statement
println "Program ends";
at the end of the source code (i.e. below the loop's statement block, not inside it).
This example prints out the first 10 multiples of number 3.
[F8:Run]
Modify the program so that it prints
out the first ten multiples of number 7.
Bookmark your progress by using the grey sharing buttons. The sharing buttons for
Google Plus, Twitter, Facebook, Pinterest, Reddit and LinkedIn are provided.
The sharing buttons create a link to the current page.
The name of a variable can be composed of any letters.
[F8:Run]
The println statement scrolls the screen up when
the screen is full.
This program prints out the squares of the first 50 natural numbers. A square of
a number is the number multiplied by itself.
After executing the program, try changing the name of the variable
number to yz.
Check that the source code is correct and then run the program.
After that, try changing the program so that
it outputs the value of the expression 1000/yz.
Remember that the arguments of
println statement must be separated by commas (,).
In this example, the for loop counts backwards.
The initial value (50) of the loop is greater than the bounding value (1).
Note: it is recommended to always run the given programs as you begin to read the instructions.
[F8:Run]
After executing the program, try changing the argument 55 of the
sleepMs function into the expression
n*4 and observe the effects.
Turn the tablet to landscape to get a different arrangement.
In this example, the value of the loop
control variable n is increased by 3 in each iteration.
In other words, the step of this for loop equals 3.
[F8:Run]
The step of the for loop is specified
after the symbols +=.
Exercise:
Change the loop so that the variable
n starts from value 50 and then decreases by 3
in each iteration.