Iteration [4/10]
Iteration
A for loop executes statements of a statement block multiple times.
Press the F8 key to run the program, or click on the Run button.
A statement block consists of statements enclosed inside braces {}. Sometimes, they are also called “curly brackets”.
It can be said that a for loop repeats, or iterates, the execution of a following statement block.
In this example, the number of iterations is 7.
The statement sleepMs(600) slows down execution of the program.
Exercises:
1. In the source code of this program, the number 600 is
the argument of sleepMs function.
Change the number 600 to number 150.
[Run the program]
2. Change the argument of
the sleepMs function to value 900.
[Run the program]
This example prints on the screen the value of the loop control variable
i, in each iteration.
[Run the program]
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 statement sleepMs(500) pauses the execution for exactly one half of a second.
Exercise:
1. Modify the program so that it prints out 30 numbers.
Bookmark your progress by using the gray sharing buttons. The sharing buttons for Twitter, Facebook, Pinterest, Reddit and LinkedIn are provided. The sharing buttons create a link to the current page.
The loop control variable, named m in this example, holds the number of current iteration.
The program completes execution almost instantly,
because it contains no pauses.
[Run the program]
Exercise:
1. Change the println statement as follows: println "m*3 = ", m*3;
In this example, the body of the for loop
consists of three statements inside a statement block.
[Run the program]
The argument of sleepMs function equals 500 in this program; it specifies the sleep time in milliseconds. 500 milliseconds equals exactly one half of a second.
Exercise:
1. Inside the body of the for loop, insert a new statement: println i;
In this example, the loop control variable is named k.
[Run the program]
The variable k is also a part of the compound expression k*3.
Name of a variable is a primary expression. Primary expressions can be combined to form more complex expressions.
Numbers are also primary expressions. In this example, the number 3 is a primary expression, which is a part of the compound expression k*3.
Exercises:
1. Insert a statement
println "Program ends";
at the end of the source code (below the body of the loop).
[Run the program]
2. Insert a new line inside the body of the for loop. In the new line, write a statement sleepMs(200); [Run the program]
This example prints out first 10 multiples of number 3.
[Run the program]
A string is a text composed of characters. Kinds of characters include letters, digits, spaces, punctuation, and many others.
A single-quotes mark is actually a character called “an apostrophe”. A double-quotes character is more precisely called “a quotation mark”.
Strings are allowed to be enclosed inside single-quotes. Such strings are allowed to contain double-quotes.
Exercise:
1. Modify the program so that it prints
out first ten multiples of number 7.
[Run the program]
2. Change the name of the variable
i to z.
[Run the program]
Name of a variable can be composed of any letters, but it cannot contain spaces.
[Run the program]
A println statement scrolls the screen up when the screen is full.
This program prints out the squares of first 50 natural numbers. A square of a number is the number multiplied by itself.
Exercise:
(Challenging exercise!) Change the program so that it outputs results of expression 1000/number.
Remember that arguments of println statement must be separated by commas (,).
Peek at the solution if you can't solve this exercise.
Many differens solutions are possible. Here is one of them:
Solution
println 1000, "/", number, " = ", 1000/number;
In this example, the for loop counts backwards.
The initial value (50) of the loop is greater than the bounding value (1).
[Run the program]
Exercise:
1. Change the argument 55 of the sleepMs function into expression n*4 .
Turn the tablet to landscape to get a different arrangement.
In this example, value of the loop
control variable n is increased by 3 in each iteration.
In other words, the step of the for loop equals 3.
[Run the program]
The step of the for loop is specified after the symbols += .
If a loop is counting downwards, then the step can be specified after the symbols -= .
Exercise:
1. Change the loop so that the variable n starts from value 50 and then decreases by 3 in each iteration.
Solution
for #n= 50 .. 1 -=3
In this example, some advanced features of colors
and println statements will remain unexplained.
[Run the program]
Exercise:
1. Change the string “ZedLX” displayed on the screen. A string is a text composed of characters.
* * *
You can send this program to your friends by using the Share button above the source code.
You should provide your desired title of the program. You can leave other fields blank, if you like.
Then, click on the “Create Link” button to create a program link. Click on the link to see your program in action. To show your program to others, you should use the sharing buttons on the “Execution completed” page.
<< F2:Prev - - F4:Next >>