0% found this document useful (0 votes)
28 views3 pages

Fortran Loops: Do-Loop Explained

The document provides a tutorial on loops in Fortran, specifically focusing on the do-loop, while-loop, and until-loop constructs. It explains the syntax and usage of these loops, including examples and best practices for writing loops in Fortran 77. Additionally, it briefly mentions the improvements in loop constructs introduced in Fortran 90.

Uploaded by

golamrobbani2988
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views3 pages

Fortran Loops: Do-Loop Explained

The document provides a tutorial on loops in Fortran, specifically focusing on the do-loop, while-loop, and until-loop constructs. It explains the syntax and usage of these loops, including examples and best practices for writing loops in Fortran 77. Additionally, it briefly mentions the improvements in loop constructs introduced in Fortran 90.

Uploaded by

golamrobbani2988
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

2/26/2021 Fortran Tutorial

9. Loops
For repeated execution of similar things, loops are used. If you are familiar with other programming languages
you have probably heard about for-loops, while-loops, and until-loops. Fortran 77 has only one loop construct,
called the do-loop. The do-loop corresponds to what is known as a for-loop in other languages. Other loop
constructs have to be simulated using the if and goto statements.

do-loops
The do-loop is used for simple counting. Here is a simple example that prints the cumulative sums of the integers
from 1 through n (assume n has been assigned a value elsewhere):
integer i, n, sum

sum = 0
do 10 i = 1, n
sum = sum + i
write(*,*) 'i =', i
write(*,*) 'sum =', sum
10 continue

The number 10 is a statement label. Typically, there will be many loops and other statements in a single program
that require a statement label. The programmer is responsible for assigning a unique number to each label in
each program (or subprogram). Recall that column positions 2-5 are reserved for statement labels. The numerical
value of statement labels have no significance, so any integer numbers can be used. Typically, most
programmers increment labels by 10 at a time.

The variable defined in the do-statement is incremented by 1 by default. However, you can define any other
integer to be the step. This program segment prints the even numbers between 1 and 10 in decreasing order:
integer i

do 20 i = 10, 1, -2
write(*,*) 'i =', i
20 continue

The general form of the do loop is as follows:


do label var = expr1, expr2, expr3
statements
label continue

var is the loop variable (often called the loop index) which must be integer. expr1 specifies the initial value of
var, expr2 is the terminating bound, and expr3 is the increment (step).

Note: The do-loop variable must never be changed by other statements within the loop! This will cause great
confusion.

Many Fortran 77 compilers allow do-loops to be closed by the enddo statement. The advantage of this is that the
statement label can then be omitted since it is assumed that an enddo closes the nearest previous do statement.
The enddo construct is widely used, but it is not a part of ANSI Fortran 77.

while-loops

The most intuitive way to write a while-loop is

[Link] 1/3
2/26/2021 Fortran Tutorial
while (logical expr) do
statements
enddo

or alternatively,
do while (logical expr)
statements
enddo

The statements in the body will be repeated as long as the condition in the while statement is true. Even though
this syntax is accepted by many compilers, it is not ANSI Fortran 77. The correct way is to use if and goto:
label if (logical expr) then
statements
goto label
endif

Here is an example that calculates and prints all the powers of two that are less than or equal to 100:
integer n

n = 1
10 if (n .le. 100) then
n = 2*n
write (*,*) n
goto 10
endif

until-loops
If the termination criterium is at the end instead of the beginning, it is often called an until-loop. The pseudocode
looks like this:
do
statements
until (logical expr)

Again, this should be implemented in Fortran 77 by using if and goto:


label continue
statements
if (logical expr) goto label

Note that the logical expression in the latter version should be the negation of the expression given in the
pseudocode!

Loops in Fortran 90

Fortran 90 has adopted the do-enddo construct as its loop construct. So our "counting down in twos" example
will look like this:
do i = 10, 1, -2
write(*,*) 'i =', i
end do

For while and until loops you also use the do-enddo construct, but you have to add a conditional exit statement.
The general case is:
do
statements
if (logical expr) exit
[Link] 2/3
2/26/2021 Fortran Tutorial

statements
end do

If you have the exit condition at the beginning it is a while loop, and if it is at the end you have an until loop.

