Fortran If-then-else 语句

if…then 语句后面可以跟一个可选的 else 语句,当逻辑表达式为 false 时执行。

if…then…else 语句的基本语法是:

  1. if (logical expression) then
  2. statement(s)
  3. else
  4. other_statement(s)
  5. end if

但是,如果为 if 块命名,则命名的 if-else 语句的语法如下:

  1. [name:] if (logical expression) then
  2. ! various statements
  3. . . .
  4. else
  5. !other statement(s)
  6. . . .
  7. end if [name]

如果逻辑表达式的计算结果为 true,则 if…then 语句中的代码块将被执行,否则 else 块中的代码段将被执行。

  1. program ifElseProg
  2. implicit none
  3. ! local variable declaration
  4. integer :: a = 100
  5. ! check the logical condition using if statement
  6. if (a < 20 ) then
  7. ! if condition is true then print the following
  8. print*, "a is less than 20"
  9. else
  10. print*, "a is not less than 20"
  11. end if
  12. print*, "value of a is ", a
  13. end program ifElseProg

结果为:

  1. a is not less than 20
  2. value of a is 100

分类导航