Fortran 嵌套 If 语句

您可以在另一个 ifelse if 语句中使用一个 ifelse if 语句。

嵌套 if 语句的语法如下:

  1. if ( logical_expression 1) then
  2. !Executes when the boolean expression 1 is true
  3. if(logical_expression 2)then
  4. ! Executes when the boolean expression 2 is true
  5. end if
  6. end if
实例
  1. program nestedIfProg
  2. implicit none
  3. ! local variable declaration
  4. integer :: a = 100, b= 200
  5. ! check the logical condition using if statement
  6. if( a == 100 ) then
  7. ! if condition is true then check the following
  8. if( b == 200 ) then
  9. ! if inner if condition is true
  10. print*, "Value of a is 100 and b is 200"
  11. end if
  12. end if
  13. print*, "exact value of a is ", a
  14. print*, "exact value of b is ", b
  15. end program nestedIfProg

结果为:

  1. Value of a is 100 and b is 200
  2. exact value of a is 100
  3. exact value of b is 200

分类导航