# FORTRAN Tutorial 8 | Logical Operator | Decision Control

Welcome to the new lesson of the FORTRAN programming tutorial. Before going in, I suggest you go through previous lessons of FORTRAN.

We will focus on Logical Operators in this lesson.

## Logical Operator

Logical Operators in FORTRAN helps to combine two or more expressions to a single one. The logical operators of FORTRAN programming language are: `.or.` `.and.` `.not.`

### OR operator

`.or.` operator combines two or more expressions in such a way that, final result comes `.true`. value if any of the expressions gives .true. value. This operator gives `.false.` value only if all expressions give `.false.` value.

Let me clarify using an example.

First I will show you an individual expressions and combine them later.

``````program operatorLogical
implicit none
print*, 5 == 5
print*, 5 /= 5
print*, 5 > 3
print*, 10 < 8
end program``````

The output is:

``````T
F
T
F``````

The above four expressions give output either `.true.` or `.false.` (T or F). Let’s combine them using `.or.` operator.

``````program operatorLogical
implicit none                      ! first exp:     second exp:
print*, 5 == 5 .or. 5 > 3         !   T              T
print*, 5 /= 5 .or. 5 > 3         !   F              T
print*, 5 > 3  .or. 10 < 8        !   T              F
print*, 10 < 8 .or. 5 /= 5        !   F              F
end program``````

As the `.or.` operator gives `.false.` value only if all expressions give `.false.` value. Only the fourth `print` statement will give `.false.` value as two expressions individually gives `.false.` value.

Output is:

``````T
T
T
F``````

### And operator

`.and.` operator combines two or more expressions in such a way that it gives `.true. `value only if all expressions give .true. value. And this operator gives `.false.` value if any one of the expressions gives `.false.` value.

Let’s see the use in code.

``````program operatorLogical
implicit none                      ! first exp:     second exp:
print*, 5 == 5 .and. 5 > 3         !   T              T
print*, 5 /= 5 .and. 5 > 3         !   F              T
print*, 5 > 3  .and. 10 < 8        !   T              F
print*, 10 < 8 .and. 5 /= 5        !   F              F
end program``````

Only the first print statement will give `.true.` value because both expressions used there give `.true.` value individually.

The output is:

``````T
F
F
F``````

### Not operator

`.not.` operator is a negation of any operator. This `.not.` operator is always used with the other two operators `.or.` and `.and.` and reverses the output given by those operators.

Look at the code for better understanding.

``````program not_operator
implicit none
print*, 10 < 8 .or. 5 /= 5
print*, .not.(5 > 3  .or. 10 < 8)    ! .not. of previous
print*, 5 == 5 .and. 5 > 3
print*, .not. (5 == 5 .and. 5 > 3)    ! .not. of previous
end program``````

The output is:

``````F
T
T
F``````

## Logical Operators in if statement

We can use those logical operators in if statement for decision making. Sometimes it helps to make our code shorter, using logical operators instead of nested if statement.

Here is a code with nested if statement.

``````program with_if
implicit none
integer :: marks = 89
if (marks > 80) then
if (marks <= 90) then
print*, "you did excellent."
end if
end if
end program``````

Since the expression in both `if statement` gives `.true.` value for the `marks` value, the `print` statement will execute.

``you did excellent.``

But the same can be done using a logical operator.

``````program with_logical
implicit none
integer :: marks = 89
if (marks > 80 .and. marks <= 90) then
print*, "you did excellent."
end if
end program``````

The output is still the same but we have made our code shorter than previous.

The output is:

``you did excellent.``

Keep Learning.

Watch the video tutorial of logical operator for better understanding.