CSC 112 - Lecture 4
CSC 112 - Lecture 4
CSC 112 - Lecture 4
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,
20 CLS
60 INPUT X, Y
100 PRINT “This program does not cater for negative powers”
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.
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
30 INPUT number
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
90 STOP
140 STOP
150 PRINT “Enter the radius and height of the cone separated by commas”
160 INPUT r, h
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.
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.
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
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.
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.
40 GOTO 100
70 NEXT I
You can have the above example but cannot have this next one:
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.
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.
20 CLS
100 NEXT I
120 END
Question
1. Write a program that computes the sum of all odd numbers from 1 to 100.