9. ¹Ýº¹ (Loops)

ºñ½ÁÇÑ ÀÛ¾÷À» ¹Ýº¹ÇÏ¿©¾ß ÇÒ ¶§ loops ¸¦ »ç¿ëÇÑ´Ù. ´Ù¸¥ ÇÁ·Î±×·¥ ¾ð¾î¸¦ ¾Ë°í ÀÖ´Ù¸é for-loops, while-loops, ¶Ç´Â until-loops µî¿¡ °üÇÏ¿© ¾Ë°í ÀÖÀ» °ÍÀÌ´Ù. Fortran 77¿¡´Â ÇÑ °¡Áö loop Áï do-loop »ÓÀÌ´Ù. do-loop´Â ´Ù¸¥ ¾ð¾î¿¡¼­ for-loop¶ó°í ÇÏ´Â °Í°ú °°´Ù. if ¿Í goto ¹®À» ÀÌ¿ëÇÏ¸é ´Ù¸¥ loop¸¦ Èä³»³¾ ¼ö ÀÖ´Ù.

do-loops

do-loop ´Â °£´ÜÈ÷ ¼¼°íÀÚ ÇÒ °æ¿ì »ç¿ëÇÑ´Ù. ´ÙÀ½Àº Á¤¼ö 1ºÎÅÍ n (nÀÇ °ªÀº ´Ù¸¥ °÷¿¡¼­ ÁöÁ¤µÇ¾ú´Ù°í °¡Á¤ÇÑ´Ù.)±îÁöÀÇ ÇÕÀ» ¸Å¹ø ±¸ÇÏ´Â ¿¹ÀÌ´Ù.
      integer i, n, sum
 
      sum = 0
      do 10 i = 1, n
         sum = sum + i
         write(*,*) 'i =', i
         write(*,*) 'sum =', sum
  10  continue
¼ýÀÚ 10 Àº ¹®Àå ¹øÈ£ labelÀÌ´Ù. º¸Åë ÇÑ ÇÁ·Î±×·¥ ¾È¿¡ ¹®Àå ¹øÈ£°¡ ÇÊ¿äÇÑ loop¿Í ´Ù¸¥ ¹®ÀåÀÌ ¸¹ÀÌ ÀÖ´Ù. °¢°¢ÀÇ ÇÁ·Î±×·¥ (¶Ç´Â ºÎÇÁ·Î±×·¥) ¾È¿¡¼­ °íÀ¯ÀÇ ¹®Àå ¹øÈ£¸¦ ºÎ¿©Çϵµ·Ï ÁÖÀÇÇÏ¿©¾ß ÇÑ´Ù. Ä­ (column) 2-5´Â ÀÌ ¹®Àå ¹øÈ£¸¦ Àû´Â °÷ÀÌ´Ù. ¹®Àå ¹øÈ£·Ñ »ç¿ëÇÏ´Â ¼ýÀÚ´Â Àǹ̰¡ ¾øÀ¸¹Ç·Î ¾î¶²ÇÑ Á¤¼ö¸¦ »ç¿ëÇÏ¿©µµ °ü°è¾ø´Ù. º¸Åë ÇÑ ¹ø¿¡ 10¾¿ Áõ°¡Çϵµ·Ï ÇÑ´Ù.

do-¹®Àå¿¡¼­ Á¤ÀÇÇÑ º¯¼ö´Â Ưº°ÇÑ ¸í·ÁÀÌ ¾ø´Â ÇÑ 1¾¿ Áõ°¡ÇÑ´Ù. ±×·¯³ª step À» »ç¿ëÇÏ¿© ±× Áõ°¡·®À» ´Ù¸¥ Á¤¼ö·Î ¹Ù²Ü ¼ö ÀÖ´Ù. ´ÙÀ½Àº 1ºÎÅÍ 10»çÀÌÀÇ Â¦¼ö¸¦ Å« ¼öºÎÅÍ ÀμâÇÏ´Â ÇÁ·Î±×·¥ÀÌ´Ù.

      integer i
 
      do 20 i = 10, 1, -2
         write(*,*) 'i =', i
  20  continue

do loopÀÇ ÀϹÝÀûÀÎ ÇüÅ´ ´ÙÀ½°ú °°´Ù.

      do label  var =  expr1, expr2, expr3
         statements
label continue
var ´Â loop º¯¼ö (¶Ç´Â loop index¶ó°íµµ ÇÑ´Ù)´Â Á¤¼öÇüÅÂÀ̾î¾ß ÇÑ´Ù. expr1 ´Â varÀÇ ÃʱⰪ, expr2 ´Â Á¾·á ÇÑ°èÀÌ°í expr3 ´Â Áõ°¡(step)ÀÌ´Ù.

ÁÖÀÇ : do-loop º¯¼ö¸¦ loop ¾È¿¡¼­ ´Ù¸¥ ¹æ¹ýÀ¸·Î ¹Ù²Ù¾î¼­´Â ¾ÈµÈ´Ù. ÀÌ °æ¿ì ½É°¢ÇÑ È¥¶õÀ» ÃÊ·¡ÇÑ´Ù.

