March 29, 2024

BASIC statements are the principal components of BASIC programs. The general format of

BASIC statements is:

 

Where

xxx is the line number; Reserved word is the statement (instruction) type; and Parameters are the variables used in conjunction with the statement type.

Each statement starts with a line number followed by the reserved word statement type. Spaces have no significance in BASIC language statements except in messages or literal strings which are displayed or printed out.

 

Statement Line Numbers (Sequencing)

Failure to assign a line number results in the message:

WHAT

Each line of the program begins with a line number of 1 to 5 digits that serves to identify the line as a statement. The largest allowable line number is 99999. A common programming practice is to number lines by fives or tens, so that additional lines may be inserted in a program without the necessity of renumbering lines already present.

 

PROGRAM STATEMENTS

A BASIC program is made up of a number of BASIC program statements. Each BASIC program statement begins with a line number. Line numbers must be whole numbers in the range of 1 to 32767. It is common practice to separate line numbers by increments of 10, so that additional program statements can be added with ease later.

 

Classification of Basic Program Statements

BASIC Program statement can be categorized as follows:

  1. Remark statement e.g REM.
  2. Information Entry Statement e.g. INPUT, READ -DATA.
  3. Data manipulation Statement e.g. LET.
  4. Output Statement e.g. PRINT.
  5. Loop Statement e.g FOR … NEXT, WHILE ….. NEXT.
  6. Control Statement e.g. GOTO, IF – THEN, GOSUB.
  7. Program Termination Statement e.g. END, STOP.

 

1) The Remark Statement

The REM statement is a non – executable statement used to insert comments into the source program. A programmer may include comments in his/her program with the REM statement. BASIC completely ignores REM lines, but they are intended to assist a person trying to understand how your program works. They are also helpful for improving the visual appeal of your program. The exclamation mark (!) may also be used to add comments. The exclamation mark (!) can be used in the same way as a REM. The exclamation mark (!) can also be used to place a comment on the same line as a BASIC statement.

 

REMARK Statement Format

 

e.g.

10 REM Program to write a letter

 

2) Information Entry Statements

(a) INPUT statement: The INPUT statement allows a value, numeric or character string, to be typed at the keyboard of a computer and stored in the computer’s memory at the data-name specified.

Format: INPUT data-name

 

Example: INPUT X will enable a numeric value for X to be entered at the keyboard and stored in the memory under the data-name X

INPUT A$ will enable a string value for A$ to be entered at a keyboard and stored in the memory under the data-name A$

 

When the computer executes an INPUT statement, it will output on the screen of a terminal a prompt, either ? or ! depending upon the system used, and wait for the user to enter a value at the keyboard.

 

For example: INPUT “WHAT IS YOUR NAME”, N$

Will output to the terminal the prompt WHAT IS YOUR NAME? and enable a string value for N$ to be entered at the keyboard and stored in the memory under the data-name N$.

 

(b) DATA, READ Statements: The DATA and READ statements are always used together. The DATA statement is used to set up a list of numeric or string values. These values are accessed by the READ statement which assigns those values to variables in a program.

DATA Statement Format: The DATA statement sets up a list of values to be used by the READ statement.

 

DATA Statement Format

where values are numeric and/or string entries separated by commas. The DATA statement serves as a source of input variables for the program. The variables are accessed for processing by the READ statement.

There may be any number of DATA statements in a program. They are not executed and may be placed anywhere within the program. However, BASIC treats them all together as a single list.

Both string data and numeric data may be intermixed in a single DATA statement.

String data in a DATA list must always be enclosed by quotation marks.

For example, the three DATA statements

10   DATA A

20.  DATA B

30   DATA C

are equivalent to the following statement:

 

50 DATA 2, 3, 4

 

READ Statement Format: The READ statement accesses variables defined by the DATA statement and assigns those values to variables in a program.

 

READ Statement Format

where variables are names corresponding to values contained in DATA statements.

A READ statement may contain numeric and string variable names intermixed, to correspond to numeric and alphanumeric values in the DATA list. However, since values are taken from the DATA list in sequential order, you must ensure that the values in the list are in the correct sequence to correspond to the variable names of the same format (numeric or string).

A READ statement may have more or fewer variables than values in any one DATA statement. The READ statement causes BASIC to search all available DATA statements in the order of their line numbers until values are found for each variable in the READ. A second READ statement will begin reading values where the first one stopped.

The READ statement is always used in combination with the DATA statement.

 

Example 1

10  READ  A, B, C

20  DATA   5, 8, 3

 

Example 2

10 READ NAME1$, AGE1

20 READ NAME2$, AGE2

30 READ NAME3$

40 READ AGE3

50 PRINT NAME1$; AGE1, NAME2$; AGE2, NAME3$; AGE3

60 PRINT “Total age is”; AGE1 + AGE2+ AGE3

70 DATA “BOB”, 21, “MARY”

80 DATA 18

90 DATA BILL

100 DATA 45

110 END

RUN [Return]

 

Example 3: The following program uses DATA statements to supply both a variable number of scores and variable score values to an average calculation routine.

