[UP]


Manual Reference Pages  - select (7)

NAME

select(7f) - [FORTRAN:EXECUTION CONTROL] select a block based on the value of an expression (a case)

CONTENTS

Synopsis
Description

SYNOPSIS

1 The CASE construct selects for execution at most one of its constituent blocks. The selection is based on the
value of an expression.
R810 case-construct is select-case-stmt [ case-stmt block ] ... end-select-stmt
R811 select-case-stmt is [ case-construct-name : ] SELECT CASE case-expr)
R812 case-stmt is CASE case-selector [case-construct-name]
R813 end-select-stmt is END SELECT [ case-construct-name ]
C809 ((R810)) If the select-case-stmt of a case-construct specifies a case-construct-name, the corresponding end-select-stmt shall specify the same case-construct-name. If the select-case-stmt of a case-construct does not specify a case-construct-name, the corresponding end-select-stmt shall not specify a case-construct- name. If a case-stmt specifies a case-construct-name, the corresponding select-case-stmt shall specify the same case-construct-name.
R814 case-expr is scalar-int-expr or scalar-char-expr or scalar-logical-expr
R815 case-selector is ((case-value-range-list)) or DEFAULT
C810 ((R810)) No more than one of the selectors of one of the CASE statements shall be DEFAULT.
R816 case-value-range is case-value
or case-value :
or : case-value
or case-value : case-value
R817 case-value is scalar-int-initialization-expr or scalar-char-initialization-expr or scalar-logical-initialization-expr
C811 ((R810)) For a given case-construct, each case-value shall be of the same type as case-expr . For character type, the kind type parameters shall be the same; character length differences are allowed.
C812 ((R810)) . case-value-range using . colon shall not be used if case-expr is of type logical.
C813 ((R810)) For . given case-construct, there shall be no possible value of the case-expr that matches more than one case-value-range.

DESCRIPTION

8.1.5 CASE construct
8.1.5.1 Purpose and form of the CASE construct

8.1.5.2 Execution of . CASE construct
   The execution of the SELECT CASE statement causes the case expression
   to be evaluated. The resulting value is called the case index. For a
   case value range list, a match occurs if the case index matches any
   of the case value ranges in the list. For a case index with a value
   of c, a match is determined as follows.

1. If the case value range contains a single value v without a colon, a match occurs for type logical if the expression c .EQV. v is true, and a match occurs for type integer or character if the expression c == v is true. 2. If the case value range is of the form low : high, a match occurs if the expression low <= c .AND. c <= high is true. 3. If the case value range is of the form low :, a match occurs if the expression low <= c is true. 4. If the case value range is of the form : high, a match occurs if the expression c <= high is true. 5. If no other selector matches and a DEFAULT selector appears, it matches the case index. 6. If no other selector matches and the DEFAULT selector does not appear, there is no match.

The block following the CASE statement containing the matching selector, if any, is executed. This completes execution of the construct.

It is permissible to branch to an end-select-stmt only from within its CASE construct.

8.1.5.3 Examples of CASE constructs
       An integer signum function:

integer function signum (n) select case (n) case (:-1) signum = -1 case (0) signum = 0 case (1:) signum = 1 end select end

A code fragment to check for balanced parentheses:

character (80) :: line ... level = 0 scan_line: do i = 1, 80 check_parens: select case (line (i:i)) case (’(’) level = level + 1 case (’)’) level = level - 1 if (level < 0) then print *, ’unexpected right parenthesis’ exit scan_line end if case default ! ignore all other characters end select check_parens end do scan_line if (level > 0) then print *, ’missing right parenthesis’ end if

the following three fragments are equivalent:

if (silly == 1) then call this else call that end if select case (silly == 1) case (.true.) call this case (.false.) call that end select select case (silly) case default call that case (1) call this end select

A code fragment showing several selections of one block:

select case (n) case (1, 3:5, 8) ! selects 1, 3, 4, 5, 8 call sub case default call other end select


select (7) March 19, 2019
Generated by manServer 1.08 from 1f666916-9302-49b1-9830-f417874e53ea using man macros.