Fortran if-else if-else 语句
if 语句可以有一个或多个可选的 else if。如果 if 条件失败,则执行紧随其后的 else if。当 else if 也失败时,执行其后续 else if 语句(如果有的话),依此类推。
可选的 else 放在末尾,当上述条件均不成立时执行。
- 所有其他语句(else if 和 else)都是可选的。
- 否则,如果可以使用一次或多次。
- else 必须始终放置在构造的末尾,并且只应出现一次。
if…else if…else 语句的语法是:
[name:]if (logical expression 1) then! block 1else if (logical expression 2) then! block 2else if (logical expression 3) then! block 3else! block 4end if [name]
实例
program ifElseIfElseProgimplicit none! local variable declarationinteger :: a = 100! check the logical condition using if statementif( a == 10 ) then! if condition is true then print the followingprint*, "Value of a is 10"else if( a == 20 ) then! if else if condition is trueprint*, "Value of a is 20"else if( a == 30 ) then! if else if condition is trueprint*, "Value of a is 30"else! if none of the conditions is trueprint*, "None of the values is matching"end ifprint*, "exact value of a is ", aend program ifElseIfElseProg
结果为:
None of the values is matchingexact value of a is 100