Select Case statement is another tool for logical decision in FORTRAN.
We have used
if statement to control decisions in FORTRAN in the previous lessons. But
Select Case statement gives more control over the
In the previous lessons, we used a logical operator with
if statement to completer our task. The code became difficult to understand and as well as the length became messy. However, with the use of the Select Case statement, we can solve those problems. The code with the use of Select Case statement becomes easy to understand and can do complex tasks as well.
Fortran Select Case Statement Syntax
The syntax for select case statement is:
select case (expression OR variable name) case (value) statement........ case (value) statement........ ..... case default statement....... end select
The use of select case statement is also in decision control like
if statement. So the syntax might look somehow similar to that of
We can write many
case () inside
select case like we write many
else if inside
if statement. And
case default for the final condition like as
Now let’s see the select case statement in code.
Use of Select Case Statement in FORTRAN code
a) With Variable
When we write a variable name inside the parenthesis of
select case syntax, we have to use the value in the
case statement. When the variable value matches with
case value, then the statement inside that case will execute.
Let’s see one example.
program select_ex implicit none integer :: age = 15 select case (age) case (5) print*, "You're a kid." case (6,7,8,9) print*, "You are still a kid." case (10:20) print*, "you are young." case default print*, "Age Value ERROR!" end select
The output is:
You are young.
As you see in
case (10 : 20) we give a range of values. This couldn’t be done in the if statement. And also we can give many values like shown above in case (6,7,8,9) in select case statement in FORTRAN
if statement that line should be written like this:
if (age >= 10 .and. age <20) then print*, "You are young." end if
As you see, the code with
if statement becomes long which is not a good way to write a program.
Now try yourself changing the value of
age variable and see the output.
case value doesn’t match with the
age variable value, then
case default statement will execute.
Let’s have a look at one more example:
program select_ex implicit none character(len=2) :: grade = 'E' select case (grade) case ('A') print*, "Excellent" case ('B': 'D') print*, "Good" case default print*, "Bad" end select
This time, I used character data type. Now play with the code with different examples.
And there is one thing to remember while using select case statement in FORTRAN. The value should not be overlapped with other
for e.g. Look at this code below:
select case (age) case (4,5,6,7) ... case (7: 10) .... end select
The above code gives ERROR as the values used in both case has some similar value. So we should avoid such an error of overlapping of values.
b) With expressions
We can also give expression inside
select case parenthesis, as discussed in the syntax above.
Let’s see one example:
program select_ex implicit none integer :: age = 15 select case (age == 15) case (.true.) print*, "You're a kid." case (.false.) print*, "you are young." end select
The output is:
You're a kid.
Why is that?
Well, We use expression inside the parenthesis. And we know that expression gives either
.false. value. Hence, We have only two choices in
case value i.e. either
Let’s look at the expression used inside the parenthesis. The expression
age == 15 gives True value and hence the statement inside first
case is printed out.
Fortran select case statement is easy to use and more versatile than regular
if statement. The use of select case statement not only makes our code easier to read but also helps in complex calculations.
Select case statement is mostly used for decision control requiring many conditions to satisfy. In most of the physics complex problems, I have seen the use of a select case statement.
If you want to learn through a video, then I have that covered for you as well. CLICK HERE TO WATCH VIDEO.