QBasic Tutorial: Chapter 3
The PRINT statement - Writing data to the screen
The print statement is used to write data on the screen.
You can write:
- Numbers
- Characters, Sentences, Texts
- The contents of memory variables and constants
Note: Characters and sentences are written within inverted commas("
"). What is written within inverted commas is displayed as is during run
time.
The following examples illustrate the different ways to use the PRINT
statement:
| Code |
Screen Output |
Comment |
| PRINT
"5" |
5 |
Writing
character 5 to the screen.
|
| PRINT
5 |
5 |
Writing number
5 to the screen.
|
PRINT
5
PRINT "5" |
5
5
|
Note the space
before the number 5 in the first line. BASIC always puts a space for +/-
signs when printing a number. |
| PRINT
"5 + 7" |
5
+ 7
|
What is within
inverted commas is written as is. |
| PRINT
5 + 7 |
12
|
Writing the sum
of 5 and 7 to the screen |
| PRINT
6 * 7 |
42
|
The * sign is
used for multiplication. |
| PRINT
12 / 4 |
3
|
The / sign is
used for division. |
| PRINT
8 - 3 |
5
|
|
a
= 3
b = 4
c = a + b
d = a * b
PRINT a
PRINT b
PRINT c
PRINT d
|
3
4
7
12 |
a,
b, c and d
are memory variables.
The value 3 is assigned to memory variable a.
The value 4 is assigned to memory variable b.
In the 3rd line, the computer looks assigns the sum of memory variables a
and b to memory variable c.
In the 4th line the computer looks assigns the product of memory variables
a and b to
memory variable d.
In line 5, the computer looks for the value of memory variable a
and writes it to the screen. |
a
= 3
PRINT "a"
|
a |
Did you expect
to get 3 on the screen? Don't forget that whatever is within inverted
commas is written as is.
The correct code should be:
a = 3
PRINT a
|
We will see more features of the PRINT statement later.
[ Previous Index
Next ]
|