Fortran语言基础 - 图文

2026/1/24 10:05:17

山东建筑大学

write(*,*) \

read(*,*) height ! 读入身高 write(*,*) \

read(*,*) weight ! 读入体重 if ( weight > height-100 ) then

! 如果体重大于身高减去100, 会执行下面的程序 write(*,*) \ else

! 如果体重不大于身高减去100, 会执行下面的程序 write(*,*) \ end if stop end

3.1.2 逻辑运算

Fortran90的逻辑判断运算符号:(括号内为Fortran77的逻辑判断缩写符号)

== 相等 (.EQ.) /= 不相等 (.NE.) > 大于 (.GT.) >= 大于等于 (.GE.) < 小于 (.LT.) <= 小于等于 (.LE.)

逻辑关系运算浮号:

.AND. 逻辑与 .OR. 逻辑或 .NOT. 逻辑非

.EQV. 两边表达式运算结果相同时,结果为真 .NEQV. 两边表达式运算结果不同时,结果为真

注意:逻辑判断运算优先级高于逻辑关系运算。应此下面的两种逻辑关系式时等价的:

21

数值分析程序设计——Fortran基础

a>=80 .and. a<90 (a>=80) .and. (a<90)

不过为明显逻辑运算关系,建议使用后一种写法。

3.1.3 多重判断IF-ELSE IF

多重判断流程: if (条件1)then

程序代码 else if (条件2) then

程序代码 else if (条件3)then

程序代码 else if (条件4)then

程序代码 else

程序代码 end if

利用多重判断编写成绩等级查询程序:

program ex0505 implicit none integer score character grade write(*,*) \ read(*,*) score

if ( score>=90 .and. score<=100 ) then grade='A'

else if ( score>=80 .and. score<90 ) then grade='B'

else if ( score>=70 .and. score<80 ) then grade='C'

else if ( score>=60 .and. score<70 ) then

22

山东建筑大学

grade='D'

else if ( score>=0 .and. score<60 ) then grade='E' else

! score<0 或 score>100的不合理情形 grade='?' end if

write(*,\ grade stop end

3.1.4 嵌套IF语句

IF (……) THEN IF (……) THEN IF (……) THEN ELSE IF (……) THEN ELSE END IF END IF END IF

例 判断平面上一个点位于第几象限。

program ex0508 implicit none real x,y integer ans

write(*,*) \ read(*,*) x,y if ( x>0 ) then

if ( y>0 ) then ! x>0,y>0 ans=1

23

数值分析程序设计——Fortran基础

else if ( y<0 ) then ! x>0, y<0 ans=4 else ! x>0, y=0 ans=0 end if

else if ( x<0 ) then if ( y>0 ) then ! x<0, y>0 ans=2

else if ( y<0 ) then ! x<0, y<0 ans=3 else ! x<0, y=0 ans=0 end if

else ! x=0, y=任意数 ans=0 end if

if ( ans/=0 ) then ! ans不为0时, 代表有解 write(*,\第',I1,'象限')\ else

write(*,*) \落在轴上\ end if stop end

3.2 浮点数及字符的逻辑运算

3.2.1 浮点数的逻辑判断

使用浮点数作逻辑运算,要避免使用“等于”判断。因为使用浮点数作计算时,有效位数有限,难免出现计算上的误差,理想的等号不一定会成立。

program ex0509 implicit none

24


Fortran语言基础 - 图文.doc 将本文的Word文档下载到电脑
搜索更多关于: Fortran语言基础 - 图文 的文档
相关推荐
相关阅读
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 10

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219