In this guide, we will discuss Fortran if-then-else construct. An if… then statement can be followed by an optional else statement, which executes when the logical expression is false.
Syntax
>
The basic syntax of an if… then… else statement is −
if (logical expression) then statement(s) else other_statement(s) end if
However, if you give a name to the if block, then the syntax of the named if-else statement would be, like −
[name:] if (logical expression) then ! various statements . . . else !other statement(s) . . . end if [name]
If the logical expression evaluates to true, then the block of code inside the if…then statement will be executed, otherwise the block of code inside the else block will be executed.
Flow Diagram
Example
program ifElseProg implicit none ! local variable declaration integer :: a = 100 ! check the logical condition using if statement if (a < 20 ) then ! if condition is true then print the following print*, "a is less than 20" else print*, "a is not less than 20" end if print*, "value of a is ", a end program ifElseProg
When the above code is compiled and executed, it produces the following result −
a is not less than 20 value of a is 100
Next Topic : Click Here
Pingback: Fortran - if-then construct | Adglob Infosystem Pvt Ltd
Pingback: Fortran - Decisions | Adglob Infosystem Pvt Ltd