0% found this document useful (0 votes)
3 views84 pages

QBasic Notes

The document provides an overview of QBASIC programming, including definitions of key concepts such as computer programs, statements, and syntax. It explains the features of QBASIC, types of data, keywords, and the use of variables, LET, and INPUT statements. Additionally, it includes examples of programs and conditional statements to illustrate practical applications of QBASIC.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views84 pages

QBasic Notes

The document provides an overview of QBASIC programming, including definitions of key concepts such as computer programs, statements, and syntax. It explains the features of QBASIC, types of data, keywords, and the use of variables, LET, and INPUT statements. Additionally, it includes examples of programs and conditional statements to illustrate practical applications of QBASIC.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

QBASIC

PROGRAMMING
QBASIC Notes
▪ Computer Program:
A computer program is a set of instructions given
to a computer to perform a specific task.

▪ Statement:
An instruction / command given to a computer by
a computer program is called a statement.

▪ Syntax:
The correct way of writing QBASIC statements is
called syntax.
▪ BASIC is a high level programming language which
stands for:
Beginner’s All-purpose Symbolic Instruction Code.

▪ BASIC was developed by J. G. Kemeny and


Thomas Kurtz in 1964.

▪ Some of the versions of BASIC are :


QBASIC, GWBASIC, Visual BASIC, etc.
Features of QBASIC:

 QBASIC
uses simple English like words and
mathematical symbols.
 The QBASIC commands are called keywords.
 QBASIC works as an interpreter because it converts
one statement at a time into machine code.
▪ The QBASIC application has two screens:

Editor screen Output screen


– The place where we type the Qbasic instructions/statements.
EDITOR – Every statement is typed on a new line on the editor screen.
SCREEN – All the reserved keywords are automatically capitalised.
 This is the screen where we see the output for the program.
OUTPUT  We can see the output screen after pressing the F5 function key on the keyboard.
SCREEN  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
Examples of QBASIC keywords:

 Cls
 Print
 Rem
 Let
 Input
CLS keyword
 CLS stands for Clear Screen
 It is an instruction which helps to clear any previous data
from the output screen.
 Its is normally used as the first command in a program.
 SYNTAX:
CLS
PRINT keyword

 PRINT statement is called as a DISPLAY Statement or an


OUTPUT statement.
 It is an instruction which helps to display data on the
output screen.
 It can be used to display numbers as well as text data.
PRINT keyword
 SYNTAX:
PRINT data/instructions

 Examples:
Examples: Outputs:

PRINT 100 100

PRINT 20 + 50 + 60 130

PRINT 60 * 2 120

PRINT “NHPS” NHPS

PRINT “My School's name is NHPS” My School's name is NHPS


Let’s type these
statements in the
Qbasic editor…….
Thank You
QBASIC
AGENDA

 Recap
 Types of Data in QBASIC
 Print statement with semicolon
RECAP

What is the syntax of PRINT statement?


PRINT data/expression

Let us now learn more


about Data
DATA IN QBASIC

 Data in QBASIC can be of 2 types

Data

String Numeric
data data
NUMERIC DATA

Numeric data means numbers. It can be a


❖positive number
Eg: 4 , 23 , 756

❖Negative number
Eg: -3 , -341 , -89

❖Decimal point number


Eg: 2.9 , 46.3 , 97.5
STRING DATA

 String data can be a combination of letters, numbers and special characters. It


should always be enclosed within double quotes “”.
Eg:
❖“one”
❖“4”
❖“%”
❖“5th”
❖“@[Link]”
❖“100%”
❖“#202G”
RECAP

Write a QBASIC statement for each of the following


1. Display today’s date.
PRINT 3
2. Display the name of the current month.
PRINT “December”
3. Display the current year.
PRINT 2020
In all the above examples, observe that only one value or one data item is printed in each statement

Name the data item in each of the above statements and its type.
PRINT STATEMENT WITH SEMICOLON ( ; )

