0% found this document useful (0 votes)
7 views13 pages

Lesson 6-Repeating

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views13 pages

Lesson 6-Repeating

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Small Basic

Lesson 06: Repeating

Introduction to Programming in Small Basic


Repeating
LO: to understand how to use the FOR and WHILE loops in BBC Small Basic

Select the appropriate loop for solving a given problem.

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

The FOR loop is used to repeat a set of instructions a


given number of times. The syntax for the FOR loop
is:

For variable_name = start_value To end_value


Statement to execute
EndFor

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

A negative value in the step clause


allows us to initialise at a higher
number and count down to a lower
number.

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.

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).
The WHILE Loop

The WHILE loop repeats a set of


instructions until the condition
becomes false. The syntax for the
WHILE loop is:

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

• As long as the condition


(secret <> guess) is true, the
body of the WHILE loop is
executed.

• At least one statement should


be in the WHILE loop to make
the condition FALSE.
secret is not equal to guess

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

Select the appropriate loop for solving a given problem.

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

What is the difference between a FOR loop and a WHILE loop?

FOR Loop WHILE Loop


• You know exactly how many times you
• You don’t know how many times you
want to repeat the body before you
are going to repeat the statements.
start the loop.

• The computer keeps track of your • The programmer has to keep track of
position in the count. your position in the count.

• Called the determinate loop • Called the indeterminate loop

Select the appropriate loop for solving a given problem.

You might also like