Step 1 of 4 · Reading · ~4 min
Learn
Basics
Variables and Math
A variable is a named box in memory. In FreeBASIC you create one with DIM, and you say what type of value the box holds. The type matters because this is a compiled language: the compiler uses it to decide how much memory the box needs and which operations make sense on it.
DIM age AS INTEGER ' whole numbers
DIM price AS DOUBLE ' numbers with a decimal point
DIM city AS STRING ' text
age = 36
price = 19.99
city = "Dhaka"
You can declare and assign in one line, and declare several variables at once:
DIM score AS INTEGER = 100
DIM a AS INTEGER, b AS INTEGER
Why declaration is mandatory
FreeBASIC's default dialect refuses to touch a variable you never declared. That sounds strict; it is protection:
DIM total AS INTEGER
total = 10
totl = total + 5 ' COMPILE ERROR: variable not declared
The misspelled totl is caught at compile time. Old BASICs silently created a brand-new variable worth 0 and carried on — the most infamous bug generator of the 8-bit era.
The suffix trap (legacy syntax)
In old listings you will see type suffixes instead of AS: count% for an integer, total# for a double, a trailing $ for a string. Recognize them — but do not write them. The default FreeBASIC dialect used here rejects them:
DIM count% ' COMPILE ERROR: suffixes only exist in the old -lang qb dialect
DIM count AS INTEGER ' the modern way
Arithmetic
PRINT 7 + 2 ' 9
PRINT 7 - 2 ' 5
PRINT 7 * 2 ' 14
PRINT 7 / 2 ' 3.5 -- / ALWAYS gives a floating-point result
PRINT 7 \ 2 ' 3 -- \ is integer division (drops the remainder)
PRINT 7 MOD 2 ' 1 -- remainder
PRINT 2 ^ 10 ' 1024 -- exponent
The classic trap is / versus \. If you want a whole-number result, \ (backslash) is the operator — 7 / 2 is 3.5 even when both sides are INTEGERs.
The leading space in front of every number
This rule costs more attempts on this course than everything else combined, 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 total AS INTEGER = 7
PRINT total ' writes " 7" — space, then 7
PRINT -5 ' writes "-5" — the sign fills the column
PRINT "sum: "; total ' writes "sum: 7" — two spaces, not one
PRINT STR(total) ' writes "7" — no sign column at all
It is a teletype convention from the 1960s: numbers in a column line up whether or not they are negative. The grader does not trim leading spaces, so " 7" is not "7" and the test fails.
STR() converts a number to text without the sign column, and the & operator does the same conversion for you:
PRINT STR(-5) ' -5
PRINT "sum: " & 7 ' sum: 7 (& converts, no extra space)
PRINT "sum: " + 7 ' COMPILE ERROR: type mismatch
The rule to carry through this whole course: when a bare number is the output the grader checks, print STR(...) of it, or build the line with &.
Strings
& glues strings together — and, as you just saw, it converts numbers to text automatically:
DIM greeting AS STRING
greeting = "Hello, " & city & "!"
PRINT greeting ' Hello, Dhaka!
PRINT "Length: " & LEN(greeting) ' Length: 13
PRINT UCASE(city) ' DHAKA
PRINT LCASE(city) ' dhaka
+ also concatenates, but only when both sides are strings. Habit worth building: use & for text.
Reading two values
INPUT "", x reads one value per line of input; the "" suppresses the ? prompt that would otherwise pollute your output:
DIM a AS INTEGER, b AS INTEGER
INPUT "", a
INPUT "", b
That is exactly what your exercise starter already does. Note the "" is an empty prompt, not a value — writing INPUT "3", a prints 3 and then reads, which is not what you want.
Your exercise
Add up the two integers the starter has already read, and print the total.
- The starter declares a variable for the total and already prints it through
STR(), so the sign column is handled for you — you only have to give that variable the right value. - Print only the number. For inputs 3 and 4 the whole output is
7; decorations likesum = 7, or echoing the inputs back, fail. - One of the tests feeds a negative value, and
-5plus5is0. Plain addition already handles it — noIFneeded.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…