To display more than one value on a line, we must separate each value with a
semicolon or a comma.
A semicolon in PRINT statement displays the data items next to each other.

Example: Note: There are 3 data items in


Write a qbasic statement to display todays date. the statement given. What are
they?
PRINT 3;”December”;2020

Note: While typing a statement in QBASIC


The above statement will display the output as follows: if we forget to put a semicolon between
two data items then QBASIC will generate
a semicolon automatically. But we must
3 December 2020 not miss the semicolon while writing in the
notebook.
What happens when we execute a PRINT
statement with semicolon?
Observe the following statement
PRINT 4;9
If we press F5 key after typing the above statement, then 4 is displayed first
and the cursor waits next to 4 in the same line as shown below
4
After that the next value is displayed at the position of the cursor as shown
below
4 9
WRITE AND EXECUTE THE FOLLOWING
STATEMENTS IN QBASIC AND WRITE THE
OUTPUT
1) PRINT 1 ; 2
2) PRINT “A” ; “B”
3) PRINT “A” ; “ B”
4) PRINT 6 ; “th Cross”
5) PRINT “$” ; 10
6) PRINT “I am” ; 11 ; “years old”
7) PRINT “CLASS:” ; 5 ; ”A”
8) PRINT 5 ; “x” ; 3 ; “=” ; 15
9) PRINT “Bangalore-” ; 560008
10) PRINT “REBOOT!” ; “ ” ; “BOOK” ; 5
THANK YOU
QBASIC

Programs
AGENDA
• Recap
• Programs
RECAP
Name the following:

❖A set of instructions given to a computer to perform a


program
particular task is called a ___________.
statement
❖Every line in a program is called a ______________.
PROGRAMS
Write a program to display the name of our school houses
and their corresponding colours under appropriate headings

PRINT “Houses”,”Colours”
PRINT “Agni”,”Orange”
PRINT “Prithvi”,”Red”
PRINT “Vayu”, “Purple”
PRINT “Jal”, “Blue”
Write a program to display the following pattern
* * * * *
* * *
*

Print “*”,”*”,“*”,”*”,“*”
Print , “*”,”*”,“*”
Print ,, “*”
Write a program to display the sum, difference, product and
quotient of 12 and 2 in the following format:
12+2= 14
12-2= 10
12x2= 24
12/2=6

Print “12+2=“;12+2
Print “12-2=“;12-2
Print “12x2=“;12*2
Print “12/2=“;12/2
Thank
you
QBASIC
Recalls the use of Semi-colon(;) and Comma (,)
Using the Concatenation operator with Strings.
Definition of a program.
Executes simple programs with PRINT.
Recall

 The symbol used to place data items next to each other is


;
____.
 5
The QBASIC output screen is divided into ___________
zones.
 The symbol used to place the data items in different zones
,
__________.
RECAP
 What will be the output for the following Print statements in QBASIC?
1. PRINT 1 ,, 3 ,, 5
 1 3 5
2. PRINT 1 ; 3 ; 5
 1 3 5
3. PRINT “40+50-” ; 40 + 50
 40+50- 90
4. PRINT 10, 20 , , 30 , 40, 50
 10 20 30 40
50
5. PRINT “Two Multiples” ; “ of number 5 are-” , 5 ; 5 * 2
 Two Multiples of number 5 are- 5 10
String Operator (+) in QBASIC
 String operator joins two or more String data.
 The plus(+) sign is used as the String Operator.
 The act of combining two strings is called Concatenation.
 For example:
Print “50” + “45” gives the output as 5045.
Write outputs for the following: (Statements using String Operator)
1. PRINT “Amuda-” + “Vijay”
Amuda-Vijay
2. PRINT “Wonder” + “Woman”
WonderWoman
3. PRINT “43” + “34”
4334
Recall: What is a computer program?
 A computer program is a set of instructions given to a computer
to perform a specific task.
 So, when multiple statements are written in QBASIC, it is called as