10 PRINT ‘NUMBER’

20 PRINT ‘OF SCORES’,’AVERAGE’

30 PRINT

40 READ N1

50 FOR 1=1 TO N1

60 READ N

70 I F N= O GOTO 210

80 LET S= 0

90 FOR K = 1 TO N

100 READ T

110 LET S= S+T

120 NEXT K

130 PRINT N, S, T

140 NEXT I

150 DATA 5

160 DATA 3, 82, 88, 97

170 DATA 5, 66, 78, 71, 82, 75

180 DATA 4, 82, 86, 100, 91

190 DATA 4, 72, 82, 73, 82

200 DATA 61, 73, 67, 80, 84, 79

210 END

 

3) Output Statement

PRINT statement

The PRINT statement is used to perform calculations and display results. It is also used to display alphanumeric (string) messages.

 

PRINT Statement Format

Where expressions may be numbers, variables, strings or arithmetic expressions, separated by commas or semicolons.

When used without an expression, a blank line will be output on the terminal. In BASIC, a line is formatted into five fixed zones (called print zones) of 14 columns each. If the expressions in a PRINT statement are separated by commas, each will begin at a 14-column interval. That is, the first expression will be displayed starting at the first position, the second at the fifteenth position, and so forth. If more than five variables are involved, the display will automatically continue at the beginning of the next line. If this format is not desired, you may separate expressions with the semicolon (;), causing the text or data to be output immediately after the last character printed.

If the last expression in a PRINT statement is followed by a comma or semicolon, the next display called for by the program will begin on the same line (if there is room). This also applies to the question mark displayed by the INPUT statement.

For example 1

PRINT “GOD BLESS YOU”, N$

Will output to a screen:

GOD BLESS YOU STEVE

If the value of N$ is STEVE

 

Example 2

10 PRINT A, B, C

15 PRINT N$, T$, Game

20 PRINT (2 * 8)/2

 

Example 3

5 LET A = 10

10 LET B = 15

15 LET C = A + B

20 PRINT C

The monitor will display 25 on the screen

 

Example 4

10 PRINT “I like Programming”

 

PRINT without a literal and / or data-name will output a blank line.

 

4) Assignment Statement

LET STATEMENT

Values are assigned to numeric and string variables with the LET statement. The general form of the LET statement is:

where

v is a variable, and the expression on the right side of the equal sign can be either a constant, another variable or an arithmetic expression.

A LET statement operates by evaluating the expression on the right-hand side of the equal sign and storing the result in the variable on the left-hand side of the equal sign.

Examples:

10 LET A = B    will assigns value of A to B

15 LET M = 3    will assigns to M value 3

20 LET B$ = “WELCOME”    will assigns to B$ the value WELCOME

25 LET T = A + B      will evaluate A + B and assign to T the result.

 

5) Looping Statement

Loops

In designing computer programs, it is often necessary to repeat part of a program a number of times. One way to achieve this would be to write out that part of the program as many times as it was needed. This, however, is a very impractical method since it would produce a very lengthy computer program and the number of times that program was to be repeated is not always known in advance. The process of repeating a series of instructions is called looping. The group of repeated instruction is called a loop. A single execution of the statement(s) in the loop is called iteration.

The instructions that make up a loop have two main elements:

  • A sequence of instructions to be performed each time the loop is executed.
  • An indication of when to end the execution of the loop.

A program loop is a series of instructions that is executed repeatedly.

(a) FOR and NEXT Statement: FOR Statement Format – The FOR statement is used in combination with the NEXT statement to specify program loops.

where

V: is a variable that serves as the INDEX for the loop. The index value is incremented (or decremented) each time the loop is executed.

X: is an expression (numerical value, variable name, or mathematical expression) indicating the initial value of the index, that is, the value it will have before the loop is executed the first time;

Y: is the terminal value of the index. When v is “beyond” y, the loop will not be repeated; and

Z: is the step value, that is, it is the value that is added to the index each time the loop is executed.

 

Variable z may be assigned a minus value in cases where it is desired to decrement the index. If “STEP Z” is omitted, a value t 1 is assumed.

The x, y, and z values are expressions: these expressions are evaluated upon first encountering the loop. This includes setting the index equal to the initial value. Therefore, the program can later jump back to the same FOR statement any number of times to re-start the loop. If the x, y, and z values are unchanged, the loop will be repeated the same number of times each time the program executes the FOR statement. A NEXT statement is never used without a FOR statement. The variable value must be identical to the index value (v) used in the corresponding FOR statement.

It is possible to exit from a loop without the index reaching the terminal value by using an IF statement. Control may transfer into a loop only if that loop was left earlier without being completed.

Example: 

10 FOR N=2 TO 6 STEP -1

20 PRINT N

30 NEXT N

40 END

 

Rules for Using For …….. Next Loop.

  • Loops may be nested (embedded one inside the other).
  • Loops must not cross each other.
  • The numeric data-names given to a loop variable should never be re-assigned with a different value within the loop.
  • Avoid fractional STEPS values since these could lead to rounding problem.

 

