CSC 112 – LECTURE FOUR
We continue today by considering branching as discussed in the last class. The
conditional branching can be used to form loops. Loops are control structures that
perform continuous repetition of a statement or statements based on a condition
being met. We will look into the use of the FOR loop later on but for now, we will
consider the use of the IF-THEN branching structure to implement a loop.
Example
Write a program that provides the solution of an unknown number being raised to
the power of another number. Let both numbers be provided by the user.
Solution
We need the solution to the expression where x is the first number entered by
the user and y (the power) is the second number entered. For instance,
10 REM This program computes the exponential value of a number
20 CLS
30 PRINT “This program calculates the exponent of a number”
40 PRINT “The first number entered is the number”
50 PRINT “The second number entered is the power”
60 INPUT X, Y
70 IF Y<0 THEN 100
80 IF Y=0 THEN 120
90 IF Y>0 THEN 140
100 PRINT “This program does not cater for negative powers”
110 GOTO 210
120 PRINT X; “raised to the power of”; Y; “equals 1”
130 GOTO 210
140 LET EXPO=X
150 LET Y=Y-1
160 IF Y>0 THEN 180
170 GOTO 200
180 LET EXPO=EXPO*X
190 GOTO 150
200 PRINT X; “raised to the power of”; Y; “equals”; EXPO
210 END
Multiple branching
Multiple branching can be done with the use of the ON-GOTO or ON-THEN
statement. The THEN may also be used to replace the GOTO. This statement
consists of a numeric variable or formula resulting in a numeric variable and a list
of line numbers (two or more) in the program to which control can be transferred
based on the value of the variable/formula. If the value of the determining
variable/formula is 1, control is transferred to the first line number on the list, if 2,
then to the second line number and so on.
e.g. 20 ON X GOTO 30, 40, 50
If the value of X is a decimal value, the decimal portion is truncated and the whole
number value is used to determine the destination line number.
We will consider examples on this, but before that, we will look at the STOP
statement.
The STOP statement is one that makes use of the STOP keyword on a line. It is
used to end the program at that particular point. The STOP statement can be placed
anywhere within the program and can be placed in multiple places unlike the END
statement which must only appear once and at the end of the program.
Precious programs we have considered made use of the GOTO statement to end a
program by transferring control to the last statement which is the END statement.
So, with the use of the STOP statement, you can end the program at any point
without the use of the GOTO statement.
Example
Write a program that computes the volume of a cuboid, sphere or cone base on the
number entered by the user.
1-cuboid ( )
2-sphere ( )
3-cone (
Solution
10 REM This program computes the volume of shapes
20 PRINT “Enter 1 for cuboid, 2 for sphere, 3 for cone”
30 INPUT number
40 ON number GOTO 50, 100, 150
50 PRINT “Enter the length, breadth and height of the cuboid separated by
commas”
60 INPUT l, b, h
70 LET volume=l*b*h
80 PRINT “The volume of the cuboid is”; volume
90 STOP
100 PRINT “Enter the radius of the sphere”
110 INPUT r
120 LET volume=4/3*3.142*r^3
130 PRINT “The volume of the sphere is”; volume
140 STOP
150 PRINT “Enter the radius and height of the cone separated by commas”
160 INPUT r, h
170 LET volume=1/3*3.142*r^2*h
180 PRINT “The volume of the cone is”; volume
190 STOP
200 END
Loops
A loop can be created with the use of a FOR-TO –STEP statement. This is used to
repeat a statement or set of statements a specified number of times. The number of
repetitions must be known ahead of the loop creation.
e.g. FOR I=1 TO 10
This starts with the variable I as 1 and then continuously increases I by 1 until I
gets to 10. The STEP is used to tell the increment on the variable I each time the
loop is executed.
e.g. FOR I=1 TO 10 STEP 2
With this, I is increased by 2 after each execution. The running variable, I, can be
assigned negative numbers and fractions. Also, the STEP can be a negative number
if the value of I is to be decreased rather than increased.
e.g 30 FOR I=-1 TO 5 STEP 2
30 FOR I=1.5 TO 10 STEP 0.5
30 FOR I=6.5 TO 0 STEP -0.5
In closing a loop, we use the NEXT statement. The loop consists of all the
statements between the FOR-TO-STEP statement and the NEXT statement. The
NEXT statement consists of a statement number, the NEXT keyword and the
running variable used.
e.g
30 FOR I=1 TO 10 STEP 2
70 NEXT I
All statements between the line 30 and 70 would be executed before the running
variable is increased/decreased. After incrementing/decrementing, the statements
are executed again until the ending value (10 in this case) is reached.
Rules to be followed in constructing a FOR loop
1. The running variable can appear within the set of statements in the loop but
its value cannot be altered within the loop.
2. If the initial and final values of running the loop are equal and the step value
is positive and non-zero, then, the loop will be executed just once.
3. The loop will not be executed under the following conditions:
a) The initial and final values of the running variable are equal and the
step size is zero.
b) The final value of the running variable is less than the original value,
and the step size is positive.
c) The final value of the running variable is greater than the original
value, and the step size is negative
4. Control can be transferred out of a loop but not into the loop.
e.g.
30 FOR I=1 TO 10 STEP 2
40 GOTO 100
70 NEXT I
You can have the above example but cannot have this next one:
30 FOR I=1 TO 10 STEP 2
40 LET X=4+Y
70 NEXT I
…
90 GOTO 40
This is because control is being transferred from outside the loop into the
loop which is not allowed.
Also, it is good programming practice to indent the code within the FOR
loop for clarity.
30 FOR I=1 TO 10 STEP 2
40 LET X=4+Y
50 LET B=A+10
60 NEXT I
Example
Write a program that provides the solution of an unknown number being raised to
the power of another number. Let both numbers be provided by the user.
Solution
Here, we are using the same task as encountered previously but with a FOR loop.
10 REM This program computes the exponential value of a number
20 CLS
30 PRINT “This program calculates the exponent of a number”
40 PRINT “The first number entered is the number”
50 PRINT “The second number entered is the power”
60 INPUT X, Y
70 LET EXPO=X ’let the result be x at first
80 FOR I=1 TO Y-1 STEP 1
90 LET EXPO= EXPO*X
100 NEXT I
110 PRINT X; “raised to the power of”; Y; “equals”; EXPO
120 END
Question
1. Write a program that computes the sum of all odd numbers from 1 to 100.