Custom Search
 
  

 
ON-GOTO Statement (Conditional)

The IF-THEN statement allows the computer to follow one of two possible paths. The ON-GOTO differs from the IF-THEN statementit allows many branches or alternative paths. Several IF-THEN statements could be combined to produce the same results; however, the ON-GOTO statement can be more efficient. It conditionally transfers control to one of several lines, depending on the value of an expression in the ON-GOTO statement when it is executed.

In an ON-GOTO statement, the expression is evaluated and the result truncated to obtain an integer, whose value is then used to select a line number from the list following the GOTO. If the value of the integer is 1, the computer goes to the first line number; if it is 2, the computer goes to the second line number and so forth.

Example:

This statement will transfer control to:

line 90 if X is greater than or equal to 1 but less than 2.

line 100 if X is greater than or equal to 2 but less than 3.

line 110 if X is greater than or equal to 3 but less than 4.

If values of X other than 1, 2, or 3 are encountered, you would get an error message on some computers; on others, the statement would be ignored. Suppose you wanted to write a program to determine what your return would be on savings deposited in an account paying the following annual interest rates.

4.0% paid on savings of $1000-1999

4.5% paid on savings of $2000-2999

5.0% paid on savings of $3000-4999

The following program asks you to enter the amount saved, between $1000 and $4999, then uses the ON-GOTO to determine what interest rate to use in calculating the amount of interest earned.

If 1500 were entered, line 60 would transfer control to line 90 where the interest rate of 4% is used to calculate the interest earned in a year. If 3500 were entered, line 60 would transfer control to line 130. Likewise, if 4800 were entered, it would also transfer control to line 130.

You probably noticed a zero (0) was to be entered when there was no more data to be processed, and that line 50 tested for 0. If X equals 0, control transfers to line 160 and stops. This is another example of testing a variable condition to determine when to exit a loop.

Note, also, the statements in lines 70 and 80, these act as an error routine. If you enter a value other than 1000 through 4999, line 70 prints the message, "VALUE OUT OF RANGE" and line 80 transfers control back to line 40 for you to enter another value.







Western Governors University
 


Privacy Statement - Copyright Information. - Contact Us

Integrated Publishing, Inc. - A (SDVOSB) Service Disabled Veteran Owned Small Business