| Refresh | Home EGTry.com

if statement



one line if statement

      program ifmain
      real x
    
      x=3 
      if (x.lt.10) write(*,*) 'x is less than 10'
      stop
      end



if true, then a block of code

      program ifmain
      real sum
    
      sum=0
      x=9 
      if (x.lt.10) then
       sum=sum+x
       x=x-1 
      endif 
      stop
      end



if block1, else block 2

      program ifmain
    
      x=9 
      if (x.lt.10) then
       x=x+1 
      else 
       x=x-1
      endif 

      stop
      end



single selection from multiple choices, simulate switch statement

      program ifmain
      integer choice


      choice=3
      if (choice.eq.1) then
       write(*,*) 'choice 1'
      elseif (choice.eq.2) then
       write(*,*) 'choice 2'
      elseif (choice.eq.3) then
       write(*,*) 'choice 3'
      else 
       write(*,*) 'choice beyond 1 to 4'
      endif 

      stop
      end