Date: Wed, 11 Dec 1996 22:34:26 GMT Server: NCSA/1.5 Content-type: text/html Last-modified: Fri, 20 Sep 1996 20:37:26 GMT Content-length: 11312 CS 302 Section 70 Lecture Notes - Week 3,Part 2

Lecture Notes - Week 3,Part 2


Topic:
Repetition and loops. DO/END DO, DO WHILE/END DO and DO/CONTINUE statement.

Text:
Chp. 4.1 - 4.4, 4.6, 4.7, 4.9, 4.10

Notes:

Repetition and Loops

DO/CONTINUE Statement (FORTRAN-77)

How a DO Loops Work

Labels


Conditional Loops

DO WHILE Statement (FORTRAN-90)

FORTRAN-77 "DO WHILE" Loop

Nested Loops

  • Just as IF statements can be nested inside each other, so can DO/CONTINUE and DO WHILE loops. DO 20 X = 1, 10 FACT = 1 DO 10 COUNT = 1, X FACT = FACT * COUNT 10 CONTINUE PRINT *, X, '! =', FACT 20 CONTINUE
  • The outer loop is executed 10 times (X=1..10). Inner loop is executed X number of times, depending on the current value of X.
  • Nested loops must use different counter variables.
  • Inner loop's CONTINUE statement comes before the outer loop's CONTINUE statement.

Indenting

  • As with IF statement, indent all re-executed statements 3 additional spaces.

FORTRAN-90 Loops

  • Repitition and loops are vital for any programming languages to be useful.
  • Standard FORTRAN-77 only has the DO/CONTINUE loop.
  • FORTRAN-90 provides more powerful looping mechanisms (aside: the DO WHILE loop is also supported by many "non-standard" FORTRAN-77 compilers, such as Microsoft FORTRAN).

DO/END DO Loop (FORTRAN-90)

  • Same as the FORTRAN-77 DO/CONTINUE loop but without the CONTINUE statement and no label.
  • The end of the loop is indicated by the END DO statement. DO 10 ADD = 1, 10 READ *, NUM SUM = SUM + NUM 10 CONTINUE is the same as DO ADD = 1, 10 READ *, NUM SUM = SUM + NUM END DO
  • The DO/END DO can be nested like other loops.
  • The counter must be an INTEGER variable.
  • Important: Always use the DO/END DO loop instead of the DO/CONTINUE loop in this class. It is also supported by Microsoft FORTRAN.

EXIT and CYCLE Statement (FORTRAN-90)

  • The EXIT statement aborts the loop prematurely, jumping to the next statement after the end of the loop. DO ADD = 1, 10 READ *, NUM C Abort if a negative number is entered IF (NUM .LT. 0) EXIT SUM = SUM + NUM END DO PRINT *, SUM
  • The CYCLE statement skips the rest of the loop and resumes the loop at the next iteration. DO ADD = 1, 10 READ *, NUM C Ignore negative numbers IF (NUM .LT. 0) CYCLE SUM = SUM + NUM END DO PRINT *, SUM

DO/END DO Loop without Parameters (FORTRAN-90)

  • The counter, start, end and inc in the DO/END DO loop are actually all optional.
  • The loop is terminated instead by using the EXIT statement. DO READ *, NUM SUM = SUM + NUM C Stop when the user enters 0 IF (NUM .EQ. 0) EXIT END DO PRINT *, SUM
  • Warning: The EXIT statement must be executed eventually, otherwise you have an infinite loop.

Which Loop Do I Use?

  • Use the DO/END DO (FORTRAN-90) instead of the DO/CONTINUE (FORTRAN-77) if possible.
  • Use the DO WHILE/END DO (FORTRAN-90) instead of the IF/THEN/GOTO (FORTRAN-77) unless you have a strict FORTRAN-77 compiler.
  • Use the DO WHILE/END DO instead of the DO/EXIT/END DO whenever possible.
  • Don't use EXIT or CYCLE unless you have to. You do not not need to either for any assignment in this class.

Copyright © 1996 Modified fromGareth S. Bestor (bestor@cs.wisc.edu). Last modified September 19, 1996.