a program.
QBASIC Programs.
1. Write a program to display the colours of a traffic signal with
their meanings one below the other under a heading.
Program:
Print “Colours of a Traffic signal”
Print “Red – Stop”
Print “Yellow – Proceed with caution”
Print “Green – Go”
QBASIC Program
2. Write a program to display the first 5 odd numbers
one below the other and the first five even numbers in
different zones.
Program:
Print 1
Print 3
Print 5
Print 7
Print 9
Print 2, 4, 6, 8, 10
QBASIC

• Variables
• LET statement
RECAP:
 Torun/execute a Qbasic program, we press
the function key F5
A Qbasic program is executed sequentially,
line by line.
 Each line should begin with a keyword.
 Keywords learnt: CLS, PRINT
 PRINT is called as display/output statement
VARIABLE

A variable is a name given to a memory


location where a data can be stored.
 Sincewe have numeric data and string data,
variables are also of two types:

variable

Numeric variable String variable


Numeric VARIABLE: String VARIABLE:

A numeric variable can A string variable can


store only numeric data. store only string data.
 The default value of a  The default value of a
numeric variable is 0. string variable is Null.
 Does not have any $ A string variable always
symbol ends with the $ symbol
 Examples: Num, A1, n10  Examples: name$, c$, YR$

Note: One variable can hold only one value.


RULES FOR NAMING A VARIABLE:

 Variable name should always start with a letter


 It can be a combination of alphabets, digits and a period(.)
 It cannot have spaces in between
 It cannot be a Qbasic keyword
 A variable name can be upto 40 characters long.
LET STATEMENT:

 LET statement is called an assignment statement.


 LET statement helps in storing/assigning a value in a variable.
SYNTAX OF LET STATEMENT:

LET variable = data / expression / variable

Optional keyword Assignment operator/


Assignment symbol

Examples:
LET Num = 10
Let A1 = 100.5
LET name$ = “NHPS”
LET C$ = “red”
LET B10$ = “Ben”
PROGRAMS WITH LET STATEMENT:
Q1. 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.

PROGRAM:
CLS
LET r = 10
LET n$ = “Charles”
Print “NAME : “ ; n$
Print “Roll No : “ ; r

OUTPUT:
NAME : Charles
Roll No : 10
THANK YOU
QBASIC
PROGRAMMING
Introduction to INPUT
Input statement:

• It is called as the interactive assignment statement.


• It accepts keyboard inputs during the program execution.
• It temporarily halts the programs and waits for the data entry.
• Syntax:
INPUT “Message Prompt” ;/, List of variables
separated by comma
• Examples:
Input “Enter your name” ; N$
Input “Enter three numbers” ; P, Q, R
Input “Enter your name and age” , Name$ , A
Difference between LET and INPUT
LET INPUT

1. Values are given while writing the Values are given while executing
program. the program.

2. Only one value can be assigned to Multiple values can be assigned to


one variable in a single statement. multiple variables in one statement.

3. We can perform calculation using Calculations cannot be performed in


LET statement. Input statement.
Using Input and Possible errors with Input statement!!

• Input with semicolon(;) or comma(,)


– With ; a question mark appears next to the message prompt in the output
screen.
Example: Enter your name?
– With , the cursor blinks right next to the message prompt.
Example: Enter your name
• Input statement without ; or ,
– Following error message appears:
• Only Input without prompt:
Input N
– A question mark appears with the cursor on the output screen.
?
• Input with multiple variables
– All variable should be separated by comma.
Example: Input "Enter 3 numbers"; a, b, c
• Without comma(,) in between the variables.
– Gives an error message
Programs with INPUT
1. Write a program to accept your name and age. Display in the
following format.
My name is _______ and I am _______ years old.
Program:
CLS
Input "Enter your name"; name$
Input "Enter your age"; age
Print "My name is "; name$; " and I am "; age; "years old.
Programs with INPUT
2. Write a program to accept two numbers. Calculate and store the
sum and product of the numbers. Display all the details.
Program:
CLS
Input "Enter two numbers"; N1, N2
Let S = N1+N2
Let P = N1*N2
Print "The numbers: "; N1, N2
Print "The Sum and Product:"; S, P
Programs with INPUT
3. Write a program to accept a number. Display the multiplication table in the
proper format under a heading from 5th to 8th multiple.

