Lesson 6-Repeating
Lesson 6-Repeating
Write a program that uses the FOR loop and step to change the increment.
Write at least two programs using loops (FOR loop and WHILE loop).
The FOR Loop
Write at least two programs using loops (FOR loop and WHILE loop).
FOR Loop to Print 1–20
'Printing 1 to 20
For i=1 To 20
TextWindow.WriteLine(i)
EndFor
Output
The variable i is initialised to 1, then
incremented by 1 each time
through the loop until i=20.
Write at least two programs using loops (FOR loop and WHILE loop).
The Step Modifier
'Printing 1 to 20
For i=1 To 20 Step 2
TextWindow.WriteLine(i)
EndFor
Output
The optional step 2 modifier
changes the default increment from
1 to 2.
Write a program that uses the FOR loop and step to change the increment.
Write at least two programs using loops (FOR loop and WHILE loop).
The Step Modifier – Negative Value
'Printing 10 to 1
For i=10 To 1 Step -1
TextWindow.WriteLine(i)
EndFor
Output
Write a program that uses the FOR loop and step to change the increment.
Write at least two programs using loops (FOR loop and WHILE loop).
Activity 5.1
• Write a program that uses the FOR loop to print all of the even numbers
from 10 to 20.
Example of the output
Write a program that uses the FOR loop and step to change the increment.
Write at least two programs using loops (FOR loop and WHILE loop).
Activity 5.2
• Write a program that uses the FOR loop to print all of the even numbers
from 20 to 10.
Write a program that uses the FOR loop and step to change the increment.
Write at least two programs using loops (FOR loop and WHILE loop).
The WHILE Loop
WHILE (condition)
Statement to execute
ENDWHILE
Write at least two programs using loops (FOR loop and WHILE loop).
Guess the Password
TextWindow.Title = "Password"
• The user is required to
secret = "twyford" guess a password that
TextWindow.Write("Guess the password --> ") was set to ‘twyford’.
guess = TextWindow.Read()
While secret <> guess
TextWindow.ForegroundColor = "Red"
TextWindow.Write("Wrong ... guess again --> ") • The WHILE loop is used
TextWindow.ForegroundColor = "Gray"
guess = TextWindow.Read()
to repeat the response
EndWhile and ask for a new
TextWindow.WriteLine("Welcome ...") password.
Write at least two programs using loops (FOR loop and WHILE loop).
Parts of the WHILE Loop
Write at least two programs using loops (FOR loop and WHILE loop).
Activity 6.3
• In the following program, the computer generates a random number between 1 and 100. The user is
required to guess this number. If the guess is greater than the number, then the program prints too high.
The program will print too low if the guess is lower than the number. Complete the code below.
Code to complete
Example of the output
Write a program that uses the FOR loop and step to change the increment.
Write at least two programs using loops (FOR loop and WHILE loop).
When to Use the FOR and WHILE loops
• The computer keeps track of your • The programmer has to keep track of
position in the count. your position in the count.