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]

如果邏輯表達式的計算結果爲代碼裏面的 if ... then 語句會被執行,對 else 塊中的代碼,否則該塊將被執行 else 塊。

流程圖

Flow

示例

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