ArcBasicDocs
Chapter 2

Variables and maths

Games have to remember things: your score, your lives, the player's name. A variable is a named box that holds a value, which you can look at and change whenever you like.

Three kinds of box

02_variables.absOpen in editor
; Variables: named boxes that hold valuesscore = 10          ; a whole numberspeed# = 2.5        ; a number with a decimal pointname$ = "Robin"     ; textPrint "Name: " + name$Print "Score: " + scorePrint "Speed: " + speedscore = score + 5Print "After a bonus, the score is " + scorePrint "Twice the speed is " + speed * 2WaitKeyEnd
The variables program printing a name, score and speed

The end of a variable's name says what kind of value it holds:

NameHoldsExamples
scorewhole numbers (integers)0, 42, -7
speed#numbers with a decimal point (floats)2.5, 0.1, -3.75
name$text (strings)"Robin", "Game over"

You only need the # or $ the first time you use a variable; after that, speed means speed#.

score = score + 5 looks odd if you think of = as "equals". Read it as "make score become score + 5": take what's in the box, add 5, and put the answer back.

+ also joins text together, and numbers are turned into text when you join them to a string. That's how "Score: " + score prints Score: 10.

Naming variables

Names can use letters, digits and _, but must start with a letter. Capitals don't matter: Score and score are the same box. A few words belong to the language itself, such as If, For, Each and End, so you can't use those as names. Choose names that say what the box is for: lives is much clearer than x.

Doing sums

SignMeansExampleAnswer
+add7 + 29
-take away7 - 25
*multiply7 * 214
/divide7 / 23 (whole numbers stay whole)
/divide7 / 2.03.5
Modthe remainder after dividing7 Mod 21
^to the power of2 ^ 38.0

When both numbers are whole, dividing gives a whole number and throws the remainder away. If you want the decimal part, make one of them a float.

Asking questions

Input prints a question, waits for the player to type an answer and press Enter, and gives back what they typed. Put the answer in a number variable and ArcBasic turns it into a number for you.

02_pizza.absOpen in editor
; The pizza calculator: ask some questions, then work out an answername$ = Input("What is your name? ")pizzas = Input("How many pizzas are there? ")people = Input("How many people are eating? ")slices = pizzas * 8share = slices / peoplespare = slices Mod peoplePrint ""Print "OK " + name$ + ", that's " + slices + " slices."Print "Everyone gets " + share + ", with " + spare + " left over."WaitKeyEnd
The pizza program asking questions and answering

What happens if you say nobody is eating? Dividing by zero is impossible, so the program stops with an error. In chapter 3 you'll learn how to check for things like that first.

Try this

  • Pizzas are sometimes cut into 6 slices. Change the program so it asks how many slices each pizza has.
  • Write a program that asks for your age and prints how old you'll be in 10 years.
  • Ask for a distance in kilometres and print it in miles. One kilometre is about 0.62 miles, so you'll want float variables.