Fortran 所提供的 迴圈結構

asdf

當 no 太大的時候, sum= 1 + 2 + ... + no
會出現 錯誤的答案,integer*2 2- bytes 的整數範圍是
-32768 ... +32767

4- bytes 的整數範圍是
-2147483648 ... +2147484647

以下的 示範程式,介紹三種回圈結構,如此就 夠用了


cpp 代码
  1. ! main()   
  2. implicit none   
  3. integer no, sum, i   
  4.   
  5. ! loop for sum= 1 + 2 + ... + no   
  6. no= 100000000   
  7.   
  8. sum= 0   
  9. do i=1, no, 1   
  10.     sum= sum + i   
  11. end do  
  12. print *, 'do loop, sum= ', sum   
  13. ! -----------------------------------------------   
  14.   
  15. sum= 0   
  16. i= 1   
  17. while (i <= no) do ...   
  18. do while (i <= no)   
  19.     sum= sum + i   
  20.     i= i + 1   
  21. end do  
  22. print *, 'do while(), sum= ', sum + 1   
  23. ! -----------------------------------------------   
  24.   
  25. ! repeat ... until (i > no)   
  26. sum= 0   
  27. i= 1   
  28. do  
  29.     sum= sum + i   
  30.     i= i + 1   
  31.   
  32.     ! if (i > no) exit   
  33.   
  34.     if (i .GT. no) then   
  35.        exit   
  36.     end if  
  37. end do  
  38. print *, 'repeat ... until(), sum= ', sum + 2   
  39.   
  40. end !of main()   
  41. ! -----------------------------------------------   

asdf

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值