fortran select case
In Fortran, the SELECT CASE
construct is used to perform different actions based on the value of an expression. It is similar to the switch
statement in other programming languages.
Here is the basic syntax of the SELECT CASE
construct in Fortran:
SELECT CASE (expression)
CASE (value1)
! statements executed when expression equals value1
CASE (value2)
! statements executed when expression equals value2
CASE (value3)
! statements executed when expression equals value3
...
CASE DEFAULT
! statements executed when none of the above cases match
END SELECT
The expression
can be any scalar expression, such as a variable, a constant, or an arithmetic expression.
The CASE
statement is used to define specific values that the expression
can take. When the expression
matches one of the CASE
values, the corresponding block of statements is executed. If none of the CASE
values match the expression
, the statements under the CASE DEFAULT
block are executed.
Here's an example to illustrate the usage of SELECT CASE
in Fortran:
PROGRAM SelectCaseExample
IMPLICIT NONE
INTEGER :: num
WRITE(,) "Enter a number:"
READ(,) num
SELECT CASE (num)
CASE (0)
WRITE(,) "The number is zero."
CASE (1)
WRITE(,) "The number is one."
CASE (2, 3, 4)
WRITE(,) "The number is two, three, or four."
CASE DEFAULT
WRITE(,) "The number is not zero, one, two, three, or four."
END SELECT
END PROGRAM SelectCaseExample
In this example, the user is prompted to enter a number. Based on the value entered, the program uses a SELECT CASE
construct to determine which block of statements to execute.
I hope this explanation helps you understand how to use the SELECT CASE
construct in Fortran. If you have any further questions, feel free to ask.