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
- Conditional execution allows you to optionally execute different sections of code.
- Repetition or looping allows you to re-execute the same section of code several times.
- Example: Add up 50 numbers entered one per line.
SUM = 0
READ *, NUM
SUM = SUM + NUM
READ *, NUM
SUM = SUM + NUM
. . .
READ *, NUM
SUM = SUM + NUM
PRINT *, 'The total sum is', SUM
or more simply
SUM = 0
[re-execute the following 50 times:]
READ *, NUM
SUM = SUM + NUM
PRINT *, 'The total sum is', SUM
- Repetition is more efficient and flexible.
DO/CONTINUE Statement (FORTRAN-77)
- To re-execute one or more statements use the DO/CONTINUE statement.
DO label count = start, end, inc
statements to re-execute
label CONTINUE
12345678901234567890123456780
-----------------------------
DO 10 CTR = 1, 50, 1
READ *, NUM
SUM = SUM + NUM
10 CONTINUE
- inc defaults to 1 if not specified.
DO 10 CTR = 1, 50
- Use a negative value for inc to count down.
DO 10 CTR = 50, 1, -1
- count must be an INTEGER or REAL variable.
How a DO Loops Work
- count is initialized to start before the first iteration. After each iteration count is incremented by inc. The loop terminates when count > end (Note: not >=).
- The start, end and inc may be literals, variables or expressions.
MAX = 50
DO 10 CTR = 1, MAX, SQRT(9)-2
- Normally use INTEGER values for start, end and inc.
end - start + inc
Number of iterations = -----------------
inc
- If start > end the loop isn't executed at all!
MIN = 55
DO 10 CTR = MIN, 50, 1
- What if start, end or inc are changed inside the loop?
MIN = 1
MAX = 50
DO 10 CTR = MIN, MAX
MAX = CTR + 5
PRINT *, 'spam'
10 CONTINUE
- The start, end or inc are evaluated only once at the beginning. Only their initial values are important.
Labels
- The label is the line number of the CONTINUE statement to indicate the end of the loop. Every statement up to the CONTINUE is re-executed.
DO 10 CTR = 1, 50
...
10 CONTINUE
- The label is written in columns 2 though 5. The DO and CONTINUE statements start in column 7.
- If you have multiple DO loops then keep the labels in ascending order and use multiples of 10 for the different numbers.
DO 10 FOO = 1, 50
...
10 CONTINUE
DO 20 BAR = 1, 10
...
20 CONTINUE
Conditional Loops
- DO statement re-executes the same statements a pre-determined number of times.
- What if don't know the number of iterations ahead of time?
SUM = 0
READ *, NUM
[do the following until NUM equals 99]
SUM = SUM + NUM
READ *, NUM
PRINT *, 'The total sum is', SUM
DO WHILE Statement (FORTRAN-90)
- Tests a condition on each iteration. If the condition is .TRUE. then re-execute the loop.
DO WHILE (condition)
statements to re-execute
END DO
DO WHILE (NUM .NE. 99)
SUM = SUM + NUM
READ *, NUM
END DO
- The condition is a logical expression, just like an IF statement.
- Can re-write a DO/CONTINUE loop as a DO WHILE loop.
DO 10 COUNT = MIN, MAX, 2
PRINT *, COUNT
10 CONTINUE
is the functionally the same as
COUNT = MIN
DO WHILE (COUNT .LE. MAX)
PRINT *, COUNT
COUNT = COUNT + 2
END DO
- The condition must contain a variable who's value is changed inside the loop. Otherwise the condition always remains .TRUE. (i.e. an infinite loop)
- Right:
DO WHILE (NUM .NE. 0)
PRINT *, NUM
READ *, NUM
END DO
- Wrong:
DO WHILE (NUM .NE. 0)
PRINT *, NUM
SUM = SUM + NUM
END DO
FORTRAN-77 "DO WHILE" Loop
- The DO WHILE statement is not standard FORTRAN-77 and may not be available on all compilers. But sometimes we cannot use the FORTRAN-77 DO/CONTINUE loop.
- To implement the equivalent of a DO WHILE loop in FORTRAN-77 we can use an IF statement and a GOTO statement.
DO WHILE (NUM .NE. 0)
...
END DO
can be written as
10 IF (NUM .NE. 0) THEN
...
GOTO 100
END IF
- The GOTO statement "jumps" to resume executing the statement at the specified label.
- WARNING: Never use GOTO statements anywhere in your program except if you have a strict FORTRAN-77 compiler and then only to implement DO WHILE loops.
- SECOND WARNING: Anyone caught using GOTO statements in this class will automatically receive a mark of zero for that assignment. Just say NO to GOTOs!
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.