Back to Course

JSS2: COMPUTER STUDIES - 2ND TERM

0% Complete
0/0 Steps
  1. Introduction To Basic Language I | Week 1
    7 Topics
    |
    1 Quiz
  2. Introduction To Basic Language II | Week 2
    2 Topics
    |
    1 Quiz
  3. Computer Ethics I | Week 3
    6 Topics
    |
    1 Quiz
  4. Computer Ethics II | Week 4
    2 Topics
    |
    1 Quiz
  5. Safety Measures In Computer Environment | Week 5
    3 Topics
    |
    1 Quiz
  6. Computer Graphics Packages I | Week 6
    3 Topics
    |
    1 Quiz
  7. Computer Graphic Packages II | Week 7
    6 Topics
    |
    1 Quiz
  • excellence
  • Follow

Lesson Progress
0% Complete

Topic Content:

  • Key Basic Statements

1. REM Statement:

The REM Statement is used in making comments or remarks. This kind of comment or remark is meant only for the consumption of the users. So, they are ignored by the computer during execution. REM Statements take this form: <line number>REM<comment>. For example,

10 REM THIS PROGRAM CALCULATES ALL THE VAT FOR ALL THE TRANSACTIONS

15 REM AMOUNT READ IN

2. INPUT Statement:

The INPUT statement is a means for data statements. It includes READ-DATA Statements. It enables us to input data into a program through the computer keyboard.

INPUT statement takes this form: <line number>INPUT<variable list>. The list of variables is separated with a comma.

For example,

10 INPUT A, B, C

3. READ-DATA Statements:

Any data value including string data value can be read in the READ-DATA statement. It provides another means of data input into a program. READ-DATA statements take the following forms:

<line number>READ<variable list>

<line number>DATA<data list>. For example,

10 READ A, B, C

20 D = A + B + C

30 PRINT D

40 DATA 9.2, 5, 3.5

50 END

The execution of the statement on Line Number 10 would cause the data values 9.2, 5, and 3.5 specified in the DATA statement in Line Number 40 to be read into the variables A, B, and C respectively.

4. OUTPUT Statement:

The OUTPUT statement is a means for data output. It is shown clearly in the PRINT statement. It takes this form: <line number>PRINT<output list>.

5. PRINT Statement:

The PRINT statement provides a way of getting a result of the output from a program. It takes this form: <line number>PRINT<output list>. The output list is a list of variables to be printed out. The PRINT Statement DISPLAYS the result on the screen. It should not be confused with ‘Printing’. For example,

10 LET A = 9.2

20 LET B = 3.5

30 PRINT A, B

40 PRINT “A + B IS”, A+B

When there is a PRINT statement without <output list>, the Statement causes a print line to be skipped. This could be likened to ENTER Key on the keyboard. For example,

10 PRINT

6. LET Statement:

The LET statement to assign values in a program. It enables you to evaluate the arithmetic expression indicated on the right side of the “equal to” sign. It also helps to move the variable specified on the left side of the “equal to” sign. LET Statement takes this form:

<line number>LET<numeric variable> = <arithmetic expression>. For example,

10 INPUT A! B! C!

20 LET D! = A! + B! + C!

Consider this type of mathematical expression in BASIC programming.

10 INPUT D

20 LET D = D + 1

Line 20 gives an expression that is not a correct mathematical equation but as a BASIC statement, it is acceptable because 1 is added to D and the result is placed in D.

If D were read in as 21, 1 would be added and 22 would be placed back into D. The effect of the LET statement in line 20 is to add 1 to D. This can be used to communicate a running total.

7. GO TO Statement:

In the GO-TO statement, the transfer of control or jump from one part of the program to another is called Looping. Looping involves repeating some portion of a program either a specified number of times or until some particular conditions have been satisfied. Looping could be Conditional or Unconditional. The GO-TO statement is used for Unconditional looping. The form of the GO-TO statement is GO-TO<line number> showing the line number of statements in the program. For example,

10 INPUT A, B

20 LET A = 2

30 LET B = 3

40 PRINT A,B

50 GO-TO 10

8. IF THEN Statement:

The IF-THEN statement is the Conditional part of Looping. It is used to carry out conditional branching operations. The form of the IF-THEN statement is:

IF <relational expression> THEN<line number>

Where <relational expression> is a relational expression involving a comparison of two quantities using a relational operator. <line number> is the line number of an existing statement. The relational operators used in relational expressions are the following:

Equal to:                                      =

Less than:                                    <

Greater than:                               >

Less than or Equal to:                  <=

Greater than or Equal to:             >=

Not Equal to:                                <>

A relational expression gives a condition that is satisfied or not satisfied. Examples of relational expressions are:

N<=0.001

X=25

A+B<C+D

P<>Q

C>(C2+C2)2                              

Z>=X*Y

An Example of a conditional looping is:

10 INPUT A, B

20 LET A = 2

