.                                                . How to use this forum              The QBasic Forum                                     

 Return to Index  

1- PRINT statements and variable assignments

June 14 2004 at 11:17 AM
Solitaire  (Login Solitaire1)


The first commands/keywords to learn are:

CLS - clears the screen. Place this at the top of every program.

END - ends the program. SYSTEM also ends the program and is useful for returning to the desktop or DOS prompt if you are running a compiled program. For these lessons, we will be using the END keyword.

REM - remark or comment. Everything following the REM keyword is ignored by QBasic. You can also use a single quote ' instead of REM, which does the same thing. The ' can be used at the end of a line of code followed by a comment on what the code is supposed to do. Comments are used by the programmer to document his or her code so when s/he or someone else looks at it later, they will know what it was supposed to do.

PRINT - will output the result of an expression to the screen. PRINT by itself skips a line. PRINT followed by a constant expression will display that expression on the screen.

Example: PRINT 4
will display the number 4 on the screen.

Example: PRINT 2 + 3
will display the result, the number 5, on the screen.


You may assign values to variables. A variable occupies a place in the computer's memory. A variable may be either a number or a string. You give the variable a name you make up, which must begin with a letter. It may not be the same name as another keyword or reserved word. The assignment operator is an equal sign. The name of the variable is on the left side of the = sign and the value is on the right side.

NOTE: The keyword LET is an obsolete command used for assigning values to variables in older unstructured versions of BASIC. It was included in QB in order to be backwards compatible with older code from GWBASIC and BASICA. Unfortunately, some old QB textbooks (and some teachers who were using these old books) may still use LET in their classes, but it is no longer supported in newer version of Basic, and we will not be using it.

The following program shows how you assign a value to two variables named numx and numy, and then displays the variable's values:

numx = 6
numy = 3
PRINT numx
PRINT numy
PRINT numx + numy

numx + numy is an expression. QBasic will compute the value of the expression before printing out the result. You can also assign the value of an expression to a variable. Example:

numz = numx + numy
PRINT numz

The output (screen display) of the program will be:

There are two main variable types, strings and numbers. A string variable name must end with a $. This tell QBasic that the value is not a number and not to do any arithmetic with it. The value of the string must be enclosed within quotation marks:

myname$ = "Joe"
PRINT myname$

The program will display the word, "Joe" without the quotation marks.

A string will not compute. If you assign an expression like the following to a string variable, it will simply display everything that was between the quotation marks. Example:

mynum$ = "2 + 3"
PRINT mynum$

The program will display "2 + 3" without the quotes.

You may combine string or number values with variable values by using a semicolon to separate them. Example:

nom$ = "Student"
age = 16
PRINT "Hello "; nom$
PRINT "You are"; age; "years old."

The program will display the following:
Hello Student
You are 16 years old.

When combining strings, keep in mind that you must include a space between words before adding the quotation mark. Numbers will add a space on either side. Type in the above programs to see for yourself how it looks.

Variable values may be reassigned. That's why they are called variables - the value may vary. The new value takes the place of the old one in the computers memory location. Example:

mynum = 5
PRINT mynum
mynum = 8
PRINT mynum

The program will display:

----------------LAB PROBLEM---------------------------------------------

For your first lab problem, write a program that uses two string variables and two number variables. Assign the values "Student" and "Teacher" to the string variables. Assign the values 12 and 34 to the number variables. Print "Hello" to the student and "Good morning" to the teacher. Print out an addition example using the number variables with a "+" between the two numbers, and then display the result of the addition after an "=" sign. You will need to use semicolons to separate each of the number variables from the string "+" and "=" and the numeric expression at the end.

This message has been edited by Solitaire1 on Jun 14, 2004 11:19 AM

 Respond to this message   
 Copyright © 1999-2018 Network54. All rights reserved.   Terms of Use   Privacy Statement