Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
For loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===1957: FORTRAN=== {{Further|Fortran}} Fortran's equivalent of the {{mono|for}} loop is the {{mono|DO}} loop, using the keyword do instead of for, The syntax of Fortran's {{mono|DO}} loop is: <syntaxhighlight lang="fortranfixed"> DO label counter = first, last, step statements label statement </syntaxhighlight> The following two examples behave equivalently to the three argument for-loop in other languages, initializing the counter variable to 1, incrementing by 1 each iteration of the loop, and stopping at five (inclusive). <syntaxhighlight lang="fortranfixed"> DO 9, ICOUNT = 1, 5, 1 WRITE (6,8) ICOUNT 8 FORMAT( I2 ) 9 CONTINUE </syntaxhighlight> As of Fortran 90, block structured {{mono|END DO}} was added to the language. With this, the end of loop label became optional: <syntaxhighlight lang="Fortran"> do icounter = 1, 5 write(*, '(i2)') icounter end do </syntaxhighlight> The step part may be omitted if the step is one. Example: <syntaxhighlight lang="fortranfixed"> * DO loop example. PROGRAM MAIN INTEGER SUMSQ SUMSQ = 0 DO 199 I = 1, 9999999 IF (SUMSQ.GT.1000) GO TO 200 199 SUMSQ = SUMSQ + I**2 200 PRINT 206, SUMSQ 206 FORMAT( I2 ) END </syntaxhighlight> In Fortran 90, the {{mono|GO TO}} may be avoided by using an {{mono|EXIT}} statement. <syntaxhighlight lang="fortranfixed"> * DO loop example. program main implicit none integer:: sumsq integer:: i sumsq = 0 do i = 1, 9999999 if (sumsq > 1000) exit sumsq = sumsq + i**2 end do print *, sumsq end program </syntaxhighlight> Alternatively, a {{mono|DO - WHILE}} construct could be used: <syntaxhighlight lang="fortranfixed"> program main implicit none integer:: sumsq integer:: i sumsq = 0 i = 0 do while (sumsq <= 1000) i = i+1 sumsq = sumsq + i**2 end do print *, sumsq end program </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)