qbasic notes

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

QBASIC NOTES FOR CLASS 8

INTRODUCTION
A computer is a machine that understands instructions in binary or machine language.
Therefore, programs written in high level languages like C, C++, Java and BASIC have to be
translated first to its equivalent machine language with the help of translators.
BASIC stands for Beginners All – Purpose Symbolic Instruction code. In the year 1975, Bill
Gates and Paul Allen developed QBasic. It is an interpreter based high level language, where
tasks are performed using Windows environment.

Features of QBASIC
• The syntax is very simple and user friendly.
• It provides Windows based platform for writing programs.
• Debugging can be done easily, as it provides the facility to find errors.
STARTING QBASIC
When QBASIC is loaded and is included in the program’s menu, following the given steps:
Step 1: Click on start button.
Step 2: Click on the All Programs option.
Step 3: From the list displayed click on the QBASIC program. It will open and
display a welcome message on it.
Step 4: Press the Esc key to hide the welcome screen and view the QBASIC editor.

The screen of QBASIC is called an IDE- Integrated Development Environment and creating
of the program are done at the same time.

Components of QBASIC
The screen of the user interface of QB64 window is given below: -
• Title Bar: It displays the name of the application which is QBASIC. On the right hand
side we see the Minimize, Maximize and Close buttons.
• The Menu Bar: The menu bar has various menus: File, Edit, View, Search, Run,
Debug, Options, and Help. Clicking on any of these will show you the commands
under these menus.
• Name of the Current Program: The name of the currently opened program file is
displayed at the top of the screen.
• Status Bar: The status bar at the bottom of the screen displays a list of shortcut keys to
some commands.

ELEMENTS OF QBASIC PROGRAMMING LANGUAGE


Like all programming languages, QBASIC is also made up of some basic elements. These
elements construct the program written by us in QBASIC. These elements are as follows:
Constants
These are the data (fixed) value used in a BASIC program. Constants cannot be changed and
remain the same during the entire program execution.
Types of constants:
• Numeric constants can be positive, negative and can even have decimal
points. For example, 3, 73, -56, 3.7 etc.
• String constants or alphanumeric constants are a combination of letters,
digits, special characters and blank spaces enclosed in double quotes. For
example “Python”, “25 Wellington Street”, etc.
Variables
It is a name given to a memory location that store constant values. The values stored in a
variable are not fixed and can change during program execution.
Types of variable:
• Numeric Variables: Numeric variables store numeric constants. It is a user
defined name which is a combination of letters and digits. For example,
age, mar1.
• String Variables: String variables are used to store a string of characters.
String variable names should always end with a $ sign. For example,
name$="Radha", items$="eraser".
Rules for naming a variable.
While naming variables we must follow the rules mentioned below:
i. Both numeric and string variables must begin with alphabets.
ii. Variable names can contain numbers also but not at the starting position.
iii. The variable name must not have any space in between.
iv. Variable name must not contain any special characters.
Operators
Operators are used to perform various operations on constants or variables. For example,
consider the operation 1+2.Here 1 and 2 are constants and called the operands while + is the
operator which is causing the 1 and 2 to be added.
QBASIC has three types of operators:
• Arithmetic Operators: These are used to perform mathematical calculations on
constants or numeric values. They are:
+ (Addition) - (Subtraction) * (Multiplication)
/ (Division) ^ (Exponential)
• Relational Operators: These are used to compare values. They are:
> (Greater than) >= (Greater than or equal) < (Less than)
<= (Less than or equal) <> (Not Equal) = (Equal to)

• Logical Operators: These operators are used to combine two or more relational
expressions which return a single value that can be either True or False. Logical
operators are used in decision making statements. The logical operators are: AND,
OR, and NOT. For example: (age>0 AND age<60).

COMMANDS/STATEMENTS
Statements or commands are typed to give instructions to the machine to evaluate logically
correct output. These commands help construct proper syntactic instructions to the
interpreter.
• REM COMMAND
REM command is used to put a comment line in a program. It is a program heading ignored
by the interpreter while executing a set of codes.
• CLS COMMAND
It is a command used to clear the output screen.
• PRINT COMMAND
The Print statement is used to display content on the output screen.

