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

SELECT CASE construct

netphilou31
New Contributor III
1,754 Views

Hi,

I have a very basic question related to the SELECT CASE construct.

The documentation tells that is is possible to branch to the END SELECT statement only from within the SELECT CASE construct but it does not mention how to do that. My problem, is that I would like to be able to exit a CASE block before the end of the block. Is there something I missed?

Best regards,

Phil.

0 Kudos
12 Replies
Arjen_Markus
Honored Contributor II
1,754 Views

I do not think I have ever used it, but the EXIT statement comes to mind, in particular:

myselect: select case ( myvar )

    case( 1 )

         exit myselect

end select myselect

I would use the naming to make sure it exits the select construct.

Regards,

Arjen

 

0 Kudos
netphilou31
New Contributor III
1,754 Views

Hi Arjen,

Thanks for the suggestion, but the exit statement can only be used in a DO construct, using it into a CASE block generates a compiler error.

error #6604: The CYCLE or EXIT statement must belong to a particular DO construct, and they do not.

Best regards,

Phil.

0 Kudos
Steven_L_Intel1
Employee
1,754 Views

Using EXIT in a SELECT construct is a F2008 feature we don't support yet.

0 Kudos
netphilou31
New Contributor III
1,754 Views

Hi Steve,

Thanks for the reply, however what about the documentation telling that is possible to branch to the END SELECT statement from within the SELECT CASE construct? Is it also part of the F2008 feature which is not supported in the current version of the compiler?

Best regards,

Phil.

0 Kudos
LRaim
New Contributor I
1,754 Views

A GOTO  statement can solve many problems and make the code more readable (both in fortran and C++)

Best regards

 

0 Kudos
netphilou31
New Contributor III
1,754 Views

Hi Luigi,

If possible I would like to avoid the need to use a goto statement. I have finally managed the things differently but I am currious to know if the documentation is correct or not with respect to the implementation of the Fortran standards in my current release (IVF 14.0.0.103).

Best regards,

Phil.

0 Kudos
Steven_L_Intel1
Employee
1,754 Views

I'm looking at the current documentation and the only statement I see on this is " Branching to the END SELECT statement is allowed only from within the CASE construct." This typically would be done with a GOTO (though other branch constructs could be used). What does the text you're looking at say?

0 Kudos
netphilou31
New Contributor III
1,754 Views

Yes Steve, That's exactly the statement I was refering to. I was wondering how branching could be done (which set of instructions to use). I was expecting a more elegant way than a single GOTO. What type of "other branch construct" do you have in mind?

Best regards,

Phil.

0 Kudos
Steven_L_Intel1
Employee
1,754 Views

Arithmetic IF, ERR=, assigned GOTO, ... Compared to these, a regular GOTO is elegant. Nothing wrong with a GOTO if used properly, but eventually you'll be able to avoid it here.

I suppose an alternative is to use IF-THEN to block out the rest of the statements in the CASE block.

0 Kudos
netphilou31
New Contributor III
1,754 Views

You are right, a regular GOTO is always more elegant than the other possibilities. I have finally used a IF...THEN...ELSE construct. However having the possibility to use an EXIT statement would be more interesting especially when one of the two branches of the IF...THEN...ELSE block is particularly big. I hope that this will be implemented soon.

Best regards,

Phil.

0 Kudos
FortranFan
Honored Contributor III
1,754 Views

netphilou31 wrote:

You are right, a regular GOTO is always more elegant than the other possibilities. I have finally used a IF...THEN...ELSE construct. However having the possibility to use an EXIT statement would be more interesting especially when one of the two branches of the IF...THEN...ELSE block is particularly big. I hope that this will be implemented soon.

Best regards,

Phil.

With update 1 to 16.0 beta, one should be able to use EXIT with BLOCK construct and this can be another option:

   blk: block
   
      slc: select case ( foo )
         
         case ( FOO_1 ) slc
            
            ..
            
            exit blk
            
         case ( FOO_2 ) slc
            
            ..
            
         case default slc
            
      end select slc
      
   end block blk

 

 

0 Kudos
netphilou31
New Contributor III
1,754 Views

Great !

0 Kudos
Reply