Program:
CLS
Input "Enter a number"; Num
Print "Multiplication table of"; Num
Print Num; "x"; 5; "="; Num * 5
Print Num; "x"; 6; "="; Num * 6
Print Num; "x"; 7; "="; Num * 7
Print Num; "x"; 8; "="; Num * 8
QBASIC
PROGRAMMING
RECAP
◈ Conditional Statement which checks for only one condition: IF-THEN-
ELSE
◈ Conditional Statement which checks multiple conditions: IF-THEN-
ELSEIF…
◈ Syntax of Elseif:
If <condition> THEN
<True statement>
Elseif <condition> THEN
<True statement>

Else
<False statement>
End if
IF-THEN-ELSEIF statement
◈ Write a program to accept a number. Display if the number is a
positive number, negative number or zero.
Program:
Input “Enter a number:”; N
If N >0 THEN
PRINT N; “is a positive number”
ElSEIF N < 0 THEN
PRINT N; “is a negative number”
ELSE
PRINT “ The number is Zero”
END IF
◈ Write a program to accept colour. Display the significance of the colour
in a traffic light.
Program:
Input “Enter a colour”; C$
IF C$=“red” THEN
PRINT C$; “ signifies stop”
Elseif C$=“green” THEN
PRINT C$; “ signifies go”
Elseif C$=“yellow” THEN
PRINT C$; “ signifies proceed with caution”
Else
Print C$; “ does not belong to traffic light”
End if
IF-THEN-ELSEIF statement
Write a program to accept two numbers and an arithmetic operator. Based on the operator,
perform the arithmetic calculations.
Program:
Input “Enter two numbers and an arithmetic operator:”; A, B, Op$
IF Op$=“+” THEN
S = A +B
PRINT “Sum:” ;S
ELSEIF Op$ = “*” THEN
P=A*B
PRINT “Product:”; P
ELSEIF Op$ = “-” THEN
D=A–B
PRINT “Difference:” D
ELSEIF Op$ = “/” THEN
QD = A / B
PRINT “Quotient with decimal” ; QD
ELSEIF Op$ = “\” THEN
Q=A\B
PRINT “Quotient without decimal” ; Q
ELSEIF Op$ = “MOD”
R = A MOD B
PRINT “Remainder”; R
ELSEIF Op$ = “^” THEN
C= A ^ B
PRINT “Exponent: “; C
ELSE
PRINT “Invalid Operator”
END IF
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.
Program:
Input "Enter the length and breadth of a rectangle"; L, B
Input “Enter your choice: a for area/ p for perimeter”; C$
IF C$ = “a” THEN
A=L*B
PRINT “Area of the rectangle”; A
ELSEIF C$ = “p” THEN
P = 2 * (L + B)
PRINT “Perimeter of the rectangle”; P
ELSE
PRINT “Invalid Choice”
END IF
Write a program to accept a colour. Display whether it is one of the
primary colour otherwise display an appropriate message.

Program:
Input “Enter a colour”; C$
IF C$ = “red” THEN
PRINT C$; “ is a primary colour.”
ELSEIF C$ = “green” THEN
PRINT C$; “ is a primary colour.”
ELSEIF C$ = “blue” THEN
PRINT C$; “ is a primary colour.”
ELSE
PRINT C$; “ is not a primary colour.”
END IF
QBASIC
PROGRAMMING
Recap Of QBASIC:
• Programs using Print, Let and Input.
• Use of Arithmetic operators (+, -, *, /, \, MOD)
• Program using Caret operator.
OPERATOR SYMBOL NAME OPERATION
+ PLUS SUM

