Removing goto statements in FORTRAN code


 
Thread Tools Search this Thread
Top Forums Programming Removing goto statements in FORTRAN code
# 1  
Old 04-10-2013
Removing goto statements in FORTRAN code

I have the code below and I want to remove the "go to" statements. Any idea how I can do it?

Code:
 if (iorder == 0) then

    tmincurrent = 1.0e11
    if(ireverse == 0 .or. istop /= 1) then
      do i = 1, 6
        if ((side(i) /= sidelimit(i)) .and. (tminside(i) < tmincurrent)) then
          tmincurrent = tminside(i)
          iside = i
        end if
      end do
    else
      do i = 1, 6
        if(side(i) /= sidestop(i) .and. tminside(i) < tmincurrent) then
          tmincurrent = tminside(i)
          iside = i
        end if
      end do
    end if

  else

    if ((ireverse == 0) .or. (istop /= 1)) then
      2120 ioc = ioc + 1
      if (ioc < 7) then
        iside = order(ioc)
        if (side(iside) /= sidelimit(iside)) go to 2130
      else
        ioc = 0
      end if
      go to 2120
    else
      2140 ioc = ioc + 1
      if (ioc < 7) then
        iside = order(ioc)
        if (side(iside) /= sidestop(iside)) go to 2130
      else
        ioc = 0
      end if
      go to 2140
    end if

  end if

2130  continue

# 2  
Old 06-13-2013
All goto's like this can be refactored using a do/cycle/exit strategy. I wrote about this a bit at my Barrowes Consulting website.

In your case, the resulting code would be:

Code:
logical :: remg(3)=.true.
do
 if (remg(3)) then
  if (iorder == 0) then

   tmincurrent = 1.0e11
   if(ireverse == 0 .or. istop /= 1) then
    do i = 1, 6
     if ((side(i) /= sidelimit(i)) .and. (tminside(i) < tmincurrent)) then
      tmincurrent = tminside(i)
      iside = i
     endif
    enddo
   else
    do i = 1, 6
     if(side(i) /= sidestop(i) .and. tminside(i) < tmincurrent) then
      tmincurrent = tminside(i)
      iside = i
     endif
    enddo
   endif

  else

   if ((ireverse == 0) .or. (istop /= 1)) then
    do
2120 ioc = ioc + 1
     if (ioc < 7) then
      iside = order(ioc)
      if (side(iside) /= sidelimit(iside)) then
       remg(3)=.false.;exit
      endif
     else
      ioc = 0
     endif
     remg(1)=.false.;cycle
     exit
    enddo
    if (.not.(remg(3))) cycle
   else
    do
2140 ioc = ioc + 1
     if (ioc < 7) then
      iside = order(ioc)
      if (side(iside) /= sidestop(iside)) then
       remg(3)=.false.;exit
      endif
     else
      ioc = 0
     endif
     remg(2)=.false.;cycle
     exit
    enddo
    if (.not.(remg(3))) cycle
   endif

  endif

 endif
 remg(3)=.true.
2130 continue
 exit
enddo

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

A dash to GOTO or a dash from GOTO, that is the question...

Well, guys I saw a question about GOTO for Python. So this gave me the inspiration to attempt a GOTO function for 'dash', (bash and ksh too). Machine: MBP OSX 10.14.3, default bash terminal, calling '#!/usr/local/bin/dash'... This is purely a fun project to see if it is possible in PURE... (3 Replies)
Discussion started by: wisecracker
3 Replies

2. Programming

Problem with IF ELSEIF and GOTO statements in FORTRAN

Hi I am reading a book about Fortran 90 and I write the following code, to test my understanding of the first chapter. I have a problem with the last section of the code with deals with an IF, ELSEIF, and GOTO statements. Here is my Code PROGRAM sim ! This code is used to solve two... (3 Replies)
Discussion started by: faizlo
3 Replies

3. Programming

Strange characters in FORTRAN code output

Hi guys, After compiling a .f90 code and executing it, i get strange characters in the output file like : ^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@ Are these windows characters? how can i get rid of this? Much appreciated. Paul (1 Reply)
Discussion started by: Paul Moghadam
1 Replies

4. Programming

Using tops command to update NSLog statements in objective-c code

In my objective-c code base I have several NSLog statements. eg. NSLog(@"Here is the message without arguments"); NSLog(@"Here is the message with arguments: %s, %s","argument1","argument2"); Now I want to do two things: First to do: updating NSLog statements with... (0 Replies)
Discussion started by: Miraaj
0 Replies

5. Programming

Using Doxygen on Fortran code

I am using doxygen for documenting my fortran code. I want to write some notes after the header in different parts of the subroutine. Any idea what the tags should be as anything I write after the header is not displayed ... (0 Replies)
Discussion started by: kristinu
0 Replies

6. Programming

Help with make this Fortran code more efficient (in HPC manner)

Hi there, I had run into some fortran code to modify. Obviously, it was written without thinking of high performance computing and not parallelized... Now I would like to make the code "on track" and parallel. After a whole afternoon thinking, I still cannot find where to start. Can any one... (3 Replies)
Discussion started by: P_E_M_Lee
3 Replies

7. Programming

Error running FORTRAN code

Hi, I am new to this forum and do not know whether this is the appropriate place to post this question. Anyway am trying my luck. I have a fortran program swanhcat.ftn, which is part of a wave modelling system. There is also a file hcat.nml which is required to run this program. The program's... (9 Replies)
Discussion started by: sandhyakg
9 Replies

8. Programming

Making FORTRAN code more efficient

Hi, I have a very large, very old FORTRAN code that I work with. The code is quite messy and I was wondering if I can speed up execution time by finding subroutines that code execution spends the most time in. Is there any kind of software I can use to see where the code spends most of the... (1 Reply)
Discussion started by: rks171
1 Replies

9. UNIX for Dummies Questions & Answers

Question on variable inheritance & code statements

1) I have the below code in concattxnrecords.sh shell script and it is calling the genericVars.sh shell script which is mentioned as below has some code inside it which would intialize some variables in it, now my question is will this shell script would inherit those variable definitions or not... (3 Replies)
Discussion started by: Ariean
3 Replies
Login or Register to Ask a Question