Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 10 HP Fortran statements

CASE

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

Marks start of statement block in a CASE construct.

Syntax

CASE ( case-selector ) [ construct-name ]
case-selector

is a comma-separated list of ranges of values that are candidates for matching against the case index specified by the SELECT CASE statement. Each item in the list can take one of the following forms:

  • case-value

  • low:

  • :high

  • low:high

  • DEFAULT

where:

case-value, low, and high

are scalar initialization expressions of type integer, character, or logical

DEFAULT

indicates the statement block to execute if none of the other CASE statements in the CASE construct produces a match.

construct-name

is the name given to the CASE construct.

Description

The CASE statement is used in a CASE construct to mark the start of a statement block. The CASE construct can consist of multiple blocks; at most, one is selected for execution. Selection is determined by comparing the case index produced by the SELECT CASE statement to the case-selector in each CASE statement. If a match is found, the statement block under the matching case-selector executes. A match between the case index (c) and case-selector is determined for each form of case-selector, as follows:

case-value

For integer and character types, a match occurs if c .EQ. case-value.

For logical types, a match occurs if c .EQV. case-value.

low:

For integer and character types, a match occurs if c .GE. low.

:high

For integer and character types, a match occurs if c .LE. high.

low : high

For integer and character types, a match occurs if c .GE. low .AND. c .LE. high.

DEFAULT

For integer, character, and logical types, a match occurs if no match is found with any other case-selector and DEFAULT is specified as a case-selector.

If CASE DEFAULT is not present and no match is found with any of the other CASE statements, none of the statement blocks within the CASE construct executes and execution resumes with the first executable statement following the END SELECT statement.

At most only one DEFAULT selector can appear within a CASE construct.

Each CASE statement must specify a unique value or range of values within a particular CASE construct. Only one match can occur, and only one statement block can execute.

All case-selectors and the case index within a particular CASE construct must be of the same type: integer, character, or logical. However, the lengths of character types can differ.

The colon forms—low:, :high, or low:high—are not permitted for a logical type.

Although putting the CASE statements in order according to range may improve readability, it is not necessary for correct or optimal execution of the CASE construct. In particular, DEFAULT can appear anywhere among the CASE statements and need not be the last.

CASE statements inside a named CASE construct need not specify construct-name; but if they do, the name they specify must match that of the SELECT CASE.

A CASE statement can have an empty statement block.

Examples

The following example considers a person’s credits and debits and prints a message indicating whether a resulting account balance will be overdrawn, empty, uncomfortably small, or sufficient:

INTEGER :: credits, debits

SELECT CASE (credits - debits)
CASE (:-1)
PRINT *, 'OVERDRAWN'
CALL TRANSFERFUNDS
CASE (0)
PRINT *, 'NO MONEY LEFT'
CASE (1:50)
PRINT *, 'BALANCE LOW'
CASE (51:)
PRINT *, 'BALANCE OKAY'
END SELECT

Related statements

SELECT CASE and END (construct)

Related concepts

The CASE construct is described in “CASE construct”.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.