Variables

This assignment tests your understanding of variables, and their uses. Variables are probably the most important thing in programming. They store the data that your program is using, and without them, it would be very difficult (if not impossible) to write a useful program.

Variables in Just BASIC

All programming languages have variables, and lots of them are typed, which means that different types of data - e.g. text and numbers - are treated differently.

Just BASIC has two types of variable - numbers and strings (which store text). String variables have a $ at the end of their name, e.g. name$.

There aren't many restrictions on the names of variables - they can have any combination of letters and numbers, but they cannot start with a number or a character that isn't a letter, and they can't be the same as a basic command, e.g. PRINT. It does make sense for the variable name to suggest what it stores, e.g. name$ for a name or age for an age.

Assignment

When you first use a variable, its value is 0 (if it's a number) or blank (if it's a string). Giving a variable a value is know as assigning a value. It's very easy to do, and you can use a single value or the result of a calculation. All of the following are valid assignments:

age = 10
age = 8 + 2
name$ = "Joe"
fullname$ = "Joe " + "Bloggs"
fullname$ = forename$ + " " + surname$ 

You can also use the same variable as part of the assignment - this is useful when adding to, or taking away from, a value:

 age = age + 1
fullname$ = "Mr " + fullname$

The following assignments are NOT valid and will result in a type mismatch error because you're trying to mix strings and numbers:

age = "Hello"
name$ = name$ + 10

Related Links

If you want to read a lot more about variables and variable types, have a look at Variables at ICT in a Nutshell.

Questions

Reading programs:

1. What type of data would a variable called x hold? (1 mark)
 
2. What type of data would a variable called x$ hold? (1 mark)
 
3. What would be the output of the following program (i.e. what would it print on the screen)?
		message$ = "Hello World"
		print message$
(1 mark)
  Output:
4. What would be the output of the following program?
		a = 10
		b = 2
		print a + b
(1 mark)
  Output:
5. What would be the output of the following program?
		a$ = "10"
		b$ = "2"
		print a$ + b$
(1 mark)
  Output:
6. When this program has finished running, what will be the values of a and b?
		a = 10
		b = 5
		a = a + b
		b = b + 1
(2 marks)
 

Values: a: and b:

7. When this program has finished running, what will be the values of a and b?
		a = 10
		b = 5
		a = a + b
		b = a - b
		a = a - b
(3 mark)
 

Values: a: and b:

What has this program done?

Writing programs:

8. If you wanted to store a person's postcode in a Just BASIC program, what would be a suitable variable name? (1 mark)
  Variable name:
9. What would be the missing line of code (indicated by the question mark) in this program to work out the total cost of buying something by mail order?
		ItemCost = 25
		postage = 3
		?
		print "The total cost is £"; TotalCost
(2 marks)
  Missing line: