Fortran - If-then-else 构造
-
简述
一个 if… then语句后面可以跟一个可选的 else statement,当逻辑表达式为假时执行。句法
>的基本语法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