# Fortran Tutorial Lesson 6 | Decision Control | If Else statement

Welcome to the tutorial of Decision Control in FORTRAN Programming Language.

Before you start, I would suggest you to go through the previous lessons.

## Decision Control

Like we humans control the decision, the program also has control over the decision with the help of code. Every high-level programming language has such a feature.

FORTRAN program can do decision control with the help of if else statement.

## Scenario to use Decision control

Let me show you one example, that shows the need of decision control.

``````program decision
implicit none
print*, "I want to marry you, my love."
end program``````

The output of the code is:

``I want to marry you, my love.``

It’s obvious to get this output, as only one print statement used in a code tells us to print this text.

Now let’s create one scenario: we need that text to print only if the age of the girl is over 20.

we don’t want to execute that `print*,` statement all the time. We want the program to decide whether or not to execute that, based upon the girl’s age.

There comes the use of decision control.

If statement can control the decision in FORTRAN . There are many ways to use if statement. They are: if statement, if…else statement, if….elif…else statement and nested if statment.

## If statement

If statement is used to control the decision in FORTRAN or any other programming language. But, the way of writing the code (syntax) differs for all.

The syntax(i.e. way to write a code) to write if statement is:

``````if (conditions or logical expressions) then
code...........
...............
end if``````

### How if statement works?

If the expressions inside the parenthesis (bracket) gives `.true. `value then the code inside it is executed. And if expression gives `.false. `value then the code is not executed.

Now let’s see the code

``````program decision
implicit none
integer :: girls_age = 18
if (girls_age > 20) then
print*, "I want to marry you, my love."
end if
end program``````

Guess the output.

We first create `girls_age` variable and store an integer value 18 in it. We then use if statement to provide a condition for the code to be executed.

First, program sees the condition (expression) and evaluates it. Since the `girls_age` is provided with `18` value which is not greater than `20`, the expression gives `.false. `value. Hence, the code inside if statement does not execute.

The output is nothing here.

Now lets change the value and run the program.

``````program decision
implicit none
integer :: girls_age = 21
if (girls_age > 20) then
print*, "I want to marry you, my love."
end if
end program``````

This time, `girls_age` value is `21` which is greater than `20`. So, the expression `girls_age > 20` gives `.true.` value and the code inside it is executed.

The output is:

``I want to marry you, my love.``

You can put many statements inside if.

``````program decision
implicit none
integer :: girls_age = 21
if (girls_age > 20) then
print*, "Now you reached legal age."
print*, "I love you so much."
print*, "I want to marry you, my love."
end if
end program``````

Outptut:

``````Now you reached legal age.
I love you so much.
I want to marry you, my love.``````

## If else statement

Now we want our program to execute some statement, even if the expression gives `.false.` value. For that purpose, we use if…else statement.

syntax for if else statement:

``````if (expressions) then
code........
............
else
code........
............
end if``````

Now we got two places to write the code. One inside `if`and `else`, and another inside `else` and `end if`.

### How if else statement works?

When the expression provided inside parenthesis gives `.true.` result, the code inside `if` and `else` is executed. and when the expression gives `.false.` value, the code inside `else` and `end if` is executed.

Let’s have a look at one example.

``````program ex
implicit none
integer :: my_money
my_money = 90
if (my_money .ge. 100) then        ! .ge. means >= (greater or equal)
print*, "I can buy a book."
else
print*, "I don't have enough money."
end program``````

output:

``I don't have enough money.``

I think you understood, how this output came. The expression gave .false. value. So, `else` code is executed. Let’s change the variable value once again.

``````program ex
implicit none
integer :: my_money
my_money = 100
if (my_money .ge. 100) then        ! .ge. means >= (greater or equal)
print*, "I can buy a book."
else
print*, "I don't have enough money."
end program``````

Output is:

``I can buy a book.``

The expression provided inside if parenthesis always gives either `.true.` or `.false. `value.So, instead of providing expression, we can also directly provide the value.

Lets look at the examples.

``````program ex
implicit none
if (.true.) then
print*, "I can buy a book."
else
print*, "I don't have enough money."
end program``````

Output:

``I can buy a book.``

Lets change the logical value `.true. `to `.false.`

``````program ex
implicit none
if (.false.) then
print*, "I can buy a book."
else
print*, "I don't have enough money."
end program``````

Output:

``I don't have enough money.``

Other types of if statement and their examples are in the next lesson.

You can also video tutorial on if else statement for better understanding.