
- Fortran - Home
- Fortran - Overview
- Fortran - Environment Setup
- Fortran - Basic Syntax
- Fortran - Data Types
- Fortran - Variables
- Fortran - Constants
- Fortran - Operators
- Fortran - Decisions
- Fortran - Loops
- Fortran - Numbers
- Fortran - Characters
- Fortran - Strings
- Fortran - Arrays
- Fortran - Dynamic Arrays
- Fortran - Derived Data Types
- Fortran - Pointers
- Fortran - Basic Input Output
- Fortran - File Input Output
- Fortran - Procedures
- Fortran - Modules
- Fortran - Intrinsic Functions
- Fortran - Numeric Precision
- Fortran - Program Libraries
- Fortran - Programming Style
- Fortran - Debugging Program
Fortran - If-then-else construct
Anif then statementcan be followed by an optionalelse 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 ifthen 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
fortran_decisions.htm
Advertisements