¸¹Àº Fortran 77 compiler¿¡¼­ do-loop¸¦ enddo ¹®ÀåÀ¸·Î ³¡³¾ ¼ö ÀÖµµ·Ï ÇÑ´Ù. ÀÌ °æ¿ìÀÇ ÀåÁ¡Àº enddo°¡ ¾Õ¿¡¼­ »ç¿ëÇÑ °¡Àå ÃÖ±ÙÀÇ do ¹®ÀåÀÇ ³¡À» ÀǹÌÇϹǷΠ¹®Àå ¹øÈ£¸¦ »ç¿ëÇÏÁö ¾Ê¾Æµµ µÈ´Ù´Â Á¡ÀÌ´Ù. enddo ±¸Á¶´Â »ó´çÈ÷ ¸¹ÀÌ »ç¿ëµÇÁö¸¸ Ç¥ÁØ ANSI Fortran 77Àº ¾Æ´Ï´Ù.

while-loops

while-loop¸¦ »ç¿ëÇÏ´Â °¡Àå Á÷°üÀûÀÎ ¹æ¹ýÀº
      while (logical expr) do
         statements
      enddo
À̰ųª
      do while (logical expr) 
         statements
      enddo
ÀÌ´Ù. while ¹®ÀÌ Âü(true)ÀÎ ÇÑ ³»ºÎÀÇ ¹®ÀåµéÀ» ¹Ýº¹ÇÑ´Ù. ÀÌ syntax¸¦ ¿©·¯ ÄÄÇ»ÅÍ¿¡¼­ »ç¿ëÇÒ ¼ö ÀÖ±â´Â Çϳª ANSI Fortran 77Àº ¾Æ´Ï´Ù. ¿Ã¹Ù¸¥ ¹æ¹ýÀº if¿Í goto¸¦ »ç¿ëÇÏ´Â °ÍÀÌ´Ù.
label if (logical expr) then
         statements
         goto label
      endif 

´ÙÀ½Àº 100º¸´Ù À۰ųª °°Àº ¼ö¸¦ 2ÀÇ Áö¼ö·Î ÇÏ¿© ±× °ªÀ» °è»êÇÏ°í ÀμâÇÏ´Â ÇÁ·Î±×·¥ÀÌ´Ù.

     integer n

     n = 1
  10 if (n .le. 100) then
        n = 2*n
        write (*,*) n
        goto 10
     endif

until-loops

Á¾·á ±âÁØÀÌ ¹®ÀåÀÇ ½ÃÀÛÀÌ ¾Æ´Ï¶ó ¸Ç ³¡¿¡ ÀÖ´Â °æ¿ì À̸¦ º¸Åë until-loop¶ó°í ÇÑ´Ù. À¯»ç ÄÚµå´Â ´ÙÀ½°ú °°´Ù.
      do
         statements
      until (logical expr)
Fortran 77¿¡¼­´Â ÀÌ ¿ª½Ã if¿Í goto¸¦ »ç¿ëÇÏ¿© ±¸¼ºÇÏ¿©¾ß ÇÑ´Ù.
label continue
         statements
      if (logical expr) goto label
¸¶Áö¸·ÀÇ ³í¸®½ÄÀÌ À¯»ç ÄÚµåÀÇ ³í¸®½ÄÀÇ ºÎÁ¤À̾î¾ß ÇÑ´Ù´Â Á¡À» ÁÖÀÇÇ϶ó.

Loops in Fortran 90

Fortran 90¿¡¼­´Â do-enddo ±¸Á¶¸¦ loop ±¸Á¶ÀÇ ±âº»À¸·Î ÇÑ´Ù. µû¶ó¼­ µÑ ¾¿ ¼¼¾î ³»·Á°¥ °æ¿ì ´ÙÀ½°ú °°ÀÌ ÇÑ´Ù.
      do i = 10, 1, -2
         write(*,*) 'i =', i
      end do
while °ú until loopµµ do-enddo ±¸Á¶·Î ±¸¼ºÇÒ ¼ö ÀÖÀ¸³ª conditional exit ¹®À» ´õÇÏ¿©¾ß ÇÑ´Ù. ÀϹÝÀûÀÎ °æ¿ì¿¡´Â
      do
         statements
         if (logical expr) exit
         statements
      end do
exit Á¶°ÇÀÌ ¾Õ¿¡ ÀÖÀ¸¸é while loopÀÌ°í ¸Ç µÚ¿¡ ÀÖÀ¸¸é until loopÀÎ ¼ÀÀÌ´Ù.


Exercises

Exercise A
´ÙÀ½ À¯»ç Äڵ带 Ç¥ÁØ Fortran 77¿¡ ¸ÂÃß¾î ´Ù½Ã ½á¶ó. °¡´ÉÇϸé goto¸¦ »ç¿ëÇÏÁö ¸»¶ó.
      i = 1
      while (i<100) do
         sum = sum + i
         i = i+2
      enddo


      i = 0
      x = 1.0
      repeat
         x = f(x)
         i = i+1
      until (x<0)
      write(*,*) i, x

Exercise B
´ÙÀ½ code´Â ¿Ã¹Ù¸£Áö ¸øÇÏ´Ù. F77¿¡ ¸ÂÃß¾î ´Ù½Ã ½á¶ó. (Tip: CompileÇÏ°í ¼öÇà½ÃÄÑ µÎ ÇÁ·Î±×·¥ÀÌ °°Àº °á°ú¸¦ ÁÖ´ÂÁö »ìÆì º¸¶ó.)
      i = 1
      sum = 0
   10 do 20 i = 1, 50
         if (i .gt. 10) goto 30
         sum = sum + i
   20 continue
   30 if (i. le. 20) then
         sum = sum - 1
         goto 20
      else
         sum = 2*sum
      endif
      write(*,*) 'Sum =', sum

[Fortran Tutorial]
Ãæ³²´ëÇб³ ¹°¸®Çаú ¿À º´¼º