Numeric Constants: Qbasic Chapter 3: Fundamental Statements
Numeric Constants: Qbasic Chapter 3: Fundamental Statements
Numeric Constants: Qbasic Chapter 3: Fundamental Statements
docx
OBJECTIVES:
Differentiate between numeric and character string constants and give examples of each
Use numeric character string constants correctly in programs
Explain how variables are used to store values in the computer’s main memory
List the rules for naming variables
Define the term keywords
Correctly document programs
Assign values to variables
Perform arithmetic operations using both constants and variables
Evaluate arithmetic expressions according to the hierarchy of operations
Display program output on the monitor screen
Use the Immediate window to execute statements in immediate mode
Use the various forms of help available with QBasic
QBasic programs process data into meaningful results using Commands and Data
A constant is data that remains the same as the program runs (executes)
There are two types of constants: Numeric constants and Character String constants
Numeric constants are numbers included in a statement. They can be real numbers, which
include decimal points or integers, which do not include decimal points.
1. No commas can be used when entering numbers into the computer. The computer
interprets the digits before and after a comma as two separate numbers. [ 3456789234]
2. If a number has no sign the computer assumes it is positive. [ 546 same as +546]
3. If a number is negative, the negative sign must precede the digits. [ -34 ]
4. Fractions must be written in decimal form. [ 3.75 ]
Exponential Notation:
QBasic uses scientific notation (exponential notation) to represent very large or very small numbers.
Scientific Notation
Single-precision scientific notation numbers contain a letter E (for Exponent). Whereas double-
precision numbers contain a D (for Double-precision exponent).
To translate scientific notation, multiply the portion of the number to the left of the letter (D or E)
by 10 raised to the number to the right of the letter.
Thus, +2.164D+3 means to multiply 2.164 by 1,000 (1,000 is 10 raised to the third power, or 10 3 ).
To multiply by 10 raised to a positive power, you need only move the decimal point to the right by
the number of the power.
To multiply a number by 10 raised to a negative power, you need only move the decimal point to
the left by the number of the power.
497564044.docx
-4.940656458412465 x 10-324 to
-1.79769313486231 x 10308
You place the suffix at the end of the variable’s name if you want QBasic to assume the variable is
a specific type
Examples:
Variable name QBasic Assumption
distance single-precision
distance% Integer
distance& Long integer
distance! Single-precision
distance# Double-precision
Integer 2 bytes
Long Integer 4 bytes
Single-precision 4 bytes
497564044.docx
Double-precision 8 bytes
alphanumeric data: any combination of letters, numbers or special characters as seen on the
keyboard.
The character string must be enclosed in double quotation marks [ “character string” ]
You may include single quotation marks within a string that is contained within double quotation
marks.
“She said, ‘What is your name?’ ” Valid
“ The letter “e” is a vowel.” Invalid
The maximum length of a string is 32,767 characters. The length of a string is determined by
counting all its characters even the blank spaces.
CONSTANTS
QBasic interprets the constants in your programs and makes a good judgment on how to store them
Variables:
A variable is a storage location in your computer that holds values.
A variable contains data that changes as the program runs (executes).
497564044.docx
A variable can contain a number, a special character, a word, a sentence or an entire paragraph of
text
Because you will use many variables in one program, you must assign a unique name to each
variable so that you can keep track of them.
Variable names:
should describe the contents of the variable (storage location). – should have useful meaning
to the purpose of the program. This is a good programming habit referred to as descriptive
variable names.
can be from one to forty characters long -- While longer names are possible QBasic only
recognizes the first 40 characters. Therefore we will observe this “good programming
habit”
must begin with a letter; the characters that follow can be letters, numbers, or a period (The
use of a period is not a good practice. We will NOT use it in this class. Use the underline
instead)
can be a mix of uppercase and lowercase letters to separate parts of the variable name,
cannot have spaces in them
cannot have the same name as QBasic commands or functions—aka “keyword”
The variable names Sales and SALES refer to the same variable.
String variables: used to store a character string, such as a name, an address, or social
security number.
String variable name begins with a letter followed by letter or digits and must end with a dollar sign.
Nme$
SSnumb$
Dte$
Keywords: reserved words that have a predefined meaning to QBasic. You can not use them
as variable names. See Appendix A for a list. Please note that DATE$ and Name are on the list
497564044.docx
CLS
We will use the CLS as the first line of every program so that the screen clears before each time the
program runs so the program starts with a fresh screen.
Documenting a Program
The REM (short for remark) command is used to make the code more understandable to humans.
The computer ignores the command and everything that follow.
REM any message you choose to explain to fellow programmers what the
REM program is doing
You can insert as many remarks in your program as you want anywhere you want.
You will use the REM command at the beginning of each program to record you name, the file
name and the purpose of the program. You will use the REM command through out the program to
explain what your code is trying to accomplish.
Since the REM command appears so often in programs there is an abbreviation for the statement.
Instead of typing REM, you can type and apostrophe.
END
LET is optional but in this class you must use this command when assigning a value to a variable.
Examples
LET age = 32
LET salary = 25000
LET dependents = 2
LET ClassSize$ = “30”
LET Heading$ = “Salary Totals ”
You can assign the value stored in one variable to another variable
LET age = 32
LET salary = 25000
LET age = 25
After these lines of code have executed what is the value of age?
If you do not place a value in a variable, by default QBasic stores a zero in that variable.
497564044.docx
The same variable name can appear on both sides of the equal sign
LET Count = 10
LET Count = Count + 1
What is the value stored in Count after the first line of code is executed?
What is the value stored in Count after the second line of code is executed?
Displaying Results
PRINT Statement
The PRINT statement, during the execution of the program, sends to the screen whatever is to the
right of the word PRINT
PRINT expression
The expression that you print can be a variable or constant. If you use a variable as expression,
PRINT prints the contents of that variable on the screen. If expression is a constant, PRINT prints
that constant.
If you put PRINT in a line of code with no expression, the program prints a blank line.
A literal is a group of characters containing any combination of alphabetic, numeric and /or special
characters. It is a term applied to constants used in a PRINT statement.
Screen output
PRINT age
PRINT salary
PRINT dependents
Write a short program using variables and the PRINT command that will output the
following.
C:\>
27
-56
7890
LET X = 15
LET Y = 5
PRINT ( X + Y ) / 2, X / Y
END
10 3
The END command is optional but we will use it. This will assure me that you meant for the
program to end and none of your code is missing.
497564044.docx
END
The END command will halt the execution of the program as soon as it is encountered.
Programming statements are ordinarily executed in programming mode; execution does not begin
until you instruct QBasic to RUN.
The Immediate window at the bottom of the QBasic screen can be used to execute statements as
soon as ENTER is pressed.
Programming mode: The mode in which programs usually are entered and executed.
Immediate mode: The mode in which a QBasic statement is executed as soon as ENTER is pressed.
Press any key to return to the Immediate Window.
To move the cursor from the View window to the Immediate window, press F6.
Getting Help
The function key F1 allows you to access online help.
If you want help regarding any of the options in any of the menus simply highlight the option and
then press F1.
If you need information about using a QBasic keyword (command) use the mouse or arrow keys to
position the cursor in the keyword and press F1. you can also press the right mouse button instead
of the F1 function key.
To access a list of help topics press Alt and H. Press the highlighted letter to choose a particular
command.
QBasic has a “smart editor” to catch many types of syntax errors as you are typing in a program.
As soon as you press ENTER a message will display providing hints on how to correct the error.
But remember in order to interpret these messages you must “think like the QBasic editor”.
CLS
LET age = 32
PRINT age
LET dependents = 3
PRINT dependents
END
Review Questions
2. What is a variable?
4. True or False: A variable can be any of three types of integers: integer, single integer, or
double integer.
5. True or False: A variable can be any of two types of floating points: single-precision or
double-precision.
Review Questions
2. What is a variable?
A storage location in your computer that holds values
4. True or False: A variable can be any of three types of integers: integer, single integer, or
double integer.
False: There is no such type as single integer or double integer
5. True or False: A variable can be any of two types of floating points: single-precision or
double-precision.
True
Lesson 2 Exercises
For 1-6 use Microsoft Word to type the following short programs. Turn in a hard copy of your
work.
1. Write a program that stores your weight (you can fib), height (in feet) and shoe size in three
variables. Use the LET statement.
2. Write a program that clears the screen and then prints the current temperature on-screen.
(You can use the Internet to find this information.)
3. Write a program that stores you two favorite television channels in two variables and then
prints them. Clear the screen first.
4. Write a program that stores and prints the four types of variable that you learned about
today. Make up any valid variable names that you want. Use the suffixes of the values
when you are storing and printing the values.
5. Rewrite the program you wrote in number four so that the code clearly indicates exactly
where the program ends.
6. Write a program that stores the following scientific-notation numbers in three variables and
then prints them to a blank screen. Make sure that you use the right type of variable by
adding the correct suffix to its name. -3.43E-9 +5.43345D+20 5.43345 x 10-20
7. Write each program in the QBasic editor, save each program as Les2_1, Les2_2, etc. , then
run the program. Record below what appears on the screen.
1. 2. 3.
4. 5. 6.