String variable

A string variable is used
to store words.
Town$
The word string is used because a word is a string of
characters joined together.
String variable names have a $ at the end of them to
tell the computer and remind the programmer that it is to
be used to store words.
It is important to give each string variable a meaningful
name. This makes your program readable.
The string variable above is to be used to store the
name of a town. The programmer has called it town$.
At present, it has no value.
1
N4 TrueBasic Programming (V1)
Town$
How can we give town$ a value?
INPUT town$
LET town$ = “Edinburgh”
Edinburgh
London
The INPUT command stops
the program to allow the user
to enter a value for town$.
If the user types in LondonThe LET command allows
then the value of town$ the programmer to give a
value to a string.
becomes London.
This is only useful if the programmer
knows the value for the variable before
the program begins.
2
N4 TrueBasic Programming
(V1)
A numeric variable is
used to store numbers.
total_cost
It is important to give each numeric variable a meaningful
name. This makes your program readable.
The numeric variable above is to be used to store a
total cost. The programmer has called it total_cost. At
present, it has no value.
We can tell that it is not a string variable name because it
doesn’t have a $ sign at the end.
3
N4 TrueBasic Programming (V1)
total_cost
How can we give total_cost a value?
55
96
59
INPUT total_cost
The INPUT command stops the
program to allow the user to
LET total_cost = 96
enter a value for total_cost.
LET total_cost = first_cost + second_cost
If the user types in 55 thenThe LET command allows
the value of total_cost the programmer to give a
value to a numeric variable.
becomes 55.
This would add the values of first_cost
This is only
if the programmer
(24)useful
and second_cost
(35) and assign
knows the
value
thetovariable
before
the
totalfor
(59)
total_cost
.
the program begins.
4
N4 TrueBasic Programming
(V1)
A fixed loop makes the
computer repeat a list of
instructions a number of times.
The number of times the instructions have to
be repeated is known before the loop begins.
e.g. A 4 times loop or a 35 times loop.
This is why it is called a fixed loop.
TrueBasic uses FOR .... NEXT to set up a fixed loop.
5
N4 TrueBasic Programming
(V1)
FOR counter = 1 TO 6
PRINT “Hello”
NEXT counter
This would display the word Hello on the screen 6 times.
What is counter?
It begins with the value 1 and
increases by 1 each time it goes
round the loop until it reaches 7
then it jumps out of the loop.
It is a numeric variable.
counter
1
3
4
5
6
7
2
Let’s run through this step by step to see how it works.
6
N4 TrueBasic Programming
(V1)
FOR counter = 1 TO 6
PRINT “Hello”
NEXT counter
Hello
Hello
Hello
Hello
Hello
Hello
counter
The counter is assigned the value 1.
1
3
4
5
6
7
2
The word Hello is displayed.
NEXT counter means add 1 to the value of counter.
The FOR condition is checked to make sure that counter is
still within the range 1 to 6.
This continues and repeats until the counter reaches the
value 7 and then the loop stops.
7
N4 TrueBasic Programming
(V1)
A selection construct allows the computer
to make decisions based on conditions.
TrueBasic uses IF .... THEN .... END IF to set up a selection.
IF condition is true THEN
Execute instruction A.
END IF
Instruction A will only
be executed if the
condition is true.
Execute instruction B.
Every IF must
have an END IF.
8
Instruction B will always be
executed whether the
condition is true or not.
N4 TrueBasic Programming (V1)
IF score > 500 THEN
PRINT “First class
score”
END IF
Will “First class score”
be displayed?
Will “First class score”
be displayed now?
9
score
325
500
The condition here is
that the value of the
numeric variable
score must be
more than 500.
N4 TrueBasic Programming (V1)
A selection construct can also have an
ELSE to allow the computer to execute
different instructions if the condition is false.
IF condition is true THEN
Execute instruction A.
ELSE
Execute instruction B.
END IF
10
Instruction A will be
executed if the
condition is true.
Instruction B will be executed
if the condition is false.
N4 TrueBasic Programming (V1)
IF score > 500 THEN
score
PRINT “First class
325
550
score”
ELSE
PRINT “ Pretty Average”
END IF
PRINT “Try Again”
What
will bewe
displayed?
Pretty Average
Move
How could
make “Try
Try “Try
AgainAgain”
PRINT
again”
only
appear
for
the
What will be displayed now?
to First
beforeClass
the END
IF
pretty average scores?
score
Try Again
11
N4 TrueBasic Programming (V1)
A conditional loop makes the computer repeat
a list of instructions until a condition is true.
This is used when we don’t know the number of times the
instructions will have to be repeated.
e.g. A program which asks the user to enter their password
How many times will it take until they get it right?
TrueBasic uses DO.... LOOP UNTIL
to set up a conditional loop.
12
N4 TrueBasic Programming
(V1)
DO
PRINT “Please enter your password”
INPUT user_entry$
LOOP UNTIL user_entry$ = “topsecret”
This is the “condition”
How often will the two instructions in the loop be executed?
What is the minimum number of times that the instructions
will be executed?
The answer is 1.
This is because the condition is
checked at the end of the loop.
13
N4 TrueBasic Programming
(V1)
There is a second type of conditional loop which
checks the condition at the start of the loop.
This means that the instructions in the loop might
never need to be executed at all.
This is useful for input validation when we want to make
sure that a user enters suitable data for the program.
TrueBasic uses DO UNTIL .... LOOP for this conditional loop.
What was the other type of conditional loop?
DO.... LOOP UNTIL
14
Spot the difference?
N4 TrueBasic Programming
(V1)
PRINT “Please enter your password”
INPUT user_entry$
DO UNTIL user_entry$ = “topsecret”
PRINT “That is incorrect. Please try again”
INPUT user_entry$
What is the minimum
LOOP
number of times that
the instructions inside
PRINT “You have gained entry”
the loop will be
executed?
0 if the user gets the
password correct first time.
15
N4 TrueBasic Programming
(V1)