Step 1 of 5 · Reading · ~4 min
Learn
Basics
Hello, BASIC
By the end of this lesson you can write a FreeBASIC program that reads a line of text and prints it inside a message, and you can predict exactly which spaces PRINT will put on the line.
A program is a list of statements
BASIC (Beginner's All-purpose Symbolic Instruction Code) was created at Dartmouth College in 1964 so that people who were not scientists could program. This course uses FreeBASIC, whose compiler is called fbc. It keeps the classic syntax but compiles to native machine code, the same way a C compiler does.
A program is a list of statements that run from the top of the file to the bottom. There is no main function, no class and no import:
PRINT "Ready."
PRINT "Launch in ten seconds"
Ready.
Launch in ten seconds
PRINT writes what you give it and then moves to a new line, so two PRINT statements make two lines. Four ground rules before anything else:
- Keywords are not case-sensitive.
PRINT,printandPrintare the same word. This course writes keywords in capitals, like the classic listings. - Text inside quotes is exact.
"Ready."and"READY."are different strings, and the grader compares every character. - One statement per line. There is no semicolon at the end of a line.
- A comment starts with
'(or the old wordREM). The compiler ignores the rest of that line.
Putting several pieces on one line
PRINT takes a list of items. The separator between them decides the spacing:
PRINT "Landing on "; "Mars"; "."
PRINT "Landing on ", "Mars"
PRINT "Standby";
PRINT " ... done"
Landing on Mars.
Landing on Mars
Standby ... done
- A semicolon
;adds nothing between the items. Every space you want must be inside the quotes. - A comma
,jumps to the next print zone. Zones are 14 characters wide, so the second item starts in column 15 and the gap is filled with spaces. That layout was designed for tables on 1980s screens; it is almost never what a grader expects. - A
;at the very end of aPRINTsuppresses the newline, so the nextPRINTcontinues on the same line.
Text that is not known in advance
DIM planet AS STRING
INPUT "", planet
PRINT "Landing on "; planet; "."
DIM declares a variable: a named place in memory. AS STRING says it holds text. INPUT "", planet reads one line of input into it, so if the input is Venus the program prints Landing on Venus.
The empty "" before the comma is the prompt. Plain INPUT planet prints a ? first, and on this platform that stray ? becomes part of your output and fails the comparison. Also, INPUT treats a comma as the end of a value, so the inputs in this course never contain commas. The next lesson covers variables properly; for now, a name in a PRINT list without quotes means "the value stored in that variable".
The trap: quotes decide between text and a name
PRINT Mars
This does not print the word Mars. Without quotes the compiler reads Mars as a variable, finds no DIM Mars, and stops with a "variable not declared" error. Old BASICs silently invented the variable and printed 0 or nothing; FreeBASIC turns that bug into an error message. The opposite mistake compiles fine and is just as wrong: PRINT "planet" prints the word planet, not what the variable holds.
Common mistakes
- A comma instead of
;between the pieces.PRINT "Landing on ", planetpads out to column 15, so the line contains a run of extra spaces and fails. - A missing space or punctuation mark.
PRINT "Landing on"; planetgivesLanding onVenus. - Quoting the variable name, which prints the name instead of the value.
- Leaving a debugging
PRINTin the program. The grader compares whole lines, so an extra line of text fails even when the right line is also there.
Your exercise
Read one name and print a greeting line that contains the name, followed by a fixed second line. The starter reads the name and already prints the second line; you add the greeting above it. The most likely mistake is joining the pieces with commas instead of semicolons.
Discussion
Ask a question, share an insight, or help someone who’s stuck.
Sign in to post a comment or reply.
Loading…