Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1798410
  • 博文数量: 438
  • 博客积分: 9799
  • 博客等级: 中将
  • 技术积分: 6092
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-25 17:25
文章分类

全部博文(438)

文章存档

2019年(1)

2013年(8)

2012年(429)

分类: C/C++

2012-03-26 13:09:24

if选择结构


  1.       read *, iValue
  2.       !单行if语名
  3.       if (iValue > 0) print *, iValue, " is greater than 0"
  4.       !if then else语句块
  5.       if (ivalue > 5) then
  6.             iValue = 5
  7.       else if (ivalue > 0) then
  8.             iValue = 0
  9.       else
  10.             iValue = -5
  11.       end if
  12.       print *, iValue


case选择结构
  1. !整型case,不支持实型case和复型case
  2.       read *, iValue
  3.       select case (iValue)
  4.             case (3) !当值为3时
  5.                   print *, "It's 3!"
  6.             case (6:) !当下界为6时(大于等于)
  7.                   print *, "above 6"
  8.             case (:0) !当上界为0时(小于等于)
  9.                   print *, "below 0"
  10.             case (4:5) !当在4到5之间时(小于等于下界大于等于上界)
  11.                   print *, "between 4 and 5"
  12.             case default !其它情况
  13.                   print *, "default"
  14.       end select
  15.       !字符型case
  16.       read *, cValue
  17.       select case (cValue)
  18.             case ("x")
  19.                   print *, "it's x!"
  20.             case ("a":"g")
  21.                   print *, "between a~g"
  22.             case default
  23.                   print *, "other"
  24.       end select
  25.       !逻辑型case
  26.       read *, iValue
  27.       select case (iValue > 0)
  28.             case (.true.)
  29.                   print *, "positive"
  30.             case (.false.)
  31.                   print *, "negtive or zero"
  32.       end select


循环结构
  1. !固定循环次数的do循环
  2.       !不带标号的do
  3.       do i=1, 20, 1 !类似于C里的for(i=1,i<=20,i+=1)
  4.             print *, i
  5.       end do
  6.       !省略最后的增量
  7.       do i=1, 20 !仍然每次递增1
  8.             print *, i
  9.       end do
  10.       !带标号的do
  11.       do 123, i=20, 0, -2 !do后是标号,即从123处开始循环
  12.       print *, i
  13. 123 continue !循环终端。该行有标号。可以用end do代替,比较过时
  14. !隐do循环
  15.       !在读写语句里可以控制读写的次数
  16.       print *, (i*5, i=1,20)
  17.       !可以嵌套循环
  18.       write (*,*) ((i,j,i=1,5),j=6,9) !i为内层循环,j为外层循环
  19. !无限循环
  20.       do
  21.             print *, "I'm here!"
  22.             read *, i
  23.             if (i > 5) cycle !cycle回到循环起点,类似于C的continue
  24.             print *, "not greater than 5!"
  25.             if (i < 0) exit !exit用于退出循环,类似于C的break
  26.       end do
  27.       !用标签来控制嵌套循环的流程
  28.       outer: do
  29.             print *, "I'm in outer!"
  30.             inner: do
  31.                   print *, "I'm in inner!"
  32.                   read *, i
  33.                   if (i > 0) exit inner !跳出内循环
  34.                   if (i < 0) exit outer !直接跳出外循环
  35.             end do inner !必须接标签名
  36.       end do outer
  37. !do while循环
  38.       i = 0
  39.       do while (i < 20)
  40.             print *, "in while", i
  41.             i = i+1
  42.       end do



终止程序
  1. !程序退出
  2.       stop 1 !类似于C的exit(1)
  3.       print *, "skipped" !这行不会被执行

阅读(1216) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~