Laboratory Activity
State the output for the corresponding print statements: -
Instruction: Output:
i. PRINT "Let peace be everywhere"
ii. PRINT 295
iii. PRINT 2^5-(6*3-25/5)

Using Semicolon With Print


Using a semicolon with the Print command inserts a single blank space in between the words.

Using Coma With Print


A line on the QBASIC output screen is separated into 5 zones, each with 14 spaces. We can
use commas to put our text in different zones of the line.
For example:
PRINT "Gopal", "Swaroop "; "Sharma"
Output:
Gopal Swaroop Sharma

Leaving a Blank Line


The print statement can be given without any data or variable, to leave a blank line in
between multiple lines of output.
Gopal
PRINT “Gopal”
PRINT
Swaroop
PRINT”Swaroop”
PRINT Sharma
PRINT “Sharma”
• INPUT STATEMENT
The Input statement allows the user to enter values while the program is being executed.
Syntax: INPUT <variable>
When we run the program, the computer will display a “?” on the screen. It will wait for the
user to enter the value. The data entered is stored in the mentioned variable.

Input statement also allows displaying a message to guide the user to enter correct data. You
can also read multiple values for multiple variables in one INPUT statement by separating the
variables by a comma.

• END STATEMENT
Every program must have an END statement which is written as the last statement in every
program. The END statement is used to terminate or end the program.

CONTROL STATEMENTS IN QBASIC


Normally programs are executed from top to bottom, in the order that they are written. But
sometimes it is required to alter the flow of sequence of execution in the program. For that
QBASIC provide some statements. The statement which changes and transfer the flow of
program from one statement line to another are called control statements.
There are two types of control statements in QBASIC. They are:
Branching Statement Looping Statement

IF ... THEN FOR .. NEXT

IF .. THEN ... ELSE WHILE ... WEND

IF ... THEN ... ELSE IF DO ... LOOP

GOTO

Branching Statement also further divided into two types:


• Conditional Branching Statement
o IF ... THEN
o IF ... THEN ... ELSE
o IF ... THEN ... ELSE IF
o SELECT CASE
• Unconditional Branching Statement
o GOTO
IF ... THEN Statement
It is the most simple form of the control statements which executes a block of statements only
if the given expression or condition is true. If the condition is false, then the IF block will
skipped and execution continues with the rest of the program.
Syntax:
IF [conditional expression] THEN
[statement]
END IF
Example:
CLS
INPUT "Enter your marks: "; m
IF m >= 35 THEN PRINT "You are passed !!"
END

IF ... THEN .... ELSE Statement


It is a common control statement which is used to execute the multiple statements depending
on the condition. It is also called two way decision statement. In this statement if the
condition is true the statements after THEN will be executed and if the condition is false, the
statements in the ELSE block will be executed.
Syntax: Example
CLS
IF [conditional expression] THEN
INPUT "Enter your marks "; m
[statement 1] IF m >= 35 THEN
ELSE PRINT "You are passed !!"
[statement 2] ELSE
END IF PRINT "You are failed .."
END IF
END
IF ... THEN ... ELSEIF Statement
It is also same as IF .. THEN statement, but the only difference is that, by using IF .. THEN
statement we can test only one condition. But what if we have more than one condition? At
that time, we can use IF ... THEN ... ELSEIF statement.
Syntax:
Example
IF [condition] THEN
CLS
[statement 1]
INPUT "Enter Three Numbers "; x, y, z
ELSEIF
IF x > y AND x > z THEN
[statement 2]
PRINT "Greatest number is "; x
ELSEIF
ELSEIF y > x AND y > z THEN
[statement n]
PRINT "Greatest number is "; y
........................
ELSE
.......................
PRINT "Greatest number is "; z
ELSE
END IF
[statement n]
END
END IF

