How to BREAK the program with a message when some condition occurs

10,220
if (condition) stop

will bring your program to a stop immediately. You might prefer

if (condition) then
   write(*,*) 'A friendly message'
   stop
end if

If your compiler is Fortran 2008 compliant you can even write

if (condition) stop 'A friendly message'

However, perhaps what you want to do is not stop your program but exit from the subroutine in which case you would jump to the end of the subroutine, in some acceptable fashion.

Note well that condition=true is not syntactically correct Fortran to compare the value of condition with the logical constant .true.. It is an assignment statement. The syntactically correct comparison would be condition == .true. but that is semantically noxious, simply writing if (condition) expresses everything that if (condition == .true.) does. The abbreviated form also suggests that you are a programmer rather than a script-kiddy.

Share:
10,220
APuig
Author by

APuig

Updated on July 16, 2022

Comments

  • APuig
    APuig almost 2 years

    I am trying to find a way to put a breaking order when some condition occurs in one subroutine of my f90 program. Is it possible to have any ideas from it? the code scheme looks like this:

        /
        modules
        PROGRAM
        allocate variables
        CALL subroutines for initializing variables
        ...
        do 1,max iterations
        CALL subroutine1
        CALL subroutine2
        CALL subroutine3 !--> here I have the condition
        ...
        ...
        end do
    
        END PROGRAM
    
        Subroutine subroutine3 
        ...
        if (condition = true) then ! what I want to do here is to break the program printing a message saying that it is stopped because condition is true)
    
        end if
        end subroutine 3
    
        /
    

    I would appreciate your help,

    I am quite new with fortran and I am new in this forum!

    Thank you in advance,

    Albert P

  • Hristo Iliev
    Hristo Iliev over 11 years
    "The abbreviated form also suggests that you are a programmer rather than a script-kiddy." LOL, made my day! :)
  • APuig
    APuig over 11 years
    thanks again, I was just putting redundant stuff, I was aware about the 'condition = true'