流程控制語句
if的基本用法
if(邏輯表達(dá)式)then
...
... ! 當(dāng)邏輯表達(dá)式為真時(shí)執(zhí)行then代碼塊,否則執(zhí)行end if之后的內(nèi)容
...
end if
以上then代碼塊中如果只有一行程序代碼,可以改寫成下面的形式:
if(邏輯表達(dá)式) 一行表達(dá)式---------如:
if(a>3) a=a+1
if-else語句塊
if(邏輯表達(dá)式)then
...
else
...
end if
下面利用該語句塊寫一個(gè)分段函數(shù):
program main C implicit real*8(a-z) real*8::x,f C if(x<-1)then f=x**2+sin(x) else f=x**3-cos(x) end if C print*,"please input a real number" read(*,*)x write(*,*)"the value of function is: ",f end program main
多重判斷if-elseif語句
if可以配合else-if來做多重判斷,多重判斷可以一次列出多個(gè)條件以及多個(gè)程序模塊,但其中最多只有一個(gè)成立校翔。也就是說每次最多只有一個(gè)程序塊被執(zhí)行。
if(條件1)then
...
elseif(條件2)then
...
elseif(條件3)then
...
...
else ! else這個(gè)條件模塊可以省略,當(dāng)前面都不成立時(shí)吨拍,才執(zhí)行這個(gè)。
...
...
end if
下面寫一個(gè)簡(jiǎn)單的程序网杆,要求給出一個(gè)人的工資羹饰,計(jì)算他應(yīng)該納多少稅。
program main
!------------------------------program comment
! Description :這個(gè)小程序計(jì)算個(gè)人收入應(yīng)上交稅金
!---------------------------------------
! Version :V1.0
! Date :20160731
! Coding by :xiaodai
!-----------------------------------------
! Input parameter :wage,thr_ensure,house_pay,s_num
! Output parameter :tax,aftax
!-----------------------------------------
!
implicit real*8(a-z)
real*8::wage,temp,tax,aftax
! wage月工資碳却,tax應(yīng)納稅金队秩,aftax稅后工資
!假設(shè)這個(gè)城市的三險(xiǎn)一金標(biāo)準(zhǔn)是養(yǎng)老保險(xiǎn)8%、醫(yī)療保險(xiǎn)2%昼浦、失業(yè)保險(xiǎn)1%馍资、住房公積金8%
!
print*,"請(qǐng)輸入您的月工資"
read(*,*)wage
temp=wage*(1-0.08-0.02-0.01-0.08)-3500
!
if(temp<=0)then
tax=0
elseif(temp<=1500)then
tax=temp*0.03
elseif(temp<=4500)then
tax=1500*0.03+(temp-1500)*0.1+105
elseif(temp<=9000)then
tax=1500*0.03+(4500-1500)*0.1+(temp-4500)*0.2+555
elseif(temp<=35000)then
tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(temp-9000)*0.25+1005
elseif(temp<=55000)then
tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(35000-9000)*0.25+(temp-35000)*0.3+2755
elseif(temp<=80000)then
tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(35000-9000)*0.25+(55000-35000)*0.3+(temp-55000)*0.35+5505
else
tax=1500*0.03+(4500-1500)*0.1+(9000-4500)*0.2+(35000-9000)*0.25+(55000-35000)*0.3+(80000-55000)*0.35+(temp-80000)*0.45+13505
end if
!
aftax=wage-tax
write(*,*)"您需要上交的稅金是:",tax
write(*,*)"您的稅后工資是:",aftax
write(*,*)"您上交的稅金占您的工資",tax/wage*100,"%"
end program main
if語句嵌套
if(...)then
if(...)then
if(...)then
else if(...)then
else
...
end if
end if
end if
Select case語句
select case 是進(jìn)行“多重判斷”的有效而簡(jiǎn)明的語句,語法如下:
select case(變量)
case(數(shù)值1)
...
case(數(shù)值2)
...
case(數(shù)值3)
...
case(數(shù)值4)
...
...
case(數(shù)值n)
...
case deflaut
...
end select
下面給出一個(gè)有趣的例子:
program main
! --------------------------------------------------------program comment
! 描述:本程序要求用戶給出任意一個(gè)成績(jī)关噪,返回給用戶該成績(jī)所對(duì)應(yīng)的等級(jí)
! --------------------------------------------------------
implicit none
integer::score
print*,"please input your score"
read(*,*)score
select case(score)
case(90:100)
write(*,*),"your grade is A !"
case(80:89)
write(*,*),"your grade is B !"
case(60:79)
write(*,*),"your grade is C !"
case(0:59)
write(*,*),"your grade is D !"
case default
write(*,*),"There are something error !"
end select
end program
使用select case要注意的地方有:
select case后面的參數(shù)只能使用整數(shù)(integer)鸟蟹、字符(character)、以及邏輯變量(logical)使兔,不能使用浮點(diǎn)數(shù)和復(fù)數(shù)建钥。如果要使用浮點(diǎn)數(shù)來判斷,則只能使用if-else-if語句虐沥。
每個(gè)case所使用的數(shù)值必須是常量熊经,不能是變量泽艘。
-
case后面可以使具體的數(shù)值,也可以是一個(gè)變化范圍镐依,也可以是幾個(gè)獨(dú)立的數(shù)字等匹涮,如下:
case(1) ! 變量等于1時(shí),執(zhí)行這個(gè)case case(1:) ! 變量大于1時(shí)槐壳,執(zhí)行這個(gè)case case(1:5) ! 變量在1-5之間時(shí)然低,執(zhí)行這個(gè)case case(1,3,5) ! 變量等于1,3,5其中任何一個(gè)時(shí),執(zhí)行這個(gè)case case(:5) ! 變量小于5時(shí)宏粤,執(zhí)行這個(gè)case
下面是一個(gè)頗為有趣的小程序脚翘,摘自彭國(guó)倫的《Fortran95程序設(shè)計(jì)》一書的例子。
program main
implicit none
real*8:: a,b,ans
character c
write(*,*),"請(qǐng)按如下格式輸入:數(shù)字绍哎,運(yùn)算符来农,數(shù)字:"
read(*,*),a
read(*,"(a1)")c
read(*,*)b
select case (c)
case('+')
ans=a+b
case('-')
ans=a-b
case('/')
ans=a/b
case('*')
ans=a*b
case default
write(*,"('Unknown operator',a1)") c
stop
end select
write(*,"(F16.2,a1,F16.2,'=',F16.2)")a,c,b,ans
stop
end program
Goto語句
顧名思義,goto語句就是將程序跳轉(zhuǎn)到要去的地方崇堰,這個(gè)語句的優(yōu)點(diǎn)是可以讓程序控制變得更靈活沃于,但是缺點(diǎn)也很明顯,就是它是的程序支離破碎海诲,沒有一定功力的人很難得駕馭繁莹。建議初學(xué)者盡量不要使用。
Fortran中特幔,任何一行程序代碼都可以有自己的標(biāo)簽(行標(biāo)號(hào))咨演,goto可以控制程序在不同行之間跳躍。
下面的程序用來計(jì)算個(gè)人的BMI指標(biāo)蚯斯。
program main
! ------------------------------------------------------program comment
! 改程序用來檢測(cè)一個(gè)人的體重是否超標(biāo)
! 中華人名共和國(guó)衛(wèi)生行業(yè)標(biāo)準(zhǔn)成人體重判定
! ------------------------------------------------------
implicit none
real::height,weight,BMI
write(*,*),"Please input your height(m) and weight (kg) : "
read(*,*)height,weight
BMI=weight/height**2
if(BMI>=28) goto 100
if(BMI>=24.AND.BMI<28) goto 101
if(BMI>=18.5.AND.BMI<24)goto 102
if(BMI<18.5)goto 103
100 write(*,*),"You are too fat,guys . Please have a hleathy diet."
stop
101 write(*,*),"You are too weight,guys . "
stop
102 write(*,*),"You have a hleathy body."
stop
103 write(*,*),"You should take care of your weight."
end program
趕緊來試試你的健康狀況吧薄风。(上面的程序把stop去掉會(huì)是什么情況呢?請(qǐng)?jiān)囋嚢膳那丁#?/p>
goto還可以用來寫循環(huán)遭赂。如下的程序計(jì)算一個(gè)數(shù)的階乘。
program main
! -----------------------------------program comment
! 改程序用來計(jì)算一個(gè)數(shù)的階乘(狹義階乘)
! --------------------------------------
real*8::num=1
integer*4::a,i
print*,"Please input a number: "
read(*,*)i
a=i
100 num=num*a
a=a-1
if(a>1) goto 100
write(*,*) i,"'s factorial is ",num
end program
上面的小程序只能計(jì)算狹義的階乘横辆,不能計(jì)算歐拉第一積分
下面的程序中撇他,goto可以提供多個(gè)跳轉(zhuǎn)點(diǎn)共選擇,更具有靈活性狈蚤。該程序改編自彭國(guó)倫《Fortran95程序設(shè)計(jì)》一書困肩,原書程序表示沒有看懂,歡迎大家討論脆侮、指導(dǎo)僻弹。
program main
implicit none
integer i,n
print*,"請(qǐng)輸入兩個(gè)整數(shù)"
read(*,*)i,n
! 當(dāng)I/N=1時(shí),goto 10他嚷,當(dāng)I/N=2時(shí),goto 20,當(dāng)I/N=3時(shí)筋蓖,goto 30卸耘,當(dāng)I/N<1或I/N>3時(shí),不跳轉(zhuǎn)粘咖,執(zhí)行下一行
goto(10,20,30) i/n
10 write(*,*) 'I/N=1'
goto 100
20 write(*,*) 'I/N=2'
goto 100
30 write(*,*) 'I/N=3'
100 stop
end program
同樣的程序蚣抗,改編自彭國(guó)倫《Fortran95程序設(shè)計(jì)》一書
program main
implicit none
real::a,b,c
write(*,*),"Please input two number"
read(*,*)a,b
c=a-b
! c<0就goto 10,c=0就goto 20,c>0就goto 30
if(c) 10,20,30
10 write(*,*)a,'<',b
goto 40
20 write(*,*)a,'=',b
goto 40
30 write(*,*)a,'>',b
goto 40
40 stop
end program
PAUSE CONTINUE STOP
pause 程序執(zhí)行到pause時(shí)會(huì)停止執(zhí)行,直到用戶按下"Enter" 鍵才會(huì)繼續(xù)瓮下。
continue 就是繼續(xù)往下執(zhí)行代碼的命令翰铡。
stop 用來結(jié)束程序