Exercises
Exercise A
Rewrite these pseudo-code loops into proper Fortran 77 syntax. Avoid goto if possible.
i = 1
while (i<100) do
sum = sum + i
i = i+2
enddo

i = 0
x = 1.0
repeat
x = f(x)
i = i+1
until (x<0)
write(*,*) i, x

Exercise B
The following code is poorly written. Rewrite it in good F77 style. (Tip: Compile and run to verify that the
new version gives the same answer as the old version.)
i = 1
sum = 0
10 do 20 i = 1, 50
if (i .gt. 10) goto 30
sum = sum + i
20 continue
30 if (i. le. 20) then
sum = sum - 1
goto 20
else
sum = 2*sum
endif
write(*,*) 'Sum =', sum

[Fortran Tutorial Home]

boman@[Link]

[Link] 3/3

Common questions

Powered by AI

The use of goto statements in Fortran 77 for managing loops and conditional flows can lead to pitfalls such as 'spaghetti code,' where the control flow becomes tangled and hard to follow. This practice increases the likelihood of logic errors, maintenance difficulties, and can obscure the intended structure of the program. Overreliance on goto can detract from readability and contribute to error-prone programming, as it blurs the defined start and endpoints of logical constructs typically managed by more structured loops and control statements in newer languages .

Fortran 90 introduced the do-enddo construct, which simplified the closing of loops without requiring statement labels, a feature that improved code readability and reduced errors associated with managing labels. Additionally, Fortran 90 included support for while and until constructs with conditional exits, enhancing expressiveness and flexibility. These changes align Fortran more closely with other high-level languages and have a positive impact on programming practices by fostering clearer and more structured code .

Statement labels in Fortran 77 affect program readability and structure by adding a level of complexity as they require unique assignment by the programmer and must be managed individually. While helpful in directing program flow, excessive use or poor management of statement labels can make the program difficult to follow and increase the likelihood of errors, thus impacting readability and maintainability .

It is important not to modify the do-loop variable within the loop because doing so can cause great confusion. The loop's behavior relies on a predictable incrementing of the do-loop variable as defined in the loop's header. Changing this variable elsewhere can make code harder to understand and maintain, and can introduce logical errors .

Statement labels in Fortran 77 are structured as integers placed in reserved column positions (2-5) on a line. They are used to mark specific lines in the code, most commonly as loop start or end points, to direct execution using goto statements. Conventionally, labels are incremented by tens (10, 20, 30) to allow easy insertion of additional labeled lines without alteration of the existing structure .

Using pseudocode constructs for while and until loops in Fortran 77 can improve a developer's conceptual understanding and design but may reduce code execution efficiency and maintenance clarity. Pseudocode often provides a clear logic blueprint, but translating it into actual Fortran 77 requires if and goto statements, which can complicate the code structure and make it prone to error. Native constructs like the do-loop are more efficient and directly supported by the language, leading to cleaner and usually more reliable code .

To rewrite a pseudo-code loop into Fortran 77 syntax while avoiding the use of the goto statement, you would use the do-loop construct for counting loops. For example, if the pseudo-code involves a counting loop, translate it by identifying the initial value, bound, and increment. Utilize if statements to handle conditions and break the loop using logic checks. While it is mainly designed for counting loops, smart usage of conditional statements can divert the flow and mimic other loop behaviors .

Statement labels in Fortran 77 serve as predefined markers that control the program's flow, especially in loops and when using goto statements. They mark the start or end of a loop and provide a target for the goto command to redirect execution. This is crucial for structured flow control since labels dictate where execution should continue in iterative processes or conditional branches .

The primary loop construct in Fortran 77 is the do-loop, which corresponds to the for-loop in other programming languages. Unlike modern languages that support multiple loop constructs like for, while, and until, Fortran 77 uses the do-loop for simple counting. To simulate other loop behaviors, such as while and until, you must use a combination of if and goto statements .

In Fortran 77, you can simulate a while-loop using the combination of if and goto statements. You create a label before the if statement that checks your loop condition. If the condition is met, the loop body executes followed by a goto statement that redirects the flow back to the label, effectively forming a loop .

You might also like