LOOPING STATEMENT
The process of repeating or doing same task many times until the given condition is true is
called looping or iteration. There are different looping statements are used in QBASIC such
as FOR ... NEXT, WHILE .... WEND, DO ... LOOP, etc. It allows a specified group of
statements to be executed a certain number of times while a condition is true. Among these
looping statements FOR ... NEXT is the most common and popular looping statement.

FOR ... NEXT Loop


The FOR ... Next is a most popular and mostly used looping statement which is used to
execute the set of statements repeatedly for a given number of times.
Syntax:
FOR = TO STEP n
[statements]
NEXT
Example
WRITE A PROGRAM to print the natural numbers from 1 to 10.
CLS
FOR x = 1 TO 10 step 1
PRINT x
NEXT x
END

FUNCTIONS IN QBASIC
Functions are built-in operations which can be used in our current program to simplify the
logic and perform manipulations easily.

String functions: These built-in functions help in manipulating string variable and
string constants. A function that ends with $ sign returns string values as a result whereas
other functions return numeric output.
Function Syntax Purpose Example
LEN Len(string) Returns the number of Print Len(“Apple”)
characters present in the display 5 on the output
string screen
Ucase UCase$(String) Returns the passed A$= ucase$(“Apple”)
parameter in uppercase will store APPLE in
variable A$
Lcase LCase$(String) Returns the passed Print LCase$(“Apple”)
parameter in lowercase displays apple on the
output screen
Left Left$(String, count) Extracts number of Left$("Computer", 5)
characters of the string returns Compu.
from the left.
Right Right$(String, count) Extracts number of Right$("Computer", 5)
characters of the string returns puter.
from the right.

Assignment
A. Choose the correct option:
a. This statement is used to terminate or end the program.
i. Input ii. End iii. Cls
b. This command is used to clear the output screen.
i. Rem ii. End iii. Cls
c. This command is like program heading.
i. Input ii. Rem iii. End
d. A line on the QBASIC screen is separated into _______.
i. 2 zones ii. 5 zones iii. 7 zones
e. This variable is used to store a string of characters.
i. Numeric ii. String iii. Constants
B. Fill in the blanks:
a. __________ are the data values used in a BASIC program.
b. __________ is a version of BASIC.
c. AND/ OR are the __________ operators.
d. __________ variable store numeric data.
C. Answer the following questions:
a. What do you understand by QBasic?
b. Why is QBASIC environment called an IDE?
c. What are constants? Explain its types.
d. What are variables? Discuss its types.
e. What is the Input statement used for?
f. How are logical operators different from relational operators? Give examples.
g. What is the significance of step in for loop? What happens if the step is not
mentioned?
h. Define iteration.
i. Which tab and command are used to display the output? Mention its shortcut
key.
D. Write QBASIC programs for the following task:
a. To calculate the sum of three numeric values.
b. To find the average of four subject marks.
c. To read two numbers and swap them using a third temporary variable.
d. Find the area of rectangle, square and circle.
e. To find the volume of sphere, cylinder, cone.
f. Find simple interest and amount for entered principle, rate, and time.
g. To display whether a number is positive or negative.
h. To accept two unequal numbers and display the greater of the two numbers.
i. To accept two numbers and if two numbers are equal then find the sum
otherwise, their product.
j. To enter two angles and check whether they are complementary,
supplementary or neither.
k. To enter the cost price and the selling price of an article. Calculate the profit
and profit percent or the loss and the loss percent.
l. To print the natural numbers from 1 to 15.
m. To print even numbers from 0 to 20.
n. To print numbers from 12 to 1 in descending order.
o. To print multiplication table up to 12 of given number.
p. To accept a 3-digit number and print the sum of the first and second number
and the product of first and last number.
q. To print a number in the reverse order.
r. To accept any range of m and n and print all the odd numbers between them.
s. To accept any number between 100-500 and check if it is even or odd.
t. To accept any number between 1 to 100 and print whether the number is a
prime number or not.
u. Read a name and display in a triangular pattern.
For example,
Q
QB
QBA
QBAS
QBASI
QBASIC

You might also like