Professor Diomar Cesar Lobao

Universidade Federal Fluminense-Volta Redonda, RJ, Brasil

Diomar Cesar


Dept. Ciências Exatas - Exact Science Dept.

Search

iftests.f

c <html>
c <head><title></title></head>
c <body>
c <pre>
c
c
c       John Mahaffy,  Penn State University, CmpSc 201 Example
c       1/26/96
c
      program iftests
      implicit none
c<a name="logical"><font color="FF0000">
      logical ltest
c</font></a>
      integer i,j
      character*1 char
c
c    This program gives examples of various uses of the if statement
c
c    variables:
c      i -  an input integer
c      j -  an integer used in some tests
c      char -  an input single character
c      ltest - logical variable holding the results of a comparison
c
c    Begin by reading in a integer
c
  10  print *, 'Type in an integer value:'
c
      read *, i
c
c     The arithmetic IF statement is archaic.  You shouldn't use it
c     but need to know that it exists.
c
c<a name="a1"><font color="FF0000">
      if(i-100) 20,30,40
   20 print *, 'input is less than 100'
      go to 50
   30 print *, 'input is equal to 100'
      go to 50
   40 print *, 'input is greater than 100'
c</a></font>
c
c     The simplest logical IF just does something immediately
c
 50   if(i.lt.0) i=-i
      if(i.gt.50.and.i.lt.100) print *, ' 50 < input < 100 '
c
c    A more powerful logical IF conditionally executes a block of code
c    the word 'then' always appears at the end of the IF line and the
c    block of code is ended with a line saying 'endif' or 'end if'
c    Note the use of indentation to highlight the structure.
c
      if(i.ge.100.and.i.le.200) then
         i=i+150
         print *, 'i incremented by 150'
c<a name="14"><font color="00FF00">
      endif
c</font></a>
c
c    The contents of the parentheses can always be replaced by a logical
c    variable
c
      ltest= i.ge.-200.and.i.le.-100
      if (ltest) then
         i=i+200
         print *, 'i incremented by 200'
      endif
c
c    Block logical IF's may contain other tests and a final option
c    (ELSE) to be executed if one of the others is not done.  Only one
c    of the possible actions will be taken.  When it is the others are
c    bypassed
c<a name="10"><font color="FF0000">
      if((i.gt.200.and.i.le.300).or.(i.gt.1200)) then
         i=i+200
         print *, 'input incremented by 200'
      else if (i.eq.400) then
         print *, 'input = 400'
      else
         print *, 'Nothing special about input.'
      endif
c</a></font>
c
c    Sometimes it makes sense to use the Fortran 90 Case structure instead
c    of IF, ELSE IF structures.  Only one case will be done
c
c<a name="case"><font color="FF0000"> 
      select case (i)
c</a></font>
         case default
            print *, 'Nothing special about input.'
         case (201:300)
            i=i+200
            print *, 'input incremented by 100'
         case (400)
            print *, 'input = 400'
      end select
c
c    Of the above examples only SELECT CASE required use of an integer
c    rather than a real number. However SELECT CASE also functions with
c    Character strings.
c
      print *, 'Do you want to continue? (y or n)'
      read 1000, char
1000  format(a1)
      select case (char)
         case ('y')
            ltest=.true.
            print *, 'Continuing'
         case ('n')
            ltest=.false.
            print *, 'Stopping'
         case default
            print *, 'Incorrect Response'
            stop
c<a name="es"><font color="FF0000">
      end select
c</a></font>
c    The most basic if test (what the machine does) is just a branch
c    These things make the flow of the program difficult to follow and
c    should be used as little as possible
c
      if(ltest) go to 10
c
c    An if test that forces a loop through coding can be implented in
c    Fortran 90 with a DO WHILE structure.  Notice values of i and j
c    when the loop is done.  The test always done at the beginning of
c    the loop.
c
      i=0
      j=2
c<a name="dow"><font color="FF0000">
      do while (i.lt.10.and.j.lt.10000)
c</font></a>
         i=i+1
         j=j**2
      end do
      print *, 'i = ',i,' , j = ',j
c
      stop
      end
c
c </pre>
c </body>
c </html>
c
Skip to content