CSC 112 - Lecture 3

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

CSC 112 – LECTURE THREE

Running a BASIC program

Programs can be written, edited, saved and run on a computer. Also, there are
methods to correct errors within the program. The process of identifying errors in a
computer program and correcting such is called Debugging.

For this class, we will consider running a BASIC program on a mainframe


computer, which is a large computer, running in a time-sharing environment.

I. Logging in
First, the computer has to identify whoever wants to gain access to the
computer and so, the user is prompted for a username and password. This is
after connection has been established. The use of commands is quite
necessary here. The terminal displays > for prompting the user for the next
set of information to be entered into the computer.
Logging in can be done with the use of any of the LOGIN, LOGON or LOG
commands.

For instance, after the connection is established, the computer responds:

PLEASE LOGIN

This continues and the user keeps interacting with the computer with the use
of commands to supply the required information to the computer.

II. Entering a program


Once a connection has been established and a user logged in, the program
may be typed into the computer one line at a time. The user must wait to be
prompted by the computer for the next line of code.
The user enters a line of code in the same way- line number statement
After the line of code is entered, the user has to hit the “Enter” key for the
code to be transmitted into the computer. After transmission, the computer
prompts the use with the > symbol. The user then enters the next line of
code. This continues until the last statement, then, the user can enter a
command specifying what the computer should do with the program. That
is, run, save and other options. The line numbers of the lines of code entered
by the user is used to arrange the program in order. So, irrespective of the
order in which the lines of code in the program are entered into the
computer, the computer arranges these lines based on their line number.
III. Correcting errors
It is very rare for any programmer to write programs without finding errors
in them or making simple mistakes. There is therefore, the need to be able to
correct typing errors, add, change or delete statements of code after being
entered into the computer. Errors can be changed in code statements when
entered into the computer but this must be before the lines of code are
transmitted into the computer by hitting the “ENTER” key.
Errors in spelling or the use of characters can be corrected by using the
“DELETE” key. This key rubs out the most recent character of the code
statement. When the “DELETE” key is pressed, the backslash character is
shown on the command line to denote deletion. Each time the backslash
character is pressed, the deleted character appears to the right of the
backslash.
IV. Processing a program
Once a program has been entered into the computer and all known errors
have been corrected, the program can be run. To type out the program, use
the LIST command; to store or save the program for execution at a later
time, use the SAVE command; to run or execute a program, use the RUN
command.

Branching and looping in BASIC


Branching is an important aspect of programs which involves transferring the
control of execution from one line of the program to another line of the program.
Branching could be of two major types and these are: Conditional and
unconditional branching.
Conditional branching is that kind of branching that occurs when a specified
condition is met. This involves the logical evaluation of the conditional statement
to check of the condition(s) is/are met.
Unconditional branching is that kind of branching that does not involve the use or
evaluation of logical statements before the transfer of control is made.
The GOTO statement can be used for branching.
Looping is a very important part required in writing programs. Looping involves
the repetition of a particular code or set of codes a particular number of times. It
can be done a specified number of times or it can be done until a particular
condition is met. If a condition is specified, the repetition of statements is done and
when the condition is met, the loop is terminated.
Relational operators
These operators are used to make comparisons and carry out conditional branching
in programs. These relational operators include
Equal to: =
Not equal to: <>
Less than: <
Less than or equal to: <=
Greater than: >
Greater than or equal to: >=
Examples of the use of relational operators include:
N<=34
P<>Q
S>=X*Y
For comparison of strings,
N$= “funke” Condition is satisfied (returns true) if the string “funke” is in N$
P$<>Q$ Condition satisfied if both strings are not equal
V$<E$ Condition satisfied if V$ appears before E$ in an alphabetical
order
S$>D$ Condition satisfied if S$ appears after D$ in an alphabetical order

Conditional branching
The IF-THEN statement is used for conditional branching. The statement has the
format
line_number IF condition THEN destination_line_number
So, if the condition is met, the control is transferred to the destination line number
stated but if the condition is not met, the execution continues from the next line
after the IF statement. Also, the GOTO statement can be used in place of the
THEN.
Example
Consider this snippet of code:

40 IF age<18 THEN 60

50 REM This is a comment

60 PRINT “You are under aged”

Example
Write a BASIC program that takes in the coefficients of any quadratic equation and
solves for the roots of the equation.

(Remember: x= )

Solution
10 REM This program computes the roots of a quadratic equation
20 CLS
30 INPUT a, b, c
40 REM Calculate the determinant to determine if roots are real or imaginary
50 LET D=(b^2-(4*a*c))^0.5
60 IF D=0 THEN 90 ’since determinant=0, roots are equal
70 IF D>1 THEN 120
80 IF D<1 THEN 150
90 X1=-b/(2*a)
100 X2=-b/(2*a)
110 GOTO 160
120 X1=(-b+D)/(2*a)
130 X2=(-b-D)/(2*a)
140 GOTO 160
150 PRINT “The result is a complex number”
160 PRINT “X1=”; X1, “X2=”; X2
170 END

Example
Write a program that receives three numbers from the user and displays and prints
the minimum of the three numbers.
Solution
10 REM This program accepts three numbers and displays the minimum of all
20 CLS
30 INPUT a,b,c
40 LET min=a
50 IF b<min THEN 70
60 IF c<min THEN 90
70 LET min=b
80 GOTO 60
90 LET min=c
100 PRINT “The minimum of the three is”; min
110 END

It is best to place the conditions in succession and then figure out where to place
the assigning and GOTO statements that will make the program be in order.

Example
You are the parking lot manager of a big parking services company. On the job, the
following rates apply in calculating the parking charge for anyone who parks
within the premises:
3hrs and below- 1000 naira per hr
More than 3hrs- 2000 naira per hr
Write a program that computes the bill of anyone who parks in the parking lot of
the company.
Solution
10 REM This program computes the bill of customers using the parking lot
20 CLS
30 INPUT x ’x is the number of hours of parking
40 IF X<=3 THEN 60
50 IF X>3 THEN 80
60 LET pay= 1000*x
70 GOTO 90
80 LET pay= 2000*x
90 PRINT “The charge for the customer is”; pay; “naira”
100 END

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy