Go To ( Unconditional)

Statement Purpose

The unconditional GO TO statement is one of many branching techniques in FORTRAN. This statement will cause the program to branch to another part of the program by forcing the program to transfer program control to an executable statement with a specified label..

Rules of Use and Examples

One of the unconditional GO TO's biggest uses before FORTRAN 90 came along was to force either an early exit from a DO loop or to force the loop to cycle through the loop another time. For instance, by looking at the following DO loop, you should get an idea of how these statements can be used to either exit the loop or force it to cycle.
		    n=1
		    do 100 i=1,ldata
		      if (Fr(i).eq.0) go to 200
		      if (Fr(i).lt.1.0) go to 100
		      supf(n)= Fr(i)
		      lsupf(n)= station(i)
		      n=n+1
		100 continue
		200 print *, 'Loop exited at data point',i
To write code like this is still perfectly legitimate in FORTRAN. However, there is a school of thought that says GO TO statements are dangerous and should never be used. This is the reason why the exit and cycle statements were introduced in FORTRAN 90. These statements were created to replace go to's inside of loops like the one above. For instance, the CYCLE statement could be used in place of "go to 100", while the EXIT statement could be used instead of "go to 200".

Even though providing a means to exit a loop early is one of the biggest uses of the unconditional GO TO, it is by far not its only use. In fact, the GO TO statement can appear almost anywhere among the executable statements in a program. It is not permitted as the termination statement of a DO or DO WHILE statement. That means a line like
		go to 2001
can be placed at random in your code. However, before you go littering your code with lots of GO TO statements you should be warned that if not used carefully your code can be very difficult to read, or you may accidentally loop over crucial parts of your code or force your code to branch to an unintended part of your program. The likelihood of such errors occurring is why the anti- GO TO statement school of thought exists.

For Additional Information and Examples See:

lecture eleven

examples: log_plot.f and secant.f

Up one level / Home


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