Fortran-If-then-else-construct

提供:Dev Guides
移動先:案内検索

Fortran-If-then-elseコンストラクト

if…then ステートメント」の後に、オプションの「* elseステートメント」を続けることができます。これは、論理式が偽の場合に実行されます。

構文

*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 ブロック内のコードブロックが実行されます。

流れ図

フロー図1

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