 
- 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 - Nested If Construct
You can use oneiforelse ifstatement inside anotheriforelse ifstatement(s).
Syntax
The syntax for a nested if statement is as follows −
if ( logical_expression 1) then
   !Executes when the boolean expression 1 is true 
   
   
   if(logical_expression 2)then 
      ! Executes when the boolean expression 2 is true 
      
   end if
end if
Example
program nestedIfProg
implicit none
   ! local variable declaration
   integer :: a = 100, b= 200
 
   ! check the logical condition using if statement
   if( a == 100 ) then
  
   ! if condition is true then check the following 
      
   if( b == 200 ) then
  
   ! if inner if condition is true 
   print*, "Value of a is 100 and b is 200" 
  
   end if
   end if
   
   print*, "exact value of a is ", a
   print*, "exact value of b is ", b
 
end program nestedIfProg
When the above code is compiled and executed, it produces the following result −
Value of a is 100 and b is 200 exact value of a is 100 exact value of b is 200
fortran_decisions.htm
   Advertisements