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!
===Early exit and continuation=== Some languages may also provide other supporting statements, which when present can alter how the for-loop iteration proceeds. Common among these are the [[Break statement|break]] and [[Control flow#Continuation with next iteration|continue]] statements found in C and its derivatives. The break statement causes the innermost loop to be terminated immediately when executed. The continue statement will move at once to the next iteration without further progress through the loop body for the current iteration. A for statement also terminates when a break, goto, or return statement within the statement body is executed.[Wells] Other languages may have similar statements or otherwise provide means to alter the for-loop progress; for example in Fortran 90: <syntaxhighlight lang="Fortran"> DO I = 1, N statements!Executed for all values of "I", up to a disaster if any. IF (no good) CYCLE! Skip this value of "I", and continue with the next. Statements!Executed only where goodness prevails. IF (disaster) EXIT! Abandon the loop. Statements!While good and, no disaster. END DO! Should align with the "DO". </syntaxhighlight> Some languages offer further facilities such as naming the various loop constructs so that with multiple nested loops there is no doubt as to which loop is involved. Fortran 90, for example: <syntaxhighlight lang="Fortran"> X1:DO I = 1, N statements X2:DO J = 1, M statements IF (trouble) CYCLE X1 statements END DO X2 statements END DO X1 </syntaxhighlight> Thus, when "trouble" is detected in the inner loop, the CYCLE X1 (not X2) means that the skip will be to the next iteration for I, ''not'' J. The compiler will also be checking that each END DO has the appropriate label for its position: this is not just a documentation aid. The programmer must still code the problem correctly, but some possible blunders will be blocked.
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)