(b) WHILE and NEXT statements: The WHILE and NEXT statements are also used to create program loops. WHILE loops are used when you are not sure how many times you want the loop to execute, but want the loop to continue executing until a terminating condition is met. The general form of the WHILE/NEXT statement is:

 

NEXT marks the end of the loop. Statement(s) are indented to show visually that they are all part of the same loop.

The WHILE loop operates by executing the statement(s) while the relational expression is true. When the relational expression becomes false, the loop terminates. The next statement that is executed is the one that follows the NEXT statement.

10 INPUT “Enter a name (LAST to end)”; NAME$

20 WHILE NAMES <> “LAST”

30 PRINT NAME$

40 INPUT “Enter a name (LAST to end)”; NAME$

50 NEXT

60 END

 

6) Control Statement

Control statements allow the execution of the BASIC program to be redirected. It can be classified as follows:

  • Conditional Branch (IF – THEN Statement).
  • Unconditional Branch (GOTO Statement).
  • Branch to Subroutine (GOSUB Statement).

 

(a) IF-THEN Statement: The IF-THEN statement tests a condition and redirects program control if that condition is true. That is, if the condition specified is true, the IF-THEN statement effectively executes a GOTO statement for the line number specified. When the condition is false, the next statement is executed.

where

V1, V2 are variables, numbers, strings, or expressions to be compared;

Relation is the relational operator to be used in comparing VI and V2.

THEN may be replaced with GOTO if desired (either THEN or GOTO is acceptable, but one of the two must be present); and

N is the number of the statement to which the program will jump if the relationship described is true.

 

Strings may be used in IF-THEN statements, but comparisons are based on the positions of the characters in the string sequence. The question mark (?) is the highest alphanumeric character and the at sign (@) is the lowest. The two strings described in the IF-THEN statement are compared one character at a time, from left to right, until the ends of the strings are reached or until an inequality is found.

 

Example 1

10 READ A, B, C, K

15 DATA 1, 2, 3, 4

20 LET D = A + B + C

25 IF K < 2 THEN

30 PRINT D

35 END

 

IF-THEN ELSE Statement: This statement is used to execute multiple conditional statements.

Example:

A BASIC program to find the area of a square or a circle depending on the string that is assigned to the variable mark.

Solution

IF (mark $ = “square”) THEN

INPUT length, width

ELSE

INPUT radius

LET area = 3.142 * radius ^2

END IF

PRINT “Area”; = area

 

(b) GOTO Statement: The GOTO statement is used to transfer control to another line statement in a program. BASIC then continues execution at the line number referred to in the GOTO statement.

 

GOTO Statement Format

 

Where N is the line number of the statement to which control should be transferred.

Example 1

5 REM Program sum of numbers

10 GOTO 25

15 READ A, B, C, D

20 PRINT SUM = A+B+C+D

25 DATA 6, 7, 8, 9

30 STOP

35 END

 

Example 2

10 REM program sum

15 GOTO 25

20 PRINT “SUM”

25 END

 

(c) GOSUB Statements: A subroutine is a group of statements that perform a processing operation at more than one point in a program. The GOSUB statement is used to branch to the subroutine and the RETURN statement redirects control back to the main body of the program.

GOSUB is always used with the RETURN statement. The RETURN statement is the last statement in a subroutine. When the program encounters the RETURN statement, control transfers back to the statement following the GOSUB.

 

GOSUB Statement Format

where n is the line number of the first line of the subroutine.

When the program encounters a GOSUB statement, the following action occurs:

  • BASIC internally records the number of the statement following the GOSUB statement.
  • Control is transferred to statement number n.

 

Example:

10 FOR A= 1 TO 3

20 LET T=A

30 GOSUB 1000

40 LET X=2*A

50 GOSUB 300

60 NEXT A

70 STOP

 

500 REM CALCULATE

510 LET V = X + 2

520 GOSUB 1000

530 RETURN

 

1000 REM PRINT VALUE

1010 PRINT “THE VALUE IS ” ;

1020 PRINT T

1030 RETURN

5000 END

 

7) Program Termination Statements

BASIC is equipped with two statements, the END and STOP statements, which can be used to terminate program execution.

 

END Statement Format

The END statement terminates execution of the program. It informs the BASIC compiler that it has reached the last line of the program. Only one END statement may appear in a program and it must be the last statement in the program

 

END Statement Format

 

NOTE

STOP Statement Format

The STOP statement is used to terminate the execution of a program. It should be noted that there may be several STOP statements in a program.

 

STOP Statement Format

 

 

The STOP statement is used to terminate the program execution at any point in the program. This can appear as many times as possible in the program.

The END statement is used to terminate the entire program. It can only appear at the end of the program once.

Example

5 REM This program calculates the sum of numbers

10 READ A, B, C, D

15 DATA 10, 12, 13, 15

20 PRINT SUM = A + B + C+ D

25 STOP

30 END

Leave a Reply

Your email address will not be published. Required fields are marked *