Qbasic Notes
Qbasic Notes
Aditya Bhattacharjee
September 2023
1
Contents
Introduction to QBasic 4
What is a computer program? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is a statement? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is a syntax? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is BASIC? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
What is QBasic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
Features of QBasic? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
What are Keywords? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Screens . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Let Statement 13
Syntax of LET statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Difference between LET and INPUT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Conditional Statement 17
Understanding IF .. THEN .. ELSE in QBasic . . . . . . . . . . . . . . . . . . . . . . . . 17
Multiple Conditions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Logical Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
Choosing an Outfit Based on the Weather . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
Counter 22
DO WHILE... LOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
DO UNTIL... LOOP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
DO ... LOOP WHILE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
DO ... LOOP UNTIL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
FOR ... NEXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
FOR ...STEP ... NEXT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Subjective Questions 30
2
Objective Questions 30
Identify the numeric and String data. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
Rewrite the following statements after correcting the errors . . . . . . . . . . . . . . . . . 30
Write the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
School Programs 31
Revision 36
Question 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Fill in the blanks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Question 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Name the following and write their syntax. . . . . . . . . . . . . . . . . . . . . . . . 36
Question 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Name the following and write their syntax. . . . . . . . . . . . . . . . . . . . . . . . 36
Question 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Answer the following questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Question 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
Write one QBasic statement for each of the following. . . . . . . . . . . . . . . . . . 36
Question 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Write the output of the following programs . . . . . . . . . . . . . . . . . . . . . . . 37
Question 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Fill in the blanks to complete the program . . . . . . . . . . . . . . . . . . . . . . . . 37
Question 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Write the following programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Question 9 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
Write any four of the following programs: . . . . . . . . . . . . . . . . . . . . . . . . 37
3
Introduction to QBasic
What is a computer program?
A computer program is a set of instructions or a sequence of coded commands that tells a computer
how to perform a specific task or solve a problem. It is a precise set of instructions written in a
programming language that can be executed by a computer to produce a desired output or result.
What is a statement?
In computer programming, a statement is a complete instruction that performs a specific action or
operation. It is a fundamental unit of code that represents a single executable command within a
program.
What is a syntax?
In computer programming, syntax refers to the set of rules and conventions that dictate the structure,
format, and placement of individual elements and statements within a programming language. It
defines how code should be written and organized to be considered valid and understandable by the
programming language compiler or interpreter.
What is BASIC?
BASIC (Beginner’s All-purpose Symbolic Instruction Code) is a high-level programming language
that was designed in the 1960s to make programming more accessible to beginners and non-experts.
It was developed at Dartmouth College by John G. Kemeny and Thomas E. Kurtz as an educational
language.
What is QBasic?
QBasic is a programming language developed by Microsoft in the early 1990s. It stands for ”Quick
Beginners All-purpose Symbolic Instruction Code.” QBasic is based on the BASIC programming
language and was designed to be easy to learn and use, especially for beginners and hobbyists.
4
Features of QBasic?
• Simple Syntax: QBasic has a straightforward and easy-to-understand syntax, making it
accessible to beginners. It uses plain English keywords and minimal punctuation, allowing
users to focus on learning programming concepts rather than complex language constructs.
• Integrated Development Environment (IDE): QBasic includes an integrated develop-
ment environment with a text editor, debugger, and compiler. This makes it convenient for
writing, testing, and running programs in a single environment.
• Structured Programming Support: QBasic supports structured programming concepts
such as loops, conditionals, and subroutines. These features enable the organization and
modularization of code, making it easier to write and maintain programs.
• Graphics and Sound Capabilities: QBasic provides built-in commands for creating simple
graphics and sound effects. This makes it suitable for developing simple games and interactive
applications.
• Event-Driven Programming: QBasic supports event-driven programming, allowing users
to respond to user input or system events such as mouse clicks or key presses. This enables
the creation of interactive and responsive applications.
• Compatibility with DOS: QBasic is designed to run on the DOS operating system, making
it compatible with older computer systems. It can be run directly from the command line or
as a standalone executable.
• Educational Resources: QBasic has a wealth of educational resources available, including
tutorials, sample programs, and documentation. This makes it an excellent choice for learning
programming concepts and practising coding skills.
5
Screens
QBasic IDE as two screens.
Program Area/Editor Screen: This is the main area where you write your QBasic code. It is a
text editor where you can enter your program line by line.
• The place where we type the Qbasic instructions/statements.
• Every statement is typed on a new line
• All the reserved keywords are automatically capitalised.
Output Window: When you run a QBasic program, any output or error messages are displayed in
the output window. It shows the results of your program’s execution or any messages generated
during runtime.
• This is the screen where we see the output for the program.
• We can see the output screen after pressing the F5 function key on the keyboard.
• The output screen is divided into 5 zones which are not visible on the output screen.
• Pressing any key, takes us back to the EDITOR SCREEN
6
Fundamentals of QBasic Programming
Data Types
Data do not come in just one form. Programming languages, therefore, need different types of
variables to hold different types of data. BASIC has two categories of variables: Numeric, and
string. Numeric data can be both Integers and floating points (Decimal). It can also be positive and
negative. String data contains alpha-numeric (alphabet, numbers and punctuation) data enclosed
with double quotation marks.
Integer Data
An integer number is a whole number, without a fractional part For example, 0, −45, and 1278
are integers. Integer numbers are stored in integer variables. BASIC has two types of integer
variables: integer and long integer. A variable name ending in % is an integer variable. Integer
variables can have any whole-number value between −32, 768 and 32, 767. A variable name ending
in & is a long-integer variable and can have any whole-number value between −2, 147, 483, 648 and
2, 147, 483, 647.
Postive: 52, 42, 746
String
A string variable holds a sequence (a string) of characters. A variable name ending in $ is a string
variable. Just as a numeric variable is given an initial value of 0, a newly defined string variable is
assigned an initial length of an empty string. It contains no data at all, not even blank spaces.
For example, if blank$ has not previously been given a value, the statement
PRINT blank$
displays an empty line.
7
Variables
Variables are names given to data stored in memory. Data stored in memory is called objects.
Computer memory is divided into storage locations, each of which has a unique “address”.
Imagine computer memory as a neighbourhood of houses in a row, where each house has a number.
Each house can hold some information. If we have too much information that doesn’t fit in one
house, we spread it out over a few houses next to each other.
For example, if we think of a house as being able to hold 2 people, but we have a family of 6,
we would need 3 houses. If these houses are number 20, 21, and 22, we assign the whole family to
these houses. When it’s time for breakfast, we just need to knock on the first house, number 20,
and the whole family will come along.
In programming with BASIC, we don’t need to remember all these house numbers. We give a
name, or a ”variable name,” to these groups of houses (or memory spots). BASIC automatically
links this name to the right spot in memory. So, when we need to access our information, we just
use the variable name, and BASIC knows exactly where to find it, just like knowing to knock on
house number 20 to call the whole family for breakfast.
Figure 3: The Variable greeting is only associated with the memory block 1231
A variable gets its value through an “assignment” statement. An assignment statement has three
components: the variable that receives a new value; an equal sign (=); and the number, string, or
calculation whose value the variable takes on. These are all valid assignment statements:
age% =41
mynameS = " Willie "
result ! = result3 ! + leftval ! + rightval !
stringl$ = "Hello ,"
string2$ = " there !"
bigstring$ = stringl$ + string2$
Although the assignment is shown by the equal sign, it is not the same thing as algebraic equality.
To BASIC, the assignment statement means: “Perform all the calculations and data manipulations
on the right side of the equal sign. Then give the variable on the left side the result.” The same
variable name can appear on both sides of the assignment statement. This occurs most often when
you want to change the variable’s value.
counter % = 6
PRINT counter %
counter % = counter % - 1
PRINT counter %
6
5
You cannot assign numeric values to string variables (or vice versa). Therefore, the following
statements are illegal. Either one halts execution and causes the error message Type mismatch when
the program runs.
strg$ = 3.14159
number ! = "hi , there"
8
What are the rules for naming variables?
• A variable should start with an alphabet.
• It contains a combination of alphabets, numbers and a period(.).
• No spaces are allowed in a variable name.
• QBASIC keyword cannot be as a variable name.
9
Input and Output Operations
Print Keyword
The print statement also known as the display statement is used to display data in the output
console. The print statement can display both numeric and alpha-numeric data.
SYNTAX
Example
Statement Output
PRINT 15;" AUGUST ";2022 15␢AUGUST␢2022
PRINT 1;2 1␢2
PRINT "A";"B" AB
PRINT "$";10 $␢10
PRINT "I am ";11;" years old" I␢am␢11␢years␢old
1.............2.............3.............4.............5.......................
a e i o u
PRINT "a",,"i",,"u"
1.............2.............3.............4.............5.......................
a i u
PRINT "a",,,,,"u"
1.............2.............3.............4.............5.......................
a
u
10
Input Statement
1. In QBASIC, the INPUT statement is used to accept user input. It prompts the user for input
and stores the value entered by the user into a variable.
2. It is called the interactive assignment statement.
3. It accepts keyboard inputs during the program execution.
4. It temporarily halts the programs and waits for the data entry.
Input delimiter
1. With ; a question mark appears next to the message prompt in the output screen. Enter
your name?
2. With, the cursor blinks right next to the message prompt. Enter your name
3. Input statement without ; or , Following error message appears:
Syntax
11
Example
12
Let Statement
Let Statement is an assignment statement. It helps in storing/assigning a value to a variable.
Example
LET INPUT
Values are given while writing the program. Values are given while executing the program.
Only one value can be assigned to one
variable in a single statement. Multiple values can be assigned to multiple
variables in one statement.
We can perform calculations using the LET
statement. Calculations cannot be performed in Input
statement.
13
Operators and Expressions
An “expression” is what most people would call a formula. An expression is any combination of
numbers, strings, variables, functions, and operators that can be evaluated. For example, 2 + 2 is
a simple expression. It evaluates to four. Here + is an operator and 2 is an operand. The operator
+ is called a binary operator as it requires two operands.
Expressions may be as complex as you like, with the results of one expression passed to a func-
tion, the value returned by a function combined with other expressions, and so on.
Almost any place you can use a number or a string, you can use any expression (no matter how
complex) that evaluates to a number or a string. This means expressions can be used in BASIC’s
math functions, PRINT statements, and assignment statements.
Mathematical Operators
In BASIC, the mathematical operations of addition, subtraction, multiplication, and division are
represented by +, −, ∗ (the asterisk), and / ( the slash ), respectively. Exponentiation ( raising a
number to a power ) is shown by ( the caret ). BASIC also has the integer division that retains the
integer ( whole-number ) part of the division and discards the fractional part. Integer division is
represented by \( the backslash ). Finally, it has the remainder operator which is the complement of
integer division. The whole-number part of the division is discarded, and the remainder is returned.
The remainder division is performed by the BASIC keyword MOD.
Precedence of Operations
BASIC evaluates mathematical expressions from left to right, following the rules of algebraic prece-
dence: exponentiation is performed first, then multiplication and division, then addition and sub-
traction.
PRINT 2*3+2^3 -2/3
PRINT 2 * (3+2) ^ (3 -2/3)
13.33333
85.49879
14
Relational Operators in QBasic:
Numbers and strings are compared using ”relational operators.”. These operators compare values
and return True or False There are six possible relationships, shown by the following operators:
Relational operators are used to compare two values. In programming, especially in languages
like QBasic, these operators play a crucial role in decision-making structures like the IF statement.
The result of a comparison using a relational operator is either TRUE or FALSE. The following list
shows how these relationships are evaluated:
As for the last statement—apples aren’t oranges. But if apples and oranges were variable names,
then the truth or falsity of the statement would depend on the values of these variables. The
following relational operations compare strings. Are they true or false?
" Tarzan " <> "Jane"
"Y"="Y"
"Y"="y"
"miss ">" misty "
Tarzan and Jane are different strings, so the first assertion is true. Y and Y are the same charac-
ter, so the second assertion is true. Y and y are not the same character, so the third assertion is false.
The last statement may be confusing at first; how can one string be ”larger” or ”smaller” than
another? It can be, and the decision is made this way. The strings are first compared, position
by position, until BASIC finds characters that don’t match. In this example, the fourth characters
are the first non-matching characters. The decision of “larger” or “smaller” is then based on these
differing characters, in the following way.
Within the computer, characters are represented by numbers. Letters coming later in the alphabet
have numeric values larger than those appearing earlier. Since t follows s in the alphabet, t is a
“larger” letter, and the statement is therefore false; misty is “larger” than miss. This ability to
determine the relative value of strings makes it easy to write a program that can alphabetize a list
of words or names.
In most of these examples, numbers were compared with numbers and strings with strings. But
any combination of numbers and numerical variables can be compared; the same is true for strings
and string variables. The only things you cannot compare are numerical values with string values
because the comparison is logically meaningless.
15
Condition Evaluation:
When a condition is evaluated using a relational operator, the result is a boolean value: either TRUE
or FALSE.
For example:
• 5 = 5 evaluates to TRUE
• 5 <> 5 evaluates to FALSE
Start
yes
Age ≥ 18 Print ”Eligible to vote”
no
Print ”Not eligible to vote”
End
In the flowchart above, the diamond shape represents a condition that’s being evaluated. If the
condition (in this case, age >= 18) is TRUE, the program takes one path (rightwards), and if it’s
FALSE, it takes another path (downwards).
16
Conditional Statement
1. Conditional Statement which checks for only one condition: IF-THEN- ELSE
2. Conditional Statement which checks multiple conditions: IF-THEN- ELSEIF...
3. If statements are used to check conditions in the program.
4. Also called decision-making statement.
Start
Yes
Is the clue correct? Open Door 1
No
Open Door 2
End
In the game, if the clue is correct, you open Door 1. Otherwise, you open Door 2. In QBasic, we
write this as:
IF clue is correct THEN
PRINT "Open Door 1"
ELSE
PRINT "Open Door 2"
END IF
Just like in our game, the computer checks the clue and decides which door to open!
17
Multiple Conditions
You can combine multiple conditions in a conditional statement. Each condition can be combined
using logical operators like AND, OR and NOT.
Logical Operators
AND Operator
A B A AND B
False False False
False True False
True False False
True True True
As an example
LET NUM = 0
INPUT "Enter a Number :"; NUM
IF NUM MOD 2 = 0 AND NUM MOD 3 = 0 THEN
PRINT " Divisible by 6"
ELSE
PRINT "Not Divisible by 6"
END IF
Enter a Number:? 12
Divisible by 6
Enter a Number:? 10
Not Divisible by 6
OR Operator
A B A OR B
False False False
False True True
True False True
True True True
As an example
LET CHOICE$ = ""
INPUT "Do you want to continue :"; CHOICE$
IF CHOICE$ = "YES" OR CHOICE$ = "yes" OR CHOICE$ = "Yes" THEN
PRINT "Let 's continue "
ELSE
PRINT "Ok Quitting "
END IF
18
NOT Operator
The Not operators invert the condition
A NOT A
False True
True False
As an example
LET CHOICE$ = ""
INPUT "Do you want to quit :"; CHOICE$
IF NOT ( CHOICE$ = "NO" OR CHOICE$ = "no" OR CHOICE$ = "No ") THEN
PRINT "Ok Quitting "
END IF
Example
When Boolean expressions are evaluated, the precedence of evaluation is taken in the same order as
arithmetic expressions, with AND equivalent to multiplication and OR equivalent to addition. Eval-
uation goes from left to right, with all the simple expressions evaluated first. All the AND-connected
expressions are evaluated next, followed by the OR-connected expressions. As with arithmetic ex-
pressions, parentheses can be used to control the order of evaluation, rather than relying on the
rules of precedence.
You can even combine multiple logical operators. Example
LET AGE = 0
LET Gender$ = ""
LET Married$ = ""
INPUT "Enter your age :"; AGE
INPUT "Enter your gender :"; Gender$
INPUT "Are you married [YES|NO ]:"; Married$
IF AGE < 18 AND GENDER = "MALE"
PRINT "Good Morning Master "
IF AGE < 18 AND GENDER = " FEMALE "
PRINT "Good Morning Miss"
ELSEIF AGE >= 18 AND AGE <= 60 AND GENDER = "MALE"
PRINT "Good Morning Mister "
ELSEIF AGE >= 18 AND AGE <= 60 AND GENDER = " FEMALE " AND Married = "NO"
PRINT "Good Morning Ms"
ELSEIF AGE >= 18 AND AGE <= 60 AND GENDER = " FEMALE " AND Married = "Yes"
PRINT "Good Morning Mrs"
ELSEIF AGE > 60 AND GENDER = "MALE"
PRINT "Good Morning Sir"
ELSEIF AGE > 60 AND GENDER = " FEMALE "
PRINT "Good Morning Madam "
END IF
19
When combining multiple conditions, AND takes precedence over OR
LET Weather$ = ""
LET Months$ = ""
INPUT "What is the weather :"; Weather$
INPUT "What is the month :"; Months$
IF WEATHER$ = " SUNNY " AND ( Months$ = " APRIL " OR Months$ = "MAY" OR
Months$ = "JUNE ") OR Weather = " RAINING "
PRINT "NEED an Umbrella "
ELSE
PRINT "NO NEED for an Umbrella "
ENDIF
20
Choosing an Outfit Based on the Weather
Imagine you’re getting ready for school and need to decide what to wear based on the weather
outside.
In QBasic, we can represent this decision-making process as:
IF weather = " raining " THEN
PRINT "Wear raincoat "
ELSEIF weather = " sunny " THEN
PRINT "Wear favorite t- shirt "
ELSEIF weather = " snowing " THEN
PRINT "Wear warm jacket "
ELSE
PRINT "Wear regular shirt "
END IF
Just like in real life, the computer checks the weather and decides what outfit to suggest!
21
Counter
In QBasic, a counter is a variable used to count the number of times a loop has been executed or
to control how many times the loop should execute. It’s a fundamental concept in programming,
especially when dealing with loops where you need to repeat a block of code a specific number of
times or until a certain condition is met.
Here’s a breakdown of how a counter is typically used in QBasic:
In QBasic, we need to use a loop to use a counter. There are 5 different types of loops supported
by QBasic
22
DO WHILE... LOOP
The DO WHILE...LOOP in QBasic is a loop construct that executes a block of code repeat-
edly as long as a given condition is true. The key aspect of the DO WHILE loop is that it
checks the condition at the beginning of the loop before executing the code inside. If the con-
dition is true, the loop executes. If the condition is false, the loop doesn’t execute even once.
10 LET COUNTER = 0
20 DO WHILE COUNTER < 5
30 PRINT COUNTER
40 COUNTER = COUNTER + 1
50 LOOP
60 PRINT "DONE"
0
1
2
3
4
DONE
In the above code, the variable COUNTER is initialized to zero in Line number 10. In line
number 20, the code checks the condition. If the condition is true, the execution enters the loop,
else it skips the block from Line 20 through 50 and starts executing from Line 60. Below is the dry-run
23
DO UNTIL... LOOP
The DO UNTIL...LOOP in QBasic is a loop construct that executes a block of code repeatedly until
the given condition is true. The key aspect of the DO UNTIL loop is that it checks the condition
at the beginning of the loop before executing the code inside. If the condition is false, the loop
executes. If the condition is true, the loop doesn’t execute even once.
10 LET COUNTER = 0
20 DO UNTIL COUNTER >= 5
30 PRINT COUNTER
40 COUNTER = COUNTER + 1
50 LOOP
60 PRINT "DONE"
0
1
2
3
4
DONE
In the above code, the variable COUNTER is initialized to zero in Line number 10. In line number
20, the code checks the condition. If the condition is false, the execution enters the loop, else it skips
the block from Line 20 through 50 and starts executing from Line 60. Below is the dry-run
24
DO ... LOOP WHILE
The DO ... LOOP WHILE in QBasic is a loop construct that executes a block of code repeatedly
while the given condition is true. The key aspect of the DO ... LOOP WHILE loop is that it checks
the condition at the end of the loop after executing the code inside. If the condition is false, the
loop continues. This means the code inside the loop block will execute at least once irrespective of
whether the condition is true or false.
10 LET COUNTER = 0
20 DO
30 PRINT COUNTER
40 COUNTER = COUNTER + 1
50 LOOP WHILE COUNTER < 5
60 PRINT "DONE"
0
1
2
3
4
DONE
In the above code, the variable COUNTER is initialized to zero in Line number 10. It enters
the loop unconditionally. In line number 50, the code checks the condition. If the condition is true,
the execution continues the loop or else if the condition is false it continues executing from Line 60.
Below is the dry-run
25
DO ... LOOP UNTIL
The DO ... LOOP UNTIL in QBasic is a loop construct that executes a block of code repeatedly
until the given condition is true. The key aspect of the DO ... LOOP UNTIL loop is that it checks
the condition at the end of the loop after executing the code inside. If the condition is false, the
loop continues. This means the code inside the loop block will execute at least once irrespective of
whether the condition is true or false.
10 LET COUNTER = 0
20 DO
30 PRINT COUNTER
40 COUNTER = COUNTER + 1
50 LOOP UNTIL COUNTER >= 5
60 PRINT "DONE"
0
1
2
3
4
DONE
In the above code, the variable COUNTER is initialized to zero in Line number 10. It enters
the loop unconditionally. In line number 50, the code checks the condition. If the condition is false,
the execution continues the loop or else if the condition is true it continues executing from Line 60.
Below is the dry-run
26
FOR ... NEXT
The FOR...NEXT loop in QBasic is a fundamental control structure that enables the execution of a
sequence of statements a fixed number of times. This loop is particularly useful when the number of
iterations is known beforehand. The structure begins with the FOR statement, followed by a loop
variable, its initial value, the TO keyword, and the end value. Inside the loop, the code block that
follows is executed repeatedly until the loop variable reaches or surpasses the specified end value.
Upon each iteration, the loop variable is automatically updated by 1. After the loop concludes,
execution continues with the statements following the NEXT keyword. This loop is instrumental in
tasks that require repetitive actions, such as processing arrays, generating repeated output, or iter-
ating through a range of numbers, making it a versatile tool for programmers working with QBasic.
Here’s how the FOR...NEXT loop is structured:
10 FOR COUNTER = 0 TO 5
20 PRINT COUNTER
30 NEXT
40 PRINT "DONE"
In the above code, the block of code inside the FOR LOOP is executed for COUNTER VALUE
starting from 0 until 5. Below is the dry-run
27
FOR ...STEP ... NEXT
The FOR ...STEP ... NEXT loop in QBasic is another variant of FOR ... NEXT loop control
structure that enables the execution of a sequence of statements a fixed number of times. This loop
is particularly useful when the number of iterations is known beforehand including the increment for
each iteration. The structure begins with the FOR statement, followed by a loop variable, its initial
value, the TO keyword, and the end value. Optionally, a STEP value can be specified to determine
the increment or decrement at each iteration. Inside the loop, the code block that follows is executed
repeatedly until the loop variable reaches or surpasses the specified end value. Upon each iteration,
the loop variable is automatically updated by the STEP value. After the loop concludes, execution
continues with the statements following the NEXT keyword. This loop is instrumental in tasks
that require repetitive actions, such as processing arrays, generating repeated output, or iterating
through a range of numbers, making it a versatile tool for programmers working with QBasic. Here’s
how the FOR...NEXT loop is structured:
10 FOR COUNTER = 0 TO 5
STEP 2
20 PRINT COUNTER
30 NEXT
40 PRINT "DONE"
In the above code, the block of code inside the FOR LOOP is executed for COUNTER VALUE
starting from 0 until 5. Below is the dry-run
28
Indentation Best Practices for QBasic
To maximize the benefits of indentation in QBasic, consider the following best practices:
Consistency
Choose an indentation style (spaces or tabs) and stick to it throughout your code. Consistency is
key to maintaining the readability and uniformity of your codebase.
Indentation Level
A common convention is to use two or four spaces for each indentation level. Avoid mixing tabs and
spaces, as this can lead to inconsistent indentation across different editors or viewers.
Code Blocks
Indent the lines of code within control structures such as IF...THEN...ELSE, loops (FOR...NEXT,
DO...LOOP), and subroutine definitions. This visually separates the code blocks from the rest of
the program.
IF condition THEN
' Code to execute if condition is true
ELSE
' Code to execute if condition is false
END IF
Loop Structures
Indent the code within loop structures to highlight the loop’s body, making it clear which statements
are repeated.
FOR i = 1 TO 10
PRINT i
NEXT I
29
Subjective Questions
1. What are QBASIC expressions?
2. What are the types of expressions supported by QBASIC?
Objective Questions
Identify the numeric and String data.
• ”100”
• 100
• ”AB1D”
• ”x % y”
• ”Hello everyone”
• ”1947”
LET X = 5
LET Y = 10
PRINT "The Product is :"; X * Y
3.
2. LET NUM = 50
PRINT "The variable contains "; NUM
LET area = 10 * 2
PRINT "The variable contains "; area
5.
4. PRINT " HAPPY ";" BIRTHDAY "
PRINT 10;20;100
PRINT "10";"20";"100"
PRINT 15;" AUGUST ";1947
6. PRINT1 ;2
PRINT "A" ; "B"
PRINT "A" ; " B"
PRINT 6 ; "th Cross "
PRINT "$" ; 10
PRINT "I am" ; 11 ; " years old"
PRINT " CLASS :" ; 5 ; "A"
PRINT 5 ; "x" ; 3 ; "=" ; 15
PRINT "Bangalore -" ; 560008
PRINT " REBOOT !" ; " " ; "BOOK" ; 5
30
School Programs
1. Write a program to accept and display the names of five students in your class.
2. Write a program to display the numbers below numbers in different zones. Note each number
is of the form 2n − 1
3 8 15 24 48
#
##
###
####
#####
1 2 3 4
1........0.........0.........0.........0
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
1 1 1 1 1 1
1 2 3 4
1........0.........0.........0.........0
1
1 1
1 1 1
1 1 1 1
1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1
1 1 1 1
1 1 1
1 1
1
12 X 1 = 12
12 X 2 = 24
12 X 3 = 36
12 X 4 = 48
12 X 5 = 60
12 X 6 = 72
12 X 7 = 84
12 X 8 = 96
12 X 9 = 108
12 X 10 = 120
31
7. Can you modify the above program to generate the table for any number entered?
8. Write a program that will continue to ask for the input until the user enters either ”YES” or
”NO”?
Enter either [YES|NO]? TRUE
Enter either [YES|NO]? FALSE
Enter either [YES|NO]? YES
DONE!!
10. Write a program to accept a number. Display the multiplication table from the 10th multiple
to the 15th multiple under a heading.
11. WAP to display even numbers from 40 to 80 in different zones.
12. WAP to display the following series using the counter-looping method.
1.............2.............3.............4.............5.............
4 9 16 25 36
49 64
13. Write a program to accept 10 numbers and display their doubles using the counter-looping
method.
14. WAP to accept a number and print its square. Continue the process until the user enters 0 or
a negative number
15. WAP to print first 10 natural numbers using While .... Wend
16. WAP to accept 7 numbers. Find and display the highest number
17. WAP to accept a 4 digit number. Terminate if the number is not a 4 digit number. Display
the sum of the accepted numbers.
18. WAP to accept 10 numbers. Exit the Loop if the User enters 0 or a negative number.
32
19. WAP to accept 10 numbers. Count the number of odd and even numbers. Display the result.
20. WAP to accept numbers in a loop. Count the number of odd and even numbers. Exit the loop
of the use enters zero or negative number. Display the result.
21. WAP to accept a number. Display whether it is prime or not.
22. WAP to display the multiples of 5 upto 50 using While ... Wend.
23. WAP to display the numbers from 10 to 1 in reversed order.
24. WAP to accept 5 numbers and then display its product.
25. WAP to display alternate odd numbers from 20 to 100.
29. WAP to accept the student and the house name in your class. Count students belonging to
each house.
30. WAP to reverse any number entered by user
31. WAP to find if a number entered is palindrom or not.
33
33. Write a program to display the names of our schoolhouses and their corresponding colours
under appropriate headings.
34. Write a program to display the sum, difference, product and quotient of 12 and 2 in the
following format:
35. Write a program to display the colours of a traffic signal with their meanings one below the
other under a heading.
36. Write a program to display the first 5 odd numbers one below the other and the first five even
numbers in different zones.
37. Write a program to store your roll number in a numeric variable and your name in a string
variable. Display the details one below the other using the variables.
38. Write a program to accept your name and age. Display in the following format.
39. Write a program to accept two numbers. Calculate and store the sum and product of the
numbers. Display all the details.
40. Write a program to accept a number. Display the multiplication table in the proper format
under a heading from 5th to 8th multiple.
41. Write a program to accept a number. Display if the number is a positive number, negative
number or zero.
42. Write a program to accept colour. Display the significance of the colour in a traffic light.
43. Write a program to accept two numbers and an arithmetic operator. Based on the operator,
perform the arithmetic calculations.
44. Write a program to accept the values of length and breadth of the rectangle. Depending on the
user’s choice, display either the area or the perimeter of the rectangle. Display an appropriate
message if the user enters the wrong choice.
45. Write a program to accept a colour. Display whether it is one of the primary colour otherwise
display an appropriate message.
46. Write a program to accept a number. Display whether it’s a positive number or not.
47. Write a program to accept the name of a house from NHPS. Display whether you belong to
that house or not.
48. Write a program to accept a number and display if the number is even or odd.
49. Write a program to accept two different numbers. Display the greater number of the two.
50. Write a program to accept a section. Display whether the user belongs to your section or not.
51. Write a program to accept two numbers. Display the sum of the numbers if they are equal
otherwise display the product.
52. Write a program to accept the age of a person and display if the person can vote or not. (Note:
Voting age is 18 and above)
53. Write a program to accept a number and check if it is a single digit ,double digit or triple digit
number
54. Accept an age and display whether the user is a minor or an adult
55. Accept a number and display if its even or odd
56. Accept the user’s school name . If the School name is “ New Horizon Public School “display
the school motto , otherwise display an appropriate message
57. Display a message and clear the screen
58. Accept a state of matter . Depending upon the input , display a relevant example
59. Write a program to input a number from 1- 7 and display the day of the week based on the
number accepted
34
60. Write a program to accept a school house name and colour. If both match, display the
corresponding house song
61. WAP to accept a two digit numbers and display the sum and product of the two digits
62. WAP to accept a 2 digit number and print the number in reverse order
63. WAP to accept two numbers and an arithmetic operator. Display corresponding arithmetic
operation based on the arithmetic operator
64. Accept a positive integer and display if it is divisible by 6,15 or both
65. Accept an year and check if it is a leap year
35
Revision
Question 1
Fill in the blanks
1. The outcome of a condition always results to either or .
2. The Keyword CLS stands for .
3. Each line of code in a statement is called a .
4. The operator used to perform mathematical operations between numbers are called
operators.
5. QBASIC is divided into two screens namely screen and screen.
6. Multiple IF statement in a single block is called
Question 2
Name the following and write their syntax.
1. INPUT.
2. LET
3. PRINT
4. IF ... THEN ... ELSE.
Question 3
Name the following and write their syntax.
1. INPUT.
2. LET
3. PRINT
4. IF ... THEN ... ELSE.
Question 4
Answer the following questions
1. Write one similarity between LET and INPUT statement.
2. Write the symbols and name of any four relational operator.
3. Write any two rules of naming a variable.
4. Define keyword and syntax.
5. Name two types of variables and two types of constants.
Question 5
Write one QBasic statement for each of the following.
1. Increase the value of A by 10 and store it in A.
2. Assign the value of Y$ to X$.
3. Check whether the user is as old as you or younger. assume the user’s age is stored in a variable
’age’.
4. Display Q and Basic without leaving any space between them.
5. Write the expression to divide 10 and 3 and store only the integer part of the quotient in a
variable.
36
Question 6
Write the output of the following programs
1. Print ”Program to show concatenation”
LET firstword$ = " Computer "
LET secondword$ = " Science "
PRINT firstword$ + secondword$
Question 7
Fill in the blanks to complete the program
1. CLS
INPUT " Enter the name of the item "; N$
INPUT " Enter the number of items ";P
INPUT " Enter the cost of the item ";
LET M=P*Q
PRINT "Name of the item "; _____________
PRINT " Total cost of "; _____________ ; " number of items =";
Question 8
Write the following programs
1. The employees of a firm are classified into 4 grades (1,2,3,4) 61 and salary is computed using
the following format.
12000 x (5-A)+550 x B+200.
Where A denotes the grade and B denotes the number of years of experience. Write a program
to accept the name of the employee, grade and years of experience using appropriate message.
Display the salary of the employee in the following format.
Example :The salary of Jack is
2. In an examination, 80% of students passed in English and 70% passed in Maths. Write a
program to find the number of students who failed in English and the number of students who
failed in Maths taking the total number of students in the class as input.
Question 9
Write any four of the following programs:
1. Prompt the user to enter an abbreviation from a given list.
Write a program to display the message based on the abbreviation.
Display the message “Unknown”, if any other abbreviation is entered.
The abbreviation list is shown below:
TC Take Care
FYI For Your Information
HAND Have A Nice Day
37
2. A garment shop has announced end-of-season discounts on the purchase of items, based on the
total cost of the items bought.
Write a program to accept the total cost and display the payable amount after availing the
discount.
3. Air quality index (AQI) tells you the quality of air. Write a program to accept the AQI for
today. Display the message according to the following conditions.
AQI Message
Less than or equal to 50 Air Quality is good
Between 51 to 100 Air Quality is Satisfactory
Between 101 to 200 Polluted
Above 200 Alarming
4. Write a program to accept user’s height in meters and weight in kg. Display the name of the
user, BMI and the appropriate message after calculating the BMI based on the given formula.
BMI is body Mass Index
BMI=Weight in Kg / (Height in meter)
BMI Message
Less than 18.5 Under weight
18.5 to 24.9 Ideal weight
25 to 29.9 Over weight
Greater than 29.9 1t is high time to take corrective measures
5. (Write a program to accept the measurement of an angle. Display whether the angle is acute,
obtuse or right angle.
Angle Message
Greater than 0 and less than 90 Acute
Equal to 90 Right angle
Greater than 90 and less than 180 Obtuse Angle
6. Write the QBASIC program to accept the age and height of the user in feet and inches. Convert
the given height into cms.
• 1 foot = 12 inches
• 1 inch = 2.54 cm
7. Write QBASIC program to accept marks of English I and English II. [Display error if the
marks are not in the range 0-50. Calculate and store total, average and percentage marks.
Display all the details.
8. Armstrong number: The sum of the cubes of the digits is equal to the number. Example:
• Input: 371
• Calculation: Sum of the cubes of the digits = 33 + 73 + 13 = 371
• Output: 371 is an Armstrong number
9. WAP to swap two numbers using a third variable
38
10. Write a program to accept a 3 digit number and check whether it is a spy number or not. A
number is said to be a spy number if the sum of all digits is equal to the product of the digits.
11. Write a program to accept a 4-digit number and print whether it is a Tech number or not. If
the number is split into two equal halves, then the square of the sum of these halves is equal
to the number itself.
Input: Enter a 4 digit number: 3025
Calculation: Square of the sum of the halves of 3025 = (30 + 25)2
= 552 = 3025
Output: 3025 is a Tech number
12. Write a program to accept any two values. Swap the values using a third variable. Display
the values before and after swapping.
13. Write a program to accept any two values. Swap the values without using a third variable.
Display the values before and after swapping.
14. Write a program to accept the height in feet and inches. Convert the total height into cen-
timetres.
1 feet = 12 inch
1 inch = 2.54 cms
15. Write a program to accept the height in feet and inches. Convert the total height into cen-
timetres.
39