鍍金池/ 教程/ Java/ Fortran if...then語(yǔ)句結(jié)構(gòu)
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過程
Fortran Stop語(yǔ)句
Fortran基本輸入輸出
Fortran do循環(huán)結(jié)構(gòu)
Fortran查詢函數(shù)

Fortran if...then語(yǔ)句結(jié)構(gòu)

if ... then 語(yǔ)句由一個(gè)邏輯表達(dá)式后跟一個(gè)或多個(gè)語(yǔ)句和終止 end if 語(yǔ)句。

語(yǔ)法

if… then 語(yǔ)句的基本語(yǔ)法:

if (logical expression) then      
   statement  
end if

但是可以給一個(gè)名稱,if 塊,那么語(yǔ)法命名 if 語(yǔ)句如下:

[name:] if (logical expression) then      
   ! various statements           
   . . .  
end if [name]

如果邏輯表達(dá)式的計(jì)算結(jié)果為true,那么塊代碼內(nèi)的 if ... then 語(yǔ)句會(huì)被執(zhí)行。如果在結(jié)束后的邏輯表達(dá)式計(jì)算為false,那么第一個(gè)代碼塊之后的 if 語(yǔ)句會(huì)被執(zhí)行。

流程圖:

Flow Diagram

示例 1

program ifProg
implicit none
   ! local variable declaration
   integer :: a = 10
 
   ! 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"
   end if
       
   print*, "value of a is ", a
 end program ifProg

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

a is less than 20
value of a is 10

示例 2

這個(gè)例子說明了命名的 if 塊:

program markGradeA  
implicit none  
   real :: marks
   ! assign marks   
   marks = 90.4
   ! use an if statement to give grade
  
   gr: if (marks > 90.0) then  
   print *, " Grade A"
   end if gr
end program markGradeA   

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

Grade A

上一篇:Fortran選擇決策下一篇:Fortran教程