!关系运算符和关系表达式
!关系运算符:.GT.(大于);.GE.(大于等于);.LT.(小于);.LE.(小于等于);.EQ.(等于);.NE.(不等于)
!算术运算符优先级别高于关系运算符,各关系运算符优先级别相同
!逻辑运算符和逻辑表达式
!优先级: .not.(非);.and.(与);.or.(或);.eqv.(等),.neqv.(不等),.xor.(异或)
!逻辑运算符的优先级低于关系运算符和算术运算符
!运算顺序:先括号,后算术,再关系,最后逻辑
!逻辑if语句
if(表达式e)可执行语句s
!块if结构
if(表达式e)then
<then语句体>
endif
!双分支选择块if结构
if(表达式e)then
<then语句体>
else
<else语句体>
endif
!多分支选择块if结构
if(表达式e1)then
<then语句体>
else if(表达式e2)then
<then语句体>
...
else
<else语句体>
endif
!举例:
!1.电视台节目安排
integer week
print*,"请输入查询数字1-7,对应查询每日节目:"
read*,week
if(week==1.or.week==4)then
print*,"今日节目:卡通车。"
else if(week==2.or.week==5)then
print*,"今日节目:电视剧。"
else if(week==3.or.week==6)then
print*,"今日节目:文艺综艺节目。"
else
print*,"数字输入有误!"
endif
end
!块if结构的嵌套
if()then
if()then
if()then
esle if()then
else
endif
endif
else
if()then
else
endif
endif
!举例:
!1.输入学生成绩,按条件判断成绩等级
read*,score
if(score>=60)then
if(score>=70)then
if(score>=80)then
print*,"A"
else
print*,"B"
endif
else
print*,"C"
endif
else
print*,"D"
endif
end
!块case结构
select case(表达式e)
case(数值1)
语句体1
case(数值2)
语句体2
...
case(数值n)
语句体n
case default
语句体n+1
end select
!举例:
!1.输入两个算术量和算术运算符,输出运算结果
integer a,b,c
character*2 oper
print*,"请输入两个非零整数和一个算术运算符:"
read*,a,b,oper
select case(oper)
case('+')
c=a+b
case('-')
c=a-b
case('*')
c=a*b
case('/')
c=a/b
case('**')
c=a**b
case default
write(*,'("输入运算符不正确")')
end select
print*,a,oper,b,'=',c
end
!2.给定学生成绩,评判学生等级
integer s
print*,"输入学生成绩:"
read*,s
select case(s)
case(0:59)
print*,"该学生成绩为:不及格"
case(60:69)
print*,"该学生成绩:及格"
case(70:79)
print*,"该学生成绩:中"
case(80:94)
print*,"该学生成绩:良"
case(95:100)
print*,"该学生成绩:优"
end select
end
!关系运算符:.GT.(大于);.GE.(大于等于);.LT.(小于);.LE.(小于等于);.EQ.(等于);.NE.(不等于)
!算术运算符优先级别高于关系运算符,各关系运算符优先级别相同
!逻辑运算符和逻辑表达式
!优先级: .not.(非);.and.(与);.or.(或);.eqv.(等),.neqv.(不等),.xor.(异或)
!逻辑运算符的优先级低于关系运算符和算术运算符
!运算顺序:先括号,后算术,再关系,最后逻辑
!逻辑if语句
if(表达式e)可执行语句s
!块if结构
if(表达式e)then
<then语句体>
endif
!双分支选择块if结构
if(表达式e)then
<then语句体>
else
<else语句体>
endif
!多分支选择块if结构
if(表达式e1)then
<then语句体>
else if(表达式e2)then
<then语句体>
...
else
<else语句体>
endif
!举例:
!1.电视台节目安排
integer week
print*,"请输入查询数字1-7,对应查询每日节目:"
read*,week
if(week==1.or.week==4)then
print*,"今日节目:卡通车。"
else if(week==2.or.week==5)then
print*,"今日节目:电视剧。"
else if(week==3.or.week==6)then
print*,"今日节目:文艺综艺节目。"
else
print*,"数字输入有误!"
endif
end
!块if结构的嵌套
if()then
if()then
if()then
esle if()then
else
endif
endif
else
if()then
else
endif
endif
!举例:
!1.输入学生成绩,按条件判断成绩等级
read*,score
if(score>=60)then
if(score>=70)then
if(score>=80)then
print*,"A"
else
print*,"B"
endif
else
print*,"C"
endif
else
print*,"D"
endif
end
!块case结构
select case(表达式e)
case(数值1)
语句体1
case(数值2)
语句体2
...
case(数值n)
语句体n
case default
语句体n+1
end select
!举例:
!1.输入两个算术量和算术运算符,输出运算结果
integer a,b,c
character*2 oper
print*,"请输入两个非零整数和一个算术运算符:"
read*,a,b,oper
select case(oper)
case('+')
c=a+b
case('-')
c=a-b
case('*')
c=a*b
case('/')
c=a/b
case('**')
c=a**b
case default
write(*,'("输入运算符不正确")')
end select
print*,a,oper,b,'=',c
end
!2.给定学生成绩,评判学生等级
integer s
print*,"输入学生成绩:"
read*,s
select case(s)
case(0:59)
print*,"该学生成绩为:不及格"
case(60:69)
print*,"该学生成绩:及格"
case(70:79)
print*,"该学生成绩:中"
case(80:94)
print*,"该学生成绩:良"
case(95:100)
print*,"该学生成绩:优"
end select
end