if选择结构:
- read *, iValue
- !单行if语名
- if (iValue > 0) print *, iValue, " is greater than 0"
- !if then else语句块
- if (ivalue > 5) then
- iValue = 5
- else if (ivalue > 0) then
- iValue = 0
- else
- iValue = -5
- end if
- print *, iValue
case选择结构:
- !整型case,不支持实型case和复型case
- read *, iValue
- select case (iValue)
- case (3) !当值为3时
- print *, "It's 3!"
- case (6:) !当下界为6时(大于等于)
- print *, "above 6"
- case (:0) !当上界为0时(小于等于)
- print *, "below 0"
- case (4:5) !当在4到5之间时(小于等于下界大于等于上界)
- print *, "between 4 and 5"
- case default !其它情况
- print *, "default"
- end select
- !字符型case
- read *, cValue
- select case (cValue)
- case ("x")
- print *, "it's x!"
- case ("a":"g")
- print *, "between a~g"
- case default
- print *, "other"
- end select
- !逻辑型case
- read *, iValue
- select case (iValue > 0)
- case (.true.)
- print *, "positive"
- case (.false.)
- print *, "negtive or zero"
- end select
循环结构:
- !固定循环次数的do循环
- !不带标号的do
- do i=1, 20, 1 !类似于C里的for(i=1,i<=20,i+=1)
- print *, i
- end do
- !省略最后的增量
- do i=1, 20 !仍然每次递增1
- print *, i
- end do
- !带标号的do
- do 123, i=20, 0, -2 !do后是标号,即从123处开始循环
- print *, i
- 123 continue !循环终端。该行有标号。可以用end do代替,比较过时
- !隐do循环
- !在读写语句里可以控制读写的次数
- print *, (i*5, i=1,20)
- !可以嵌套循环
- write (*,*) ((i,j,i=1,5),j=6,9) !i为内层循环,j为外层循环
- !无限循环
- do
- print *, "I'm here!"
- read *, i
- if (i > 5) cycle !cycle回到循环起点,类似于C的continue
- print *, "not greater than 5!"
- if (i < 0) exit !exit用于退出循环,类似于C的break
- end do
- !用标签来控制嵌套循环的流程
- outer: do
- print *, "I'm in outer!"
- inner: do
- print *, "I'm in inner!"
- read *, i
- if (i > 0) exit inner !跳出内循环
- if (i < 0) exit outer !直接跳出外循环
- end do inner !必须接标签名
- end do outer
- !do while循环
- i = 0
- do while (i < 20)
- print *, "in while", i
- i = i+1
- end do
终止程序:
- !程序退出
- stop 1 !类似于C的exit(1)
- print *, "skipped" !这行不会被执行
阅读(1216) | 评论(0) | 转发(0) |