Basics of for Loops
A for loop
executes a block of statements multiple times.
[Run the program]
A statement block is a group of statements
enclosed inside braces {}.
Braces are also called “curly brackets”.
A for loop iterates, or repeats,
the execution of the statement block.
In this example, the for loop
will iterate 7 times.
The sleepMs(600) statement
slows down the program's execution.
Exercises:
In the source code, the number 600 is
the argument of the sleepMs function.
Change the argument to number 150
to see how it affects the program's speed.
[Run the program]
Change the argument of
the sleepMs function to 900.
[Run the program]
This example shows the value of the loop control variable
i in each iteration.
[Run the program]
Important Note:
The statement sleepMs(500) pauses execution
for exactly one half of a second.
Exercise:
Modify the program so that it prints out 30 numbers.
You can bookmark your progress using the gray sharing buttons.
The sharing buttons create a link to the current page.
The loop control variable m
holds the current iteration number.
The program completes execution almost instantly
because it has no delays.
[Run the program]
Exercise:
Modify the println statement to:
println "m*3 = ", m*3;
In this example, the body of the for loop
contains three statements inside a statement block.
[Run the program]
The argument of the sleepMs function
is 500 milliseconds, which is half a second.
Exercise:
1. Inside the for loop body, add the statement:
println i;
In this example, the loop control variable is named k.
[Run the program]
The variable k is used
in the expression k*3.
A variable name is a primary expression.
Primary expressions can be combined to form more complex expressions.
Numbers, like 3, are also primary expressions.
In this example, the number 3 is a part of
the compound expression k*3.
Exercises:
1. Add the following statement at the end of the source code
(below the loop body):
println "Program ends";
[Run the program]
2. Insert the following statement inside
the for loop body:
sleepMs(200);
[Run the program]
This example prints out the first 10 multiples of number 3.
[Run the program]
A string is a text, a sequence of characters,
which may include letters, digits, spaces, punctuation,
and other kinds of characters.
A single quote is a character called “an apostrophe”.
A double quote character is more precisely called “a quotation mark”.
Strings can be enclosed in single quotes and
then they may contain double quotes.
An empty println statement creates an empty line by advancing to the next line.
Exercise:
1. Modify the program so that it prints
out the first ten multiples of number 7.
[Run the program]
2. Rename 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 calculates and prints the squares of the first 50 natural numbers.
The square of a number is the result of multiplying the number by itself.
Exercise:
Modify the program so that
it outputs the results of the expression 1000/number.
Remember to keep the arguments
in the println statement separated with commas
(,).
If you're stuck, you can peek at the solution.
Many different solutions are possible. Here's one example:
Solution
println 1000, "/", number, " = ", 1000/number;
In this example, the for loop
counts down from 50 to 1.
The initial value (50) of the loop is greater than the bounding value (1).
[Run the program]
Exercise:
1. Change the argument of the sleepMs function
from 55 to the expression
n*4 .
Turn the tablet to landscape to get a different arrangement.
In this example, the loop control variable
n increases by 3 in each iteration.
This is because the step of the for loop is set to 3.
[Run the program]
The step of the for loop is specified
after the symbols += .
If a loop is counting downwards, then the step should be specified
after the symbols -= .
Exercise:
1. Change the loop so that the variable
n starts from 50 and then decreases by 3
in each iteration.
This example uses some advanced features
of colors and println statements that will be explained later.
[Run the program]
Exercise:
1. A string is a sequence of characters.
Change the string “ZedLX” displayed on the screen to something else.
To share this program with friends, use the
Share button above the source code.
Then, enter a title for the program, and leave other
fields blank if you like.
Then, click “Create Link” to get a shareable link.
You can click the link to see the program in action.
Use the sharing buttons on the “Execution Completed” page to
share it with others.