鍍金池/ 教程/ Java/ Fortran if...else if...else 語(yǔ)句
Fortran還原功能
Fortran關(guān)系運(yùn)算符
Fortran運(yùn)算符優(yōu)先級(jí)
Fortran基本語(yǔ)法
Fortran文件輸入輸出
Fortran嵌套select case結(jié)構(gòu)
Fortran變量
Fortran Cycle語(yǔ)句
Fortran語(yǔ)言環(huán)境設(shè)置
Fortran數(shù)據(jù)類型
Fortran數(shù)組
Fortran字符
Fortran if...else if...else 語(yǔ)句
Fortran調(diào)試程序
Fortran編程風(fēng)格
Fortran if...then語(yǔ)句結(jié)構(gòu)
Fortran嵌套循環(huán)
Fortran常量
Fortran循環(huán)
Fortran導(dǎo)出數(shù)據(jù)類型
Fortran字符串
Fortran操作函數(shù)
Fortran do...while循環(huán)結(jié)構(gòu)
Fortran內(nèi)部函數(shù)
Fortran數(shù)字精度
Fortran選擇決策
Fortran重塑函數(shù)
Fortran運(yùn)算符
Fortran構(gòu)造函數(shù)
Fortran模塊
Fortran位置函數(shù)
Fortran數(shù)字
Fortran指針
Fortran算術(shù)運(yùn)算符
Fortran exit語(yǔ)句
Fortran動(dòng)態(tài)數(shù)組
Fortran嵌套if結(jié)構(gòu)
Fortran select case結(jié)構(gòu)
Fortran向量和矩陣乘法函數(shù)
Fortran邏輯運(yùn)算符
Fortran if...then...else 結(jié)構(gòu)
Fortran教程
Fortran過(guò)程
Fortran Stop語(yǔ)句
Fortran基本輸入輸出
Fortran do循環(huán)結(jié)構(gòu)
Fortran查詢函數(shù)

Fortran if...else if...else 語(yǔ)句

Fortran  if...else if...else 語(yǔ)句

if語(yǔ)句構(gòu)建體可具有一個(gè)或多個(gè)可選的 else-if 結(jié)構(gòu)。當(dāng) if 條件不滿足,則緊跟的 else-if 被執(zhí)行。當(dāng) else-if 還是失敗,其繼續(xù)下一個(gè) else-if 語(yǔ)句(如果有的話)被執(zhí)行,依此類推。

可選的 else 被放置在末端,當(dāng)上述條件不為真時(shí)則執(zhí)行。

  • 所有 else 語(yǔ)句 (else-if 和 else)都是可選的。
  • else-if 可以使用一次或多次
  • else 必須始終被放置在構(gòu)建體的末端,應(yīng)該只出現(xiàn)一次。

語(yǔ)法:

if...else if...else 語(yǔ)句的語(yǔ)法是:

[name:] 
if (logical expression 1) then 
   ! block 1   
else if (logical expression 2) then       
   ! block 2   
else if (logical expression 3) then       
   ! block 3  
else       
   ! block 4   
end if [name]

示例

program ifElseIfElseProg
implicit none

   ! local variable declaration
   integer :: a = 100
 
   ! check the logical condition using if statement
   if( a == 10 ) then
  
      ! if condition is true then print the following 
      print*, "Value of a is 10" 
   
   else if( a == 20 ) then
  
      ! if else if condition is true 
      print*, "Value of a is 20" 
  
   else if( a == 30 ) then
   
      ! if else if condition is true  
      print*, "Value of a is 30" 
  
   else
   
      ! if none of the conditions is true 
      print*, "None of the values is matching" 
      
   end if
   
   print*, "exact value of a is ", a
 
end program ifElseIfElseProg

當(dāng)上述代碼被編譯和執(zhí)行時(shí),它產(chǎn)生了以下結(jié)果:

None of the values is matching
exact value of a is 100

上一篇:Fortran循環(huán)下一篇:Fortran字符串