FORTRAN Tutorial Lesson 7 | If … else if statement

Else if statement works in the same way as if else works. But only difference is, more expressions can be used in else if statement.

Before you move on to this lesson, be sure to go through the previous lessons. Check out other lessons.

Need of ELSE IF statement

If you have learned through my previous lesson, then you know that we have used if condition till now, only to check one condition. With the help of if else statement, we only get to choose to execute anyone from the two conditions — one condition of if and another of else.

But what if we need to check for more conditions (or expressions), that could be done using else if statement.

Here is an example to think about the necessity of else if statement.

Suppose, we have a variable named age and a certain value stored in it. And we want to check that value with many other values, not only to one value as we did in if else statement. Let’s say, if the age value is 10, we want to say “you are a kid“, age value 20, say “you are a youth ” and soon.

In such case, else if statement works as per our desire.

Else IF statement syntax

Here is a syntax for if..else if statement:

if (expression) then
   code........
else if (expression) then
   code..........
else if (expression) then
    code........
..
..
..
else
   code...........
end if

Now we can evaluate many expressions, using in else if and execute statements based on the condition.

Whichever expressions give .true. value, statements inside that else if, will execute. If none of the expressions give .true. value, then else expression executes.

Else if statement examples

program example
  implicit none
    integer :: age = 20
     if (age == 10) then
        print*,"you are still a kid."
    else if (age == 20) then
       print*, "you are youth now."
   else if (age == 30) then
      print*, "it's time to get married."
   else if (age == 40) then
     print*, "Spend quality time with your children."
   else
    print*, "Just do what you wanna do! "
    end if
end program

Guess the output and see the answer.

The output is:

you are youth now.

So we give age == 20 then our program check the condition of age in each expression turn by turn. The first expression age == 10 gives .false. value. So it goes to the second expression and checks the expression age == 20. Yes, age == 20 gives .true. value. hence, the statement inside this else if is executed.]

Labeling if statement

FORTRAN also has a feature of labeling if we have many if statements inside our program.

Labeling is done using following syntax:

name: if (expression) then
         code goes here.........
end if name

This labeling might help us, only if we have many if statements and we give the best labeling name to if statement.

For example:

program label
  implicit none
   first_if:  if (4==4) then
                 print*, "labeling is good when there are many if."
             end if first_if
   second_if:  if ( 5 <= 8) then
                print*, "Better to give a relevant name for the label while using such format."
              end if second_if
end program

The output is:

labeling is good when there are many if.
Better to give a relevant name for the label while using such format.

If statement shorthand way

There is one good way of writing simple if statement. When single statement is inside if statement then, we can use the shorthand way. I will show you two examples, both works the same.

Regular way of writing if statement:

program regular
  implicit none
     integer :: x = 4
        if (x==4) then 
              print*, "The value of x is 4."
        end if
end program

Shorthand way of writing:

program shorthandWAY
  implicit none
     integer :: x = 4
        if (x==4) print*, "The value of x is 4."
end program

The output is same:

The value of x is 4.

Nested if statement

Nested means: one inside the same one. The term nested in if statement means: we can put many if statement inside the if statement.

Nested if statement gives more control over the decision.

Here is the syntax to write nested if.

if (expression) then
   if (expression) then     ! nested if (if inside if)
    code....
   else
       code....
   end if
else
   code.......
end if

There can be many formats of nested if as including separate if statement inside anywhere of one if statement is possible.

Here is a simple example to describe a use of nested code.

Suppose we want to execute a certain statements only if two expressions are satisfied. Ok, we have a number 40 and if and only if it passes two expressions given by nested if passes, then certain statement can execute.

Lets see the code for now:

program nestedIF
  implicit none
   integer :: number = 40
     if (number < 50) then
          if (number > 30) then
                print*, "number is between 30 and 50."
          else
                print*, "number is less than 50 but not greater than 30."
          end if
     else
          print*, "number is not less than 50."
     end if
end program

Try to figure out the code. I have used line indentation for better understanding.

The output is:

number is between 30 and 50.

Lets see line by line.

number == 40 is given. The program always goes line by line. So, it first goes to the first if statement. It checks the expressions inside it. The expression number < 50 gives .true. value. So, we can cancel the else part of that first if statement.

else
print*, “number is not less than 50.”
end if

Then the program sees nested if expression. The expression number > 30 gives .true. value. So we can exclude else of nested if from our list.

else
print*, “number is less than 50 but not greater than 30.”
end if

else
print*, “number is not less than 50.”
end if
end program

Hence the expression inside of two’s if are executed.

Enjoy coding in FORTRAN.

Watch video tutorial on Else if statement for better understanding.

Share this post:

Leave a Comment

Your email address will not be published. Required fields are marked *