30 LET B = 3

40 PRINT A,B

50 GO-TO 10

60 IF I>=100 THEN 80

70 LET I=I+1

80 PRINT A,B

90 GO-TO 10

100 END

The IF THEN statement provides the programmer the capability for looping and opens for him/her a wide variety of similar operations with minor modifications. For example,

5 REM SUM INTEGERS FROM 1-1000

10 LET S = 0

20 LET N = 1

30 LET S = S + N

40 LET N = N + 1

50 IF N <= 1000 THEN 30

60 PRINT S

70 END

9. FOR AND NEXT Statement:

The FOR AND NEXT statement is another form of looping meaning that the sequence of instructions is to be repeated a fixed number of times. They enable a more concise way of handling loops than the IF-THEN statements. Consider Using the FOR AND NEXT statement to handle programs involving IF-THEN looping.

SUM ALL INTEGERS FROM 1-200 AND PRINT OUT THE RESULT

Using the IF-THEN Statement

10 LET S = 0

20 LET I = 1

30 LET S = S + 1

40 LET I = I +1

50 IF I> 200 THEN 70

60 GO-TO 30

70 PRINT S

80 END

Using the FOR AND NEXT Statements:

10 LET S = 0

20 FOR I = 1 – 200 STEP 1

30 LET S = S + I

40 NEXT I

50 PRINT S

Statements 20, 30, and 40 constitute a FOR….NEXT Loop. A FOR…NEXT Loop starts with a FOR statement and ends with a NEXT statement.

Notice that statements 20, 40, and 50 in Example 1 are instructions to perform the following operations respectively.

– initialize I to 1

– increment I to 1

– if I is greater than 100 (the final value), get out of the loop (consisting of statements 30 – 60).

The FOR statement 20 in our example above combines these three operations performed in statements 20, 40, and 50 in the IF-THEN example. The NEXT statement in line 40 means “go back to execute the FOR statement”.

The operational logic of the FOR Loop as given in statements 20 to 40 above can be explained in this way:

a. initialize I with 1

b. if I > 100 then exit the loop by going to statement 50 (i.e the statement immediately following the NEXT statement).

c. execute statement 30 (that is the statement to be executed if I <=I<= 100).

d. increment I by 1

e. Go back to (b)

I in the example is called the loop variable. I (after =) is called the initial value of the loop variable. 100 is called the final value of the loop variable. I (after STEP) is called the incremental value for the loop variable.

FOR AND NEXT loop is certainly useful when one knows exactly how many times one wants a sequence of statements to be executed. 

10. ON-GO TO Statement:

ON-GO TO statement is used to replace many IF-THEN statements at a time. It is called a computed GO TO statement. The general form of ON-GO TO statement is:

<line number> ON <expression> GO TO <list of statement numbers>. For example,

10 INPUT A, B, C

20 IF A = 5 THEN 80

30 IF A = 7 THEN 100

40 IF A = 10 THEN 90

Line numbers 20 to 40 could be replaced with ON-GO TO statement like this:

20 ON A GO TO 80, 100, 90

If A is equal to 5, then a loop is made to the first number indicated, in this case, line number 80. If A is equal to 7, then a loop is made to line number 100 which is the second line number in this program. IF A is equal to 10, then a loop is made to the third line number which is 90.

11. STOP Statement:

The STOP statement is a control statement. It should not be mistaken for END which is used to terminate a program. Generally, the BASIC program ends with an END statement and when executed, the program terminates normally. STOP statement causes an abrupt termination of a program but it is not the final end of the program. STOP statement could be likened to a bus stop whereas the END statement could be likened to a bus terminal where all passengers are disengaged.

STOP statement takes this form:

<line number>STOP. For example, using a BASIC program, calculate and print the average of five amounts, and then repeat the process. Stop when an amount is equal to 10000.

10 S = 0

20 FOR I = 1 – 5

30 INPUT A

40 IF A = 10000 THEN 90

50 S = S + A

60 NEXT I

70 PRINT S

80 GO TO 10

90 STOP

100 END

Statement number 90 gives an example of a STOP statement.

12. DIM Statement:

DIM statement is used to tell the program to reserve enough room for a list. The BASIC translator automatically reserves enough location to allow for subscript whenever a subscripted variable is encountered during translation.

The general form of the DIM statement is: 5 DIM S(10). For example, 10 subjects are expected to be entered for every student in JSS2, we will need a DIM statement like this:

<line number> DIM <list of variables>

To allow the program so it can read in 14 exam scores of students in 14 different subjects and compute the average, we shall add the DIM statement and simply change the parameters in this form:

5 DIM S(14)

10 LET T = 0

20 FOR J = 1 – 14

30 INPUT S (J)

40 LET T = T + S (J)

50 NEXT J

60 PRINT T/14

70 GO TO 10

80 END

S = Subjects, T = name of the array, J = Next subject.

avatar