asdf
當 no 太大的時候, sum= 1 + 2 + ... + no
會出現 錯誤的答案,integer*2 2- bytes 的整數範圍是
-32768 ... +32767
4- bytes 的整數範圍是
-2147483648 ... +2147484647
以下的 示範程式,介紹三種回圈結構,如此就 夠用了
asdf
當 no 太大的時候, sum= 1 + 2 + ... + no
會出現 錯誤的答案,integer*2 2- bytes 的整數範圍是
-32768 ... +32767
4- bytes 的整數範圍是
-2147483648 ... +2147484647
以下的 示範程式,介紹三種回圈結構,如此就 夠用了
cpp 代码
- ! main()
- implicit none
- integer no, sum, i
- ! loop for sum= 1 + 2 + ... + no
- no= 100000000
- sum= 0
- do i=1, no, 1
- sum= sum + i
- end do
- print *, 'do loop, sum= ', sum
- ! -----------------------------------------------
- sum= 0
- i= 1
- ! while (i <= no) do ...
- do while (i <= no)
- sum= sum + i
- i= i + 1
- end do
- print *, 'do while(), sum= ', sum + 1
- ! -----------------------------------------------
- ! repeat ... until (i > no)
- sum= 0
- i= 1
- do
- sum= sum + i
- i= i + 1
- ! if (i > no) exit
- if (i .GT. no) then
- exit
- end if
- end do
- print *, 'repeat ... until(), sum= ', sum + 2
- end !of main()
- ! -----------------------------------------------
asdf