Reading — step 1 of 4
Learn
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.
How PRINT shows numbers
On this platform PRINT 7 outputs exactly 7 and a newline — no padding. Trivia with consequences: 1980s BASICs (and FreeBASIC's -lang qb compatibility mode) print a leading space before positive numbers. The default dialect does not, and the grader's expected outputs are written for this no-space behavior.
Strings
& glues strings together — and 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 — "Age: " + 36 is a compile-time type error, while "Age: " & 36 just works. 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.
Your exercise
Read two integers (the starter handles that) and print their sum. Replace the ' PRINT ... comment with one statement.
- Print only the number.
PRINT a + bfor inputs 3 and 4 outputs exactly7— the grader rejects decorations likesum = 7or echoing the inputs back. - The hidden test feeds a negative value (
-5and5must print0). Plain addition already handles it — no IF needed. - One PRINT, run once. The newline after
7comes from PRINT itself.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…