8. Á¶°Ç¹® (The if statements)

¾î¶°ÇÑ ÇÁ·Î±×·¥ ¾ð¾î¿¡¼­µçÁö Áß¿äÇÑ °ÍÀÌ Á¶°Ç¹®onditional statementsÀÌ´Ù. Fortran¿¡¼­ °¡Àå °øÅëÀÎ Á¶°Ç¹®Àº if¹®Àε¥ ½ÇÁ¦·Î´Â ¿©·¯ °¡Áö ÇüÅÂÀÌ´Ù. °¡Àå °£´ÜÇÑ ¹®ÀåÀº ³í¸® if¹®ÀÌ´Ù.
      if (logical expression) executable statement
ÀÌ´Â ÇÑ ÁÙ¿¡ Àû¾î¾ß ÇÑ´Ù. À§ ¿¹¿¡¼­´Â xÀÇ Àý´ë°ªÀ» ±¸ÇÏ´Â °ÍÀÌ´Ù.
      if (x .LT. 0) x = -x
if ³»ºÎ¿¡¼­ ÇÑ ¹®Àå ÀÌ»óÀ» ¼öÇàÇÏ¿©¾ß ÇÒ °æ¿ì¿¡´Â ´ÙÀ½ÀÇ syntax¸¦ »ç¿ëÇÑ´Ù.
      if (logical expression) then
         statements
      endif
°¡Àå ÀϹÝÀûÀÎ if¹®ÀÇ ÇüÅ´Â
      if (logical expression) then
         statements
      elseif (logical expression) then
         statements
       :
       :
      else
         statements
      endif
¼öÇà ¼ø¼­´Â À§¿¡¼­ ¾Æ·¡ ¹æÇâÀÌ´Ù. Á¶°Ç½ÄÀº Çϳª°¡ ÂüÀ϶§ ±îÁö ¼ø¼­´ë·Î ±× °ªÀ» ±¸ÇÑ´Ù. ±× ÈÄ ±×¿Í °ü·ÃµÈ code¸¦ ¼öÇàÇÏ°í endif ÀÌÈÄÀÇ ´ÙÀ½ ¹®ÀåÀ¸·Î ¶Ù¾î ³Ñ´Â´Ù.

°ãÃÄÁø if ¹® (Nested if statements)

if ¹®Àº ¿©·¯ ´Ü°è¿¡ °ÉÃÄ °ãÃÄ ¾µ ¼ö ÀÖ´Ù. Àбâ ÁÁ°Ô Çϱâ À§ÇÏ¿© ¾Ë¸Â°Ô µé¿©¾²±âÇϸé ÁÁ´Ù.
      if (x .GT. 0) then
         if (x .GE. y) then
            write(*,*) 'x is positive and x >= y'
         else
            write(*,*) 'x is positive but x < y'
         endif
      elseif (x .LT. 0) then
         write(*,*) 'x is negative'
      else
         write(*,*) 'x is zero'
      endif
³Ê¹« ¿©·¯ ´Ü°èÀÇ if ¹®À» °ãÃÄ ¾²¸é µû¶óÀâ±â Èûµé¹Ç·Î ÁÖÀÇÇÏ¿©¾ß ÇÑ´Ù.


Exercises

Exercise A
½Ç¼ö º¯¼ö t¿¡ ´ÙÀ½ °ªÀ» ÁöÁ¤ÇÏ´Â Fortran 77 code¸¦ ÀÛ¼ºÇ϶ó. (assume x¿Í y´Â ±× Àü¿¡ Á¤ÀÇÇÏ¿´´Ù°í °¡Á¤ÇÑ´Ù.)
     x+y       if x and y are both positive
     x-y       if x is positive and y negative
     y         if x is negative
     0         if x or y is zero


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