More Ways to use Branching


Assignment :

Finish Homework 6 and start Homework 7, Read about Newton's method (an application using branching)

New Fortran:

SELECT CASE, CASE, CASE DEFAULT, END SELECT, DO WHILE, END DO , integer range notation, computed GO TO

SELECT CASE Statements

Although the IF structures, can cover any conditional programming that you want to do, you may realize that they can be difficult to use for some tasks. The construct SELECT CASE, introduced in Fortran 90, is useful when you want to do different things depending on the value of an integer, integer expression, character string, or logical expression. It can't be used if your decisions are based on values of real numbers. The structure begins with a statement:

      select case ( expression)
where "expression" can be an integer, character, or logical expression or variable. You follow it with a string of CASE statement blocks specifying what to do for various values or ranges of values of "expression". One of these may be the CASE DEFAULT block, including statements to execute if other CASEs are not met (basically equivalent to the simple ELSE). The SELECT structure ends with an "END SELECT" statement.

      select case (i)
         case default
            cond = 0
         case (1:5)
            cond=condUO2(temp)
         case (7)
            cond=condZr(temp)
      end select
In the above example if the variable "i" has a value in the range of 1 through 5 (this 1:5 type notation will reappear later with arrays), the function "condUO2" is used to determine the value of "cond" (conductivity). If "i" is seven, the function "condZr" is used. Otherwise the default is to set conductivity to zero. Notice that there is no overlap between cases. If I had included a "case (4)" overlapping the "case (1:5), there would have been a compiler error.

Fortran 90 lets you give names to your CASE structures to improve clarity (particularly nice if you nest CASE structures. If I named the above block "conduct", the resulting code would be:

      conduct: select case (i)
         case default conduct
            cond = 0
         case (1:5) conduct
            cond=condUO2(temp)
         case (7) conduct
            cond=condZr(temp)
      end select conduct
Use of these construct names, will seem silly in the small programs that you write now, but when programs become very long, they allow you to quickly trace what is happening using pattern searches in your editor.

Computed GO TO Statements

CASE was introduced to do more than simplify the use of IFs. It also acts as a replacement for the rather unpopular computed GO TO statement. The above example might have been implemented in a computed GO TO as:

      go to (200,200,200,200,200,100,300) i
  100    cond = 0
         go to 400
  200    cond=condUO2(temp)
         go to 400
  300    cond=condZr(temp)
  400    continue
The seven label numbers within the parentheses give a list of branches for the values of "i" from one to seven. If "i" is not in this range, the next instruction after the computed GO TO is executed. In this example the instruction following the GO TO has a label "100", but no label is needed if it is not used in one of the listed branches.

The above examples may suggest that CASE constructs have a very limited usefulness. However, when we start working with character variables, you will find that this a very powerful construct for responding to various prompted user inputs. For a preview look near the end of iftests.f

DO WHILE Statements

One other important Fortran 90 construct, for cutting use of IF statements is the DO WHILE statement. It is in a sense a combination of IF and older Fortran forms of the DO loop. For example to calculate 10 factorial I could use the following loop:

      i = 2
      ifact = 1
      do while (i.le.10)
         ifact=ifact*i
         i = i + 1
      end do
      print *, i-1,' factorial = ', ifact
The contents of the parentheses on the DO WHILE line must be a logical expression giving a value of true or false. The END DO forces an unconditional branch back to the associated DO WHILE statement. If the logical expression in the parentheses evaluates as false, the program branches to execute the next instruction after the associated END DO (here the PRINT). As with SELECT CASE, you can give a name to your DO WHILE block.

      i = 2
      ifact = 1
      factorial: do while (i.le.10)
         ifact=ifact*i
         i = i + 1
      end do factorial
      print *, i-1,' factorial = ', ifact
This naming will be very helpful when you get into the business of nested DO loops.

It is worth mentioning one other variation on the DO WHILE construct. For consistency with older DO loops, Fortran 90 permits you to end your loop at a labeled statement by specifying the label number of the last statement in the loop just after the "DO".

      i = 2
      ifact = 1
      do 100 while (i.le.10)
         ifact=ifact*i
 100     i = i + 1
      print *, i-1,' factorial = ', ifact
You need to know that, for this form, Fortran has some restrictions on statements used in the labeled last line of the loop. A computed GO TO is for example not permitted. Also note that in this example, you don't see and END DO statement. Here, an END DO would only be permitted if it were the line labeled 100.

      i = 2
      ifact = 1
      do 100 while (i.le.10)
         ifact=ifact*i
         i = i + 1
  100    end do
      print *, i-1,' factorial = ', ifact

Back to the Table of Contents / Home


Written and Maintained by John Mahaffy : jhm@psu.edu