Fortran - do while 迴圈結構



當滿足給定條件時,它將重複一個語句或一組語句。它在執行迴圈體之前測試條件。

語法

do while (logical expr) 
   statements
end do

流程圖

do while

示例

program factorial  
implicit none  

   ! define variables
   integer :: nfact = 1   
   integer :: n = 1 
   
   ! compute factorials   
   do while (n <= 10)       
      nfact = nfact * n 
      n = n + 1
      print*,  n, " ", nfact   
   end do 
end program factorial  

當編譯並執行上述程式碼時,它將產生以下結果 -

2             1
3             2
4             6
5            24
6           120
7           720
8          5040
9         40320
10        362880
11       3628800
fortran_loops.htm
廣告
© . All rights reserved.