Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

named IF construct

andrew_4619
Honored Contributor II
467 Views

I was just looking at section of old code I wrote maybe 15 or more years ago that has some IF ELSEIF ELSE ENDIF constructs with some longish program blocks. There were some conditional GOTOs in some of those blocks branching out of the construct to a numbered CONTINUE after the ENDIF. I pretty much never need to write anything with numbered lines anymore as there are so many neater ways of doing things these days. So I asked myself how would I refactor that if I was doing it as new code today? Well the answer is the named IF construct which I think may of snuck in with F2008 without me noticing! So looking at the Intel help we see:

 

[name:] IF (expr) THEN
    block
[ELSE IF (expr) THEN [name]
   block]
[ELSE [name]
   block]
END IF [name]

 

But there is no indication, example or link as to what one could do with the optional construct name. OK to the internet, can I find any example using our good friend google which is usually always useful..... nope a total fail. Does anyone use named IF constructs?

Anyway taking a punt I wrote the test code shown below which works as expected with no error or standards warnings. And then delving into the Intel help a read the EXIT topic which provides some useful information.

 

integer :: mnd_pts, l1
mnd_pts = 2     
chk: if ( mnd_pts > 0 ) then
    l1 = 5
    if (mnd_pts > 1 ) exit chk
    l1 = 10
else chk
    l1 = 15
endif chk

 

Anyway I think that the Intel Help is not so friendly for someone relatively new to Fortran so an example in the IF ENDIF topic or a perhaps a line linking to the EXIT topic would be a good improvement IMO.

Finally is the optional name on ELSE and ELSEIF of any practical use other than signalling the linkage to the IF ENDIF to aid code documentation, I can't see any use unless there is something else I am missing?

 

0 Kudos
2 Replies
FortranFan
Honored Contributor II
460 Views

Your suggestion to the Intel team to link to the help on EXIT statement is a good one.  As you noticed, it is with the EXIT statement the named constructs become useful when it comes to execution control i.e., beyond a visual aid.

It is good the Fortran standard starting with Fortran 2008 permits EXIT with all named constructs including IF.. ELSE.. ENDIF barring a few (obvious) exceptions (DO CONCURRENT, CHANGE TEAM, and CRITICAL),

0 Kudos
jimdempseyatthecove
Honored Contributor III
458 Views

>>Finally is the optional name on ELSE and ELSEIF of any practical use other than signalling the linkage to the IF ENDIF to aid code documentation, I can't see any use unless there is something else I am missing?

When using several levels of nesting of conditional statements the old (error prone) procedure was try to make sense of the nest level through indentation of the source code. This is (was) too error prone as editing might introduce unintended errors.

The inclusion of the block name will enhance the compiler error detection and provide the programmer better insight as to where the error occurred.

 

Jim Dempsey

0 Kudos
Reply