Recap of -
*
HYPHEN(MINUS)
ASTERISK
DIFFERENCE
P RODUCT
Arithmetic / FORWARD SLASH QUOTIENT WITH DECIMAL
Operators \ BACK SLASH QU OTI E NT W I TH OU T D EC I MA L

MOD MODULUS REMAINDER


^ CARET EXP ONENT
The expression signs (relational operators) are:
Operator Meaning Example
< Less than 2<3

Relational <= Less than or equal to 2<=3


Operators
These operators > Greater than 5>2
compare values
and return True >= Greater than or equal to 2>=2
or False
= Equal to 2=2

<> Not Equal to 3<>2


IF- THEN – ELSE Statement (Conditional Statement)
• If statements are used to check conditions in the program.
• Also called decision making statement.
• The If...Then...Else statement conditionally executes a statement or group
of statements when the condition evaluates to True and executes another
set of statements if the condition evaluates to False.
SYNTAX of IF-THEN-ELSE
IF condition THEN
< True statement >
ELSE
< False statement >
END IF
Qbasic Programs (If-Then-Else)
1. Write a program to accept a number. Display whether it’s a positive
number or not.

Program:
Rem Program using > (greater than) operator
Input “Enter a number-”;N
If N > 0 Then
Print N; “is a positive number”
Else
Print N; “is not a positive number”
End if
QBASIC
PROGRAMMING
RECAP OF LAST CLASS

• Relational Operators:
• = , > , >= , < , <= and <>
• Conditional Statement:
• If-Then-Else statement.
• Syntax of If-then-else statement:
If <condition> Then
<True statement>
Else
<False statement>
End if
RECAP OF LAST CLASS – FILL IN THE BLANKS

Write a program to accept the name of a house from NHPS. Display whether you belong
to that house or not.

Program:
H$
Input “Enter the name of a house from NHPS-”; ____
Agni
If H$ = “___________” Then
H$ ; “ is my house name.”
Print ____
Else
________
Print H$; “ is________________________.”
______ not my house name
End if
IF-THEN-ELSE (CONTINUED)

Write a program to accept a number and display if the number is even or


odd.
Program:
Input “Enter a number-”; Num
If Num MOD 2 = 0 Then
Print Num; “is an even number.”
Else
Print Num; “is an odd number.”
End if
IF-THEN-ELSE (CONTINUED)

Write a program to accept two different numbers. Display the greater number of the
two.

Program:
Input “Enter two numbers-”; N1, N2
If N1 > N2 Then
Print N1; “ is the greater number”
Else
Print N2; “ is the greater number”
End if
Reinforcing how IF-THEN-ELSE works.
RECAP: WRITE THE OUTPUT FOR THE FOLLOWING PROGRAMS:

a) Let x = 5 b) Let x = 10
IF x = 5 THEN IF x = 5 THEN
INPUT “your name:”;name$ INPUT “your name:”;name$
PRINT name$ PRINT name$
Else Else
Print x * 3 Print x * 3
END IF END IF

Output: Output:
your name:? Raj 30
Raj
IF – THEN - ELSE PROGRAMS: CONTINUED

Write a program to accept a section. Display whether the user belongs


to your section or not.
Program:
Input “Enter a section:”; S$
If S$ = “ A “ Then
Print S$; “ is my section.”
Else
Print S$; “ is not my section.”
End if
IF – THEN - ELSE PROGRAMS: CONTINUED

Write a program to accept two numbers. Display the sum of the numbers if they
are equal otherwise display the product.

Program:
Input “ Enter two numbers:”, A, B
If A = B Then
Print “The Sum:”; A + B
Else
Print “The Product:”; A * B
End if
IF – THEN - ELSE PROGRAMS: CONTINUED

WAP to accept the age of a person and display if the person can vote or not.
(Note: Voting age is 18 and above)

Program:
Input “Enter your age”; Age
IF Age >=18 THEN
PRINT “Can Vote.”
ELSE
PRINT “Cannot Vote.”
END IF

You might also like