Fortran If-then-else 语句
if…then 语句后面可以跟一个可选的 else 语句,当逻辑表达式为 false 时执行。
if…then…else 语句的基本语法是:
if (logical expression) then
statement(s)
else
other_statement(s)
end if
但是,如果为 if 块命名,则命名的 if-else 语句的语法如下:
[name:] if (logical expression) then
! various statements
. . .
else
!other statement(s)
. . .
end if [name]
如果逻辑表达式的计算结果为 true,则 if…then 语句中的代码块将被执行,否则 else 块中的代码段将被执行。
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
结果为:
a is not less than 20
value of a is 100