Q-BASIC
LESSON 1
INTRODUCTION
• Quick Beginners All-Purpose Symbolic
Instruction Code is an interpreter and a kind of
the variety of BASIC programming languages.
• We’ ll use QB64 or QB-DOS IDE for writing our
program codes.
FEATURES OF QBASIC WINDOW
The display window has two parts namely:
1. View Window Mode
The upper and broader view layer just like the full screen.
Codes typed in here must be compiled by Clicking on the Run Menu
or pressing the F5 key.
2. Immediate Window Mode
The smaller window at the bottom of the window.
Codes typed in here gets executed immediately the Return key or
enter key is pressed.
BASIC STATEMENTS AND
COMMANDS
1. REM
Remarks is a keyword or reserved word that is used in Qbasic to
denote a comment or add a remarks to your program. Remarks are
not executable codes but help document a program and makes it
more meaningful when reading it.
The single quotation mark (‘) is also used to add comment to a
statement
2. CLS
It stands for Clear Screen and is used to clear the display screen so
that anytime the program runs, you get a blank display screen.
BASIC STATEMENTS AND
COMMANDS Cont’
3. INPUT
This is an I/O Statement that is used to accept or obtain data from a user. It is very prudent to know
the type of data you are expecting from a user and as such, you need to declare a variable of a data type to
store that data.
SYNTAX
INPUT “Message to the User”, variableName
INPUT is the keyword to obtain data from a user
“Message to the User” is to enable the user know what kind of data to give
variableName is the storage location to store the data provided by the user.
Example:
INPUT “Enter your fullname”, fullName$
Output
Enter your fullname KOBBY LEDLUM
Using semi-colon (;) as a separator with the Input command will display question mark (?)
so instead use the comma (,)
BASIC STATEMENTS AND
COMMANDS Cont’
4. PRINT
This is an I/O Statement that is used to display results to a user. After taking in
data and processing it, the output must be displayed.
SYNTAX
PRINT (Expression) Separator (Expression)
Example 1:
PRINT “The sum of two numbers is “; Sum
Use the semi-colon (;) separator to display a single space
Example 2:
PRINT “The sum of two numbers is “, Sum
Use the comma (,) separator to display multiple spaces
BASIC STATEMENTS AND
COMMANDS Cont’
5. LET
This is an assignment command which is used always with the
assignment operator (=).
SYNTAX
LET variableName = Expression
Example:
LET A = B + C
LET Fname$ = “Programming”
DATA TYPES
• In programming, it is very important to know the types of data exiting
in a particular programming language and how to declare them. The
data type defines the type of data requesting for at a particular point
in execution time.
In QBasic, there are only 2 types of data namely:
1. STRING
2. NUMERIC
VARIABLES
A variable is a unique name given to a temporal storage location in memory for storing values
during a program execution.
A value in a variable can be changed.
Two types:
1. String Variable:
It is a variable that stores alphanumeric characters or text in general. Example: letters,
numbers and symbols or any combination. In QBasic, use a dollar sign ($) at the end of a
variable name to make it a string. Example: Sname$, R$, IndexNo$, PIN$
2. Numeric Variable:
It is a variable that stores positive or negative numbers. Numbers here are those meant for
performing calculations. In QBasic, a variable name without a dollar sign ($) is presumed to be
a numeric data. Example: Age, Height, Weight, etc.
4 Different Kinds of Numeric
Variables:
1. Integer (Percentage Sign %)
This type of data is made up of positive and negative whole numbers: …,-3,-2,-1,0,1,2,3,….
Eg. Age%
2. Long Integer (Ampersand Sign &)
This type of data is also made up of large integers: 243345
Eg. Salary&
3. Single Precision (Exclamation Sign !)
This type of data contains small decimal fractions but the value before the dot (.) is the
small value: 23.456
Eg. Gross!
4. Double Precision (Hash Sign #)
This type of data contains huge decimal fractions but the value after the dot (.) is the large
value: 45637.56
Eg. Net#
CONSTANTS
Constants are fixed data values that do not change during program
execution.
2 Types:
1. String Constant
Alphanumeric characters that are enclosed in double quotes.
Eg: “Kobby Ledlum”
2. Numeric Constant
Any number so far as it is not enclosed in any double quotes is a
numeric constant. Eg:34, 2.5, -7
Rules for Naming Variables
1. A variable must have a name and the characters should not be
more than 40.
2. A variable name must begin with a letter
3. Reserved or keywords cannot be used as variable names
4. No spaces are allowed in variable names
5. Combination of letters and numbers are permitted but letters must
begin
6. No symbols allowed except for hyphens and underscores in variable
names
Accumulator
• This is a variable that stores the value of another variable. You can call
it a nested variable.
Example:
Let s = 9
Let y = s
s is a variable whereas y is an accumulator.
BASIC STATEMENTS AND
COMMANDS Cont’
6. END
This command tells the end of a program execution or rather
terminates a running program.
Example:
CLS
Let A = B * C
END
PRINT A
The above program code will run but display nothing because the
command END terminates the execution before the print line is
reached.
What is a STATEMENT?
It is an instruction in a program code.
It contains key commands and expressions that are executable.
There are 4 types of statements:
1. Declarative Statement
2. Assignment Statement
3. Control Statement
4. Input/Output Statement
Control Structure
• This is a statement or line of code that determines the flow of a
program. Since programming deals with algorithms, i.e. step-by-step
procedures, there are control structures to direct the flow of
instructions.
Here are some control structures:
1. Sequential (Unconditional)
2. Selection (Conditional)
3. Iteration (Repetition or Loop)
SEQUENTIAL STRUCTURE
• This structure executes commands in a sequential order, i.e. one after
the other. The line of execution is always fixed and not based on any
condition.
Example:
CLS
INPUT A
INPUT B
LET C = A + B
PRINT “SUM OF NUMBERS IS “; S
END
SELECTION STRUCTURE
Also known as branching structure that allows you to transfer the
program control from one part to another on the basis of a specified
condition
2 types of Selection Structure:
1. IF Statement
2. SELECT CASE Statement
IF Statement
This is a decision-making statement that decides which statement has
to be executed on the basis of the specified condition.
1. If…………..Then Statement
It is a one-way decision-making statement that evaluates a
condition and executes the statement if the result of the condition is
True.
SYNTAX Example:
If (condition) Then If age < 18 then
True Statement print “you are a child”
End if End if
IF Statement
2. If……Then…….Else Statement
It is a two-way decision-making statement that can decide which
part of the program gets executed when the specified condition is true
or false. It executes one part of the program if the condition is true and
another part of the condition if is false.
SYNTAX Example:
If (condition) Then If age < 18 then
True Statement print “you are young”
Else Else
False Statement print “you are old”
End if End if
IF Statement
3. If……Then……Elseif……Else Statement
It is a multi decision-making statement that is used when there
are two or more conditions to be evaluated.
SYNTAX Example:
If (condition) Then If sex = “Male” then
True Statement print “Yo Man!”
Elseif (condition) then Elseif age < 18 then
True Statement print “you are young”
Else Else
False Statement print “you are old”
End if End if
LOOP STRUCTURE
1. For………Next Statement
This statement repeats a block of statements a specified number of
times. The number of repetitions is known.
Example: Code to display 1,2,3,4,…………10
CLS
FOR K = 1 TO 10
PRINT K;
NEXT K
END
LOOP STRUCTURE
2. While………Wend Statement
This statement repeats a block of statements until the specified condition is
true.
Example: Code to display 1,4,7,10,…………25
CLS
K=1
WHILE K <= 25
PRINT K;
K=K+1
WEND
END
LOOP STRUCTURE
3. Do………Loop Statement
This statement repeats a block of statements while a condition istrue
Example: Code to display 1,2,3,4,…………10
CLS
FOR K = 1 TO 10
PRINT K;
NEXT K
END