Go To ( Computed )

Statement Purpose

A computed GO TO statement will transfer control to one of several possible statement labels based upon the outcome of an arithmetic expression. This is unlike the unconditional go to statement which transfers program control directly to a single statement label.

Examples and Rules of Use

The computed GO TO has a list of possible statement labels to which the program could branch, depending upon the evaluation of an arithmetic expression. To understand how this construct works, it is best to take a look at an example.
			go to (101,102,103) k/2+1
			print *, 'k/2+1 is less than 1 or greater than 3'
			go to 200
		101	print *,'k/2+1 equals 1'
			go to 200
		102	print *,'k/2+1 equals 2'
			go to 200
		103	print *,'k/2+1 equals 3'
		200	continue
Based upon my use of print statements you should be able to get a feel for how this construct works. The program will execute the lines of code directly under the initial GO TO expression if k/2+1 evaluates to be less than 1 or greater than 3. This type of construct will always branch to the statements directly beneath the initial GO TO statement if the expression is less than one or its value exceeds the number of statement labels that are enclosed by parentheses in the original GO TO statement. Next, based upon the print statements you can tell when the program will branch to each one of the statement labels that are in parenthesis. The program will go to the first statement label in parentheses if the expression equals one, the second if the expression equals two, and so on and so forth until the number of statement labels is met. Then as was said earlier, the program will continue to the statements directly underneath it.

The above example was pretty basic in nature. It shouldn't take too much imagination to realize that with all the branching and statement labels that would be required for a more complicated situation could lead to some complex code. This in turn increases the chances for programming errors to occur. Because of this potential, this statement is not used extensively in any code written today. The function provided by this construct can be taken care of by the FORTRAN 90 select case construct or the Block IF. For instance the above block of code can be rewritten using a select case construct in the following manner.
		select case (k)
		case (1)
			print *,' k equals 1'
		case (2)
			print *,' k equals 2'
		case (3)
			print *,' k equals 3'
		case default
			print *,' k is less than 1 or greater than 3'
		end select

For more Information and Examples See

lecture twelve

Up one level / Home

Written by Jason Wehr: jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu