Fortran re-engineering example (4)

Now we come to the crux of the re-engineering problem: goto

This is in reality a small program with uncomplicated unstructured constructs. But everyone has to start somewhere. The first four goto statements in reality form a type of “goto” based if-else statement. These can be converted. This means the code below can become much more readable and actually produce less code.

 5 if (totdis.gt.250) goto 10
   tower = 1
   dist = totdis
   goto 20

10 if (totdis.gt.750) goto 15
   tower = 2
   dist = iabs(totdis - 500)
   goto 20

15 tower = 3
   dist = 1000 - totdis

20 if (dist.le.30) vel = 2.425+0.00175*dist*dist

You can see how this works in the marked-up code shown earlier. This is re-engineered into the following structure:

 5 if (totdis.le.250) then
       tower = 1
       dist = totdis
   else if (totdis.le.750) then
       tower = 2
       dist = iabs(totdis - 500)
   else 
       tower = 3
       dist = 1000 - totdis 
   end if 

This means that the conditional statements have to change, because the original code jumped to another point in the program if the conditional is true, and essentially carried out the code after the if, only if the conditional was false. So the first conditional must change from (totdis.gt.250) to (totdis.le.250), and the second conditional changes from (totdis.gt.750) to (totdis.le.750). The code at label 15 becomes the else portion of the if-else structure.

Not forgetting that the newer Fortran if statement uses the then clause, and is terminated with end if. Now that the goto statements have been removed, the associated labels 10, 15 and 20 can also be deleted. Here’s the code at this point:

cablecar3

 

 

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s