Skip to content

Step 1 of 4 · Reading · ~4 min

Learn

Basics

Variables and Math

By the end of this lesson you can store numbers and text in typed variables, pick the right arithmetic operator for the job, and print a number without the stray space that PRINT puts in front of it.

Declaring a variable

A variable is a named box in memory. DIM creates one and AS says what type of value the box holds. The type matters because FreeBASIC is compiled: it decides how much memory the box needs and which operations make sense on it.

DIM crates AS INTEGER       ' whole numbers
DIM weight AS DOUBLE        ' numbers with a fractional part
DIM shipName AS STRING      ' text

crates = 23
weight = 4.75
shipName = "Aurora"

LONGINT is a 64-bit whole number for results too big for INTEGER. A new number starts at 0 and a new string starts empty. You can give a starting value in the same line: DIM perCrate AS INTEGER = 4.

A variable must be declared before it is used. That is protection, not bureaucracy:

DIM total AS INTEGER
total = 10
totl = total + 5     ' COMPILE ERROR: variable not declared

Old BASICs silently created a brand-new variable called totl worth 0 and carried on. Old listings also use suffixes such as count% in place of AS; recognise them, but do not write them, because the dialect used here rejects them.

Reading numbers

INPUT "", x reads one value per line into x. The empty "" is the prompt; without it a ? is printed and spoils your output. The type of x decides how the line is read, so an INTEGER variable reads -5 as a number.

Arithmetic

Using crates = 23 and perCrate = 4:

23 + 4       ' 27
23 - 4       ' 19
23 * 4       ' 92
23 / 4       ' 5.75   / always gives a fraction, even between two INTEGERs
23 \ 4       ' 5      backslash: whole-number quotient, the remainder dropped
23 MOD 4     ' 3      the remainder
2 ^ 10       ' 1024   exponent

Two facts decide most arithmetic bugs.

First, \ and MOD belong together: (a \ b) * b + (a MOD b) always gives back a. With a negative number the quotient is cut toward zero, as in C, and MOD takes the sign of the left operand: -23 \ 4 is -5 and -23 MOD 4 is -3. Python's // would give -6 here, so do not carry its habits over.

Second, putting a fraction into an INTEGER rounds to the nearest whole number; it does not drop the fraction:

DIM n AS INTEGER = 7 / 2     ' n is 4, not 3

An INTEGER also has a limit (at least about 2 billion). Going past it does not raise an error, the value silently wraps around, so use LONGINT when a result might be large.

The leading space in front of every number

This rule costs learners more attempts than any other, so read it twice. When PRINT writes a number, it first writes one column for the sign. A negative number puts its - there. A positive number leaves it blank:

DIM load AS INTEGER = 9
PRINT load             ' writes " 9"   (a space, then 9)
PRINT -5               ' writes "-5"
PRINT "load: "; load   ' writes "load:  9"   (two spaces)
PRINT STR(load)        ' writes "9"
PRINT "load: " & load  ' writes "load: 9"

The grader does not trim leading spaces, so " 9" is not "9". STR() turns a number into text with no sign column, and the & operator joins text and converts numbers the same way. When a number is part of the output the grader checks, print STR(...) of it or build the line with &.

Strings

& glues text together:

PRINT "Ship: " & shipName & ", crew " & 12    ' Ship: Aurora, crew 12
PRINT UCASE(shipName)                         ' AURORA
PRINT "Name length: " & LEN(shipName)         ' Name length: 6

Common mistakes

  • Writing / when a whole-number answer is wanted. 20 / 6 is 3.33333..., and an INTEGER variable that receives it holds a rounded value, not a truncated one. The whole-number quotient is 20 \ 6.
  • Printing a bare number. The arithmetic is right, the screen looks right, and the test fails over one invisible space.
  • Expecting floor division on negative numbers. -7 \ 2 is -3, not -4, and -7 MOD 2 is -1, not 1.

Your exercise

Read two integers and print five labelled results about them: their sum, difference and product, and the whole-number quotient and remainder of the first by the second. The starter reads the numbers, prints all five lines through STR(), and leaves the five result variables at 0. Negative inputs are in the tests.

Up nextIf/Then and LoopsControl Flow

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…