CSC 112 - Lecture 3
CSC 112 - Lecture 3
CSC 112 - Lecture 3
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.
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.
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.
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
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