0% found this document useful (0 votes)
4 views107 pages

Lecture Slides 2

The document provides an overview of programming in QBASIC, focusing on operators, statements, and input/output methods. It details mathematical, relational, and logical operators, as well as common QBASIC statements like LET, PRINT, and INPUT. Additionally, it includes programming examples illustrating the use of input statements and output formatting.
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)
4 views107 pages

Lecture Slides 2

The document provides an overview of programming in QBASIC, focusing on operators, statements, and input/output methods. It details mathematical, relational, and logical operators, as well as common QBASIC statements like LET, PRINT, and INPUT. Additionally, it includes programming examples illustrating the use of input statements and output formatting.
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
You are on page 1/ 107

PROGRAMMING IN QBASIC

Operators in BASIC
An operator is a symbol which tells the computer to perform certain mathematical and
logical calculation.

Types of operator

Mathematical Operators
1.Relational Operators
2.Logical Operators
1. Mathematical Operators

Opetrator Meaning Example


+ addition 2+2
- subtraction 5+2
* multiplication 2*2
/ division 2/2
Integer division (It gives
\ 2\2
the integer value)
modulus (It gives
MOD 3 MOD 2
remainder)
^ exponent 2^2
2. Relational Operator

Opetrator Meaning Example


< less than 2<3
> greater than 5>2
<= less than or equal to 2<=3
>= greater than or equal to 2>=2
= equal to 2=2
<> not equal to 3 <> 2

3. Logical Operator

Opetrator Meaning Example


AND Logical AND

OR Logical OR

NOT Logical NOT


QBASIC Statements
A statement is a computer instruction written in a source language, such as QBASIC, which
is converted into one or more machine code instructions by a compiler.

The commonly used QBASIC statements are: LET, PRINT, INPUT, CLS, END and REM. In
QBASIC, the LET statement is used to assign a value to a variable. The PRINT command is
used to display any message or value on the screen. The INPUT statement in QBASIC is used
to accept the data item from the user. REM statement is a non-executable statement and
stands for remarks. CLS statement is used to clear the screen. END statement is used to end
the program execution.
INPUT STATEMENTS
These are statements that are
used for feed/supply data unto
the computer system.

• Two Types:
• Input Method
• Read & Data Method
INPUT STATEMENTS cont’d
INPUT Method
This is used where the program given is a
closed problem (e.g. values not specified).
e.g. INPUT A
OR
INPUT “Enter value for A: ”, A
Here the value of A is expected to be inputted.
cont’d
Programming Example
Code in QBasic to solve:
(i) Z = x2 + w
y
(ii) F = b2
4ac
INPUT STATEMENTS – Illustration I
Solutions
5 CLS
10 REM
15 INPUT “Enter value for X: ”, x
20 INPUT “Enter value for W: ”, w
25 INPUT “Enter value for Y: ”, y
30 Let Z = ((x*x) + (w/y))
35 PRINT “The answer is: ”, Z
40 END
INPUT STATEMENTS – Illustration I - Output
INPUT STATEMENTS – Illustration II
Solutions
5 CLS
10 REM
15 INPUT “Enter value for A: ”, a
20 INPUT “Enter value for B: ”, b
25 INPUT “Enter value for C: ”, c
30 Let F = ((b^2) / (4*a*c))
35 PRINT “The answer is: ”, F
40 END
INPUT STATEMENTS – Illustration II - Output
INPUT STATEMENTS cont’d
READ Method
This is used where the program given is an open problem (e.g.
values specified).

e.g. READ A
DATA 7
OR
READ A, B, C
DATA 7, 15, 87

Here there is no need using INPUT to enter the value of A


because it is already given.
cont’d
Programming Example
Given:
t = 15, u = 210, s = 14, r = 11
Code in QBasic to compute:
(i) V = t + (u – r)
(ii) W = u + s3
t2 t2
INPUT STATEMENTS – Illustration I

Solutions
5 CLS
10 REM
15 READ t, u, r
20 DATA 15, 210, 11
30 Let V = (t + (u-r))
35 PRINT “The answer is: ”, V
40 END
INPUT STATEMENTS – Illustration I - Output
INPUT STATEMENTS – Illustration II

Solutions
5 CLS
10 REM
15 READ u, s, t
20 DATA 210, 14, 15
30 Let W = ((u/(t^2)) + ((s^2)/(t^2)))
35 PRINT “The answer is: ”; W
40 END
INPUT STATEMENTS – Illustration II - Output

Did you notice any different in the outputs (this slide page 42 – using PRINT with
Semicolon ; and the previous one, slide page 40 – using PRINT with Comma ,?
OUTPUT STATEMENTS
These are statements that are used
to fetch/retrieve/show to screen,
inputted or stored data from the
computer system.

• The major/basic output


statement used in QBasic is called
PRINT. Others will be treated
under FILE.
OUTPUT STATEMENTS – Usage & Format
PRINT can be used in two ways:
i. For outputting literals e.g.
PRINT “I am a programmer”

ii. For outputting variables (values


of an identifier) e.g.
PRINT radius
(where radius is a variable having a
particular value that has previously
been inputted /captured / stored)
OUTPUT STATEMENTS – Illustration I
5 CLS
10 REM
15 PRINT “I am a Programmer”
20 END

Press F5 to RUN

OR

Click on RUN and click on START


RUN well? Good! Save your work now!! Click on FILE, then click
on SAVE. Remove the highlighting and save accordingly. Please
ensure you preserve the .bas, it is the filename extension for
QBasic, just as .doc is to MS-Word.
OUTPUT STATEMENTS – Illustration I cont’d
See the output below:

RUN well? Good! Press any KEY to return to the Coding


Screen. Save your work now!! Click on FILE, then click on
SAVE. Remove the highlighting and save accordingly. Please
ensure you preserve the .bas, it is the filename extension
for QBasic, just as .doc is to MS-Word.
OUTPUT STATEMENTS – Illustration I cont’d
To save:
OUTPUT STATEMENTS – Illustration I cont’d
Filename Giving:

Use the Arrow Key to remove the highlighting. Thereafter, use


the left arrow key to move under the DOT, then use backspace to
delete UNTITLED and put in/type in your own desired filename
(e.g. programmer). See the next slide.
OUTPUT STATEMENTS – Illustration I cont’d
This is what you should now have:

Click on OK or Press ENTER to complete the SAVE process.


OUTPUT STATEMENTS – Illustration I cont’d
To start another/new program, click on FILE, then on NEW
OUTPUT STATEMENTS – Illustration I cont’d
You can start a new/fresh program codes now!
OUTPUT STATEMENTS – Illustration II
5 CLS
10 REM
15 INPUT radius
20 PRINT radius
25 PRINT radius * 2
30 END

Press F5 to RUN

OR

Click on RUN and click on START.


OUTPUT STATEMENTS – Illustration II cont’d
You will first see something like this, prompting you to
input/supply value for radius.
OUTPUT STATEMENTS – Illustration II cont’d
…do the inputting now…and press the ENTER key.
OUTPUT STATEMENTS – Illustration II cont’d
…the system responds thus:

To go back to the coding screen, obey the


instruction: ‘Press any key….’

You might also like