ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.6 months ago (distributed domain, exempt) |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | FAIL | meta_canonical IS NULL OR = '' OR = src_unparsed | org,wikipedia!en,/wiki/Loop_(statement) s443 |
| Property | Value |
|---|---|
| URL | https://en.wikipedia.org/wiki/While_loop |
| Last Crawled | 2026-03-20 17:10:35 (18 days ago) |
| First Indexed | 2013-08-09 05:49:26 (12 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Loop (statement) - Wikipedia |
| Meta Description | null |
| Meta Canonical | org,wikipedia!en,/wiki/Loop_(statement) s443 |
| Boilerpipe Text | From Wikipedia, the free encyclopedia
In
computer programming
, a
loop
is a
control flow
statement
that allows code to be executed repeatedly, usually with minor alterations between repetitions. Loops can be used to perform a repeated action on all items in a
collection
, or to implement a long lived program.
Loops are a feature of
high-level programming languages
. In
low-level programming languages
the same functionality is achieved using
jumps
. When a program is
compiled
to
machine code
, looping may be achieved using jumps; but some loops can be
optimized
to run without jumping.
[
1
]
Usually, loops are expected to run for a finite number of iteration.
[
citation needed
]
Without proper care, loops may accidentally be created that have no possibility of terminating. Such loops are called
infinite loops
. The problem of determining whether a program contains an infinite loop is known as the
halting problem
.
A
conditional loop
(also known as an
indeterminate loop
[
2
]
) is a loop that determines whether to terminate based on a logical condition.
[
3
]
These loops are flexible, but there exact behavior can be difficult to reason about.
[
4
]
: 368
A conditional loop is usually composed of two parts: a
condition
and a
body
. The
condition
is a logical statement depending on the state of the program and the
body
is a block of code that runs as long as the
condition
holds.
[
3
]
A common misconception is that the execution of the
body
terminates as soon as the
condition
does not hold anymore; but this is usually not the case.
[
4
]
: 368
In most programming languages, the
condition
is checked once for every execution of the
body
. When the
condition
is checked is not standardized and some programming languages contain multiple conditional looping structures with different rules about when the
condition
is assessed.
[
citation needed
]
A
pre-test loop
is a conditional loop where the
condition
is checked before the
body
is executed. More precisely, the
condition
is checked and if it holds the
body
is execute. Afterwards, the
condition
is checked again, and if it holds the
body
is executed again. This process repeats until the
condition
does not hold. Many programming languages call this loop a
while loop
and refer to it with the
keyword
while
. They are commonly formatted in manner similar to
while
condition
do
body
repeat
Instead of the keywords
do
and
repeat
others methods are sometime use to indicate where the
body
begins and ends, such as
curly braces
[
5
]
or
whitespace
.
[
6
]
For example, the following code fragment first checks whether
x
is less than five, which it is, so the
body
is entered. There,
x
is displayed and then incremented by one. After executing the statements in the
body
, the
condition
is checked again, and the loop is executed again. This process repeats until
x
has the value five.
x
← 0
while
x
< 5
do
display(
x
)
x
←
x
+ 1
repeat
Do-While loop flow diagram
A
post-test loop
is a conditional loop where the
condition
is checked after the
body
is executed. More precisely, the
body
is executed and afterwards the
condition
is checked. If it holds the
body
is run again and then the
condition
is checked. This is repeated until the
condition
does not hold. This is sometimes called a
do-while loop
due to the syntax used in various programming languages,
[
7
]
although this can be confusing since
Fortran
and
PL/I
use the syntax "DO WHILE" for pre-test loops.
[
8
]
[
9
]
Post-test loops are commonly formatted in manner similar to
do
body
repeat
while
condition
Instead of the keywords
do
and
repeat
others methods are sometime use to indicate where the
body
begins and ends, such as curly braces.
[
10
]
Some
languages
may use a different naming convention for this type of loop. For example, the
Pascal
and
Lua
languages have a "
repeat until
" loop, which continues to run
until
the control expression is true and then terminates.
[
11
]
[
12
]
Three-part for loop
[
edit
]
Flow diagram of a for loop that prints five asterisks.
A
three-part for loop
, popularized by C, has two additional parts:
initialization
(
loop variant
), and
increment
, both of which are blocks of code. The
initialization
is intended as code that prepares the loop and is run once in the beginning and
increment
is used to update the state of the program after each iteration of the loop. Otherwise, the three-part for loop is a pre-test loop.
They are commonly formatted in manner similar to
for
initialization
,
condition
,
increment
do
body
repeat
This syntax came from
B
and was originally invented by
Stephen C. Johnson
.
[
13
]
The following C code is an example of a three part loop that prints the numbers from 0 to 4.
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
printf
(
"%d
\n
"
,
i
);
}
Equivalent constructs
[
edit
]
Assuming there is a function called
do_work()
that does some work, the following are equivalent.
[
citation needed
]
do
do_work()
repeat while
condition
do_work()
while
condition
do
do_work()
repeat
As long as the
continue
statement is not used, the above is technically equivalent to the following (though these examples are not typical or modern style used in everyday computers):
while
true
do
do_work()
if
condition
is not
true
then
break
end if
repeat
or
LOOPSTART:
do_work()
if
condition
then
goto
LOOPSTART
end if
foreach
loops are almost always used to iterate over items in a sequence of elements.
An
enumeration
(also known as an
determinate loop
[
2
]
) is a loop intended to iterate over all the items of a collection.
[
14
]
It is not as flexible as a conditional loop; but it is more predictable.
[
citation needed
]
For example, it is easier to guarantee that enumerations
terminate
and they avoid potential
off-by-one errors
.
[
citation needed
]
Enumerations can be implemented using an
iterator
, whether implicitly or explicitly. They are commonly formatted in manner similar to
for
item
in
collection
body
repeat
Depending on the programming language, various keywords are used to invoke enumerations. For example, descendants of
ALGOL
use
for
,
[
15
]
while descendants of
Fortran
use
do
[
16
]
and
COBOL
uses
PERFORM VARYING
.
[
17
]
Enumerations are sometimes called "for loops," for example in
Zig
and
Rust
.
[
18
]
[
19
]
This can be confusing since many of the most popular
[
20
]
programming languages, such as C,
C++
, and
Java
, use that term for the three-part for loop,
[
21
]
[
22
]
[
23
]
which is not an enumeration. Other programming languages, such as
Perl
and
C#
, avoid this confusion by using the term "foreach loop."
[
24
]
[
25
]
The order in which the items in the collection are iterated through depends on the programming language.
Fortran 95
has a loop, invoked using the keyword
FORALL
, that is independent of this order. It has the effect of executing each iteration of the loop at the same time. This feature was made obsolescent in
Fortran 2018
.
[
26
]
Loops in functional programming
[
edit
]
In most
functional programming languages
,
recursion
is used instead of traditional loops. This is due to the fact that variables are
immutable
, and therefore the
increment
step of a loop cannot occur.
[
27
]
To avoid running into
stack overflow
errors for long loops, functional programming languages implement
tail call optimisation
, which allows the same
stack frame
to be used for each iteration of the loop, compiling to effectively the same code as a
while
or
for
loop.
[
28
]
Some languages, such as
Haskell
, have a special syntax known as a
list comprehension
, which is similar to enumeration, iterating over the contents of a list and transforming it into a new list.
A
loop counter
is a control variable that controls the iterations of a loop. Loop counters change with each iteration of a loop, providing a unique value for each iteration. The loop counter is used to decide when the loop should terminate. It is so named because most uses of this construct result in the variable taking on a range of integer values.
A common
identifier naming convention
is for the loop counter to use the variable names
i
,
j
, and
k
(and so on if needed),
[
29
]
where
i
would be the most outer loop,
j
the next inner loop, etc. This style is generally agreed to have originated from the early programming of Fortran
[
citation needed
]
, where these variable names beginning with these letters were implicitly declared as having an integer type, and so were obvious choices for loop counters that were only temporarily required. The practice dates back further to
mathematical notation
where
indices
for
sums
and
multiplications
are often
i
,
j
, and
k
.
Using terse names for loop counters, like
i
and
j
, is discouraged by some since the purpose of the variables is not as clear as if they were given a longer more descriptive name.
[
4
]
: 383–382
Different languages specify different rules for what value the loop counter will hold on termination of its loop, and indeed some hold that it becomes undefined. This permits a
compiler
to generate code that leaves any value in the loop counter, or perhaps even leaves it unchanged because the loop value was held in a register and never stored in memory. Actual behavior may even vary according to the compiler's optimization settings.
Modifying the loop counter within the body of the loop can lead to unexpected consequences. To prevent such problems, some languages make the loop counter
immutable
.
[
citation needed
]
However, only overt changes are likely to be detected by the compiler. Situations where the address of the loop counter is passed as an argument to a
subroutine
, make it very difficult to check because the routine's behavior is in general unknowable to the compiler unless the language supports procedure signatures and argument intents.
[
citation needed
]
Early exit and continuation
[
edit
]
Some languages may also provide supporting statements for altering how a loop's iteration proceeds. Common among these are the
break
statement, which terminates the current loop the program is in, and the
continue
statement, which skips to the next iteration of the current loop.
[
4
]
: 379
These statements may have other names; For example in
Fortran 90
, they are called
exit
and
cycle
.
[
citation needed
]
A loop can also be terminated by
returning
from the function within which it is being executed.
In the case of nested loops, the
break
and
continue
statements apply to the inner most loop. Some languages allow loops to be labelled. These statements can then be applied to any of the loops in which the program is nested.
outer_loop:
(This is a label for the outermost loop)
for
1 ≤ i ≤ 2
do
for
1 ≤ j ≤ 2
do
display(i, j)
if
i = 2
continue
outer_loop
end if
repeat
repeat
(This nested loop displays the pairs (1, 1), (1, 2), and (2, 1))
An infinite loop is a loop which never terminates. This can be intentional, or the result of a
logic error
.
Systematically detecting infinite loops is known as the
halting problem
.
[
30
]
Infinite loops are useful in applications which need to perform a repeated calculation until a program terminates, such as
web servers
.
[
31
]
Primitive recursive function
General recursive function
Repeat loop (disambiguation)
LOOP (programming language)
– a programming language with the property that the functions it can compute are exactly the primitive recursive functions
^
Angelou, Alexandros; Dadaliaris, Antonios; Dossis, Michael; Dimitriou, Georgios (2021-11-26).
"Branchless Code Generation for Modern Processor Architectures"
.
25th Pan-Hellenic Conference on Informatics
. New York, NY, USA: ACM:
300–
305.
doi
:
10.1145/3503823.3503879
.
ISBN
978-1-4503-9555-7
.
^
a
b
Samanta, Debasis; Sarma, Monalisa (15 June 2023).
Joy with Java: Fundamentals of Object Oriented Programming
. Cambridge University Press. p. 124.
ISBN
978-1-009-21190-1
.
^
a
b
"Conditional loops - Computational constructs - National 4 Computing Science Revision"
.
BBC Bitesize
. Archived from
the original
on 19 October 2021
. Retrieved
8 January
2026
.
^
a
b
c
d
McConnell, Steve (9 June 2004).
Code Complete
. Pearson Education.
ISBN
978-0-7356-3697-2
.
^
"while Statement (GNU C Language Manual)"
.
www.gnu.org
. Archived from
the original
on 12 July 2024
. Retrieved
8 January
2026
.
^
"3. An Informal Introduction to Python"
.
Python documentation
. Archived from
the original
on 31 December 2025
. Retrieved
8 January
2026
.
^
"do...while - JavaScript | MDN"
.
MDN Web Docs
. 2025-07-08
. Retrieved
2026-01-22
.
^
"DO WHILE (FORTRAN 77 Language Reference)"
.
docs.oracle.com
. Archived from
the original
on 14 March 2023
. Retrieved
8 January
2026
.
^
"DO command (PL/I)"
.
www.ibm.com
. Archived from
the original
on 2 March 2025
. Retrieved
22 January
2026
.
^
"do-while Statement (GNU C Language Manual)"
.
www.gnu.org
. Archived from
the original
on 13 July 2024
. Retrieved
8 January
2026
.
^
"The Repeat..until statement"
.
www.freepascal.org
. Archived from
the original
on 9 November 2025
. Retrieved
22 January
2026
.
^
"Programming in Lua : 4.3.3"
.
www.lua.org
. Archived from
the original
on 2 January 2026
. Retrieved
22 January
2026
.
^
Thompson, Ken
.
VCF East 2019 – Brian Kernighan interviews Ken Thompson
.
YouTube
.
Archived
from the original on 2021-12-12
. Retrieved
2020-11-16
.
I saw Johnson's semicolon version of the for loop and I put that in [B], I stole it.
^
McConnell, Steve (9 June 2004).
Code Complete
. Pearson Education. p. 367.
ISBN
978-0-7356-3697-2
.
^
Wirth, Niklaus
(1973). "Preface".
Systematic Programming: An Introduction
. Prentice-Hall. pp. xiii.
ISBN
0138803692
.
^
"DO / END DO"
.
www.ibm.com
. 24 April 2018. Archived from
the original
on 8 January 2026
. Retrieved
8 January
2026
.
^
"PERFORM with VARYING Phrase"
.
www.ibm.com
. June 2012. Archived from
the original
on 8 January 2026
. Retrieved
8 January
2026
.
^
"Zig documentation"
.
ziglang.org
. Archived from
the original
on 4 January 2026
. Retrieved
8 January
2026
.
^
"Looping Through a Collection with for"
.
rust-lang.org
. Archived from
the original
on 19 December 2025
. Retrieved
8 January
2026
.
^
"TIOBE Index for September 2024"
. Archived from
the original
on January 4, 2026
. Retrieved
2025-12-16
.
^
"for Statement (GNU C Language Manual)"
.
www.gnu.org
. Archived from
the original
on 13 July 2024
. Retrieved
8 January
2026
.
^
"for statement (C++)"
.
learn.microsoft.com
. Archived from
the original
on 28 September 2025
. Retrieved
8 January
2026
.
^
"The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)"
.
docs.oracle.com
. Archived from
the original
on 28 December 2025
. Retrieved
8 January
2026
.
^
"Iteration statements -for, foreach, do, and while - C# reference"
.
learn.microsoft.com
. Archived from
the original
on 28 December 2025
. Retrieved
8 January
2026
.
^
"Perl for Loop"
.
Perl Tutorial
. Archived from
the original
on 7 June 2025
. Retrieved
8 January
2026
.
^
"FORALL"
.
Intel
. Archived from
the original
on 1 January 2026
. Retrieved
8 January
2026
.
^
Hinsen, Konrad (2009).
"The Promises of Functional Programming"
.
Computing in Science & Engineering
.
11
(4):
86–
90.
Bibcode
:
2009CSE....11d..86H
.
doi
:
10.1109/mcse.2009.129
.
ISSN
1521-9615
.
^
Clinger, William D. (1998). "Proper tail recursion and space efficiency".
Proceedings of the ACM SIGPLAN 1998 conference on Programming language design and implementation
. New York, NY, USA: ACM. pp.
174–
185.
doi
:
10.1145/277650.277719
.
ISBN
0-89791-987-4
.
^
http://www.knosof.co.uk/vulnerabilities/loopcntrl.pdf
Analysis of loop control variables in C
^
Lee, Sarah.
"Turing's Halting Problem Explained"
.
www.numberanalytics.com
. Retrieved
2026-01-22
.
^
"What is an infinite loop (endless loop)?"
.
WhatIs
. Retrieved
2026-01-22
. |
| Markdown | [Jump to content](https://en.wikipedia.org/wiki/While_loop#bodyContent)
Main menu
Main menu
move to sidebar
hide
Navigation
- [Main page](https://en.wikipedia.org/wiki/Main_Page "Visit the main page [z]")
- [Contents](https://en.wikipedia.org/wiki/Wikipedia:Contents "Guides to browsing Wikipedia")
- [Current events](https://en.wikipedia.org/wiki/Portal:Current_events "Articles related to current events")
- [Random article](https://en.wikipedia.org/wiki/Special:Random "Visit a randomly selected article [x]")
- [About Wikipedia](https://en.wikipedia.org/wiki/Wikipedia:About "Learn about Wikipedia and how it works")
- [Contact us](https://en.wikipedia.org/wiki/Wikipedia:Contact_us "How to contact Wikipedia")
Contribute
- [Help](https://en.wikipedia.org/wiki/Help:Contents "Guidance on how to use and edit Wikipedia")
- [Learn to edit](https://en.wikipedia.org/wiki/Help:Introduction "Learn how to edit Wikipedia")
- [Community portal](https://en.wikipedia.org/wiki/Wikipedia:Community_portal "The hub for editors")
- [Recent changes](https://en.wikipedia.org/wiki/Special:RecentChanges "A list of recent changes to Wikipedia [r]")
- [Upload file](https://en.wikipedia.org/wiki/Wikipedia:File_upload_wizard "Add images or other media for use on Wikipedia")
- [Special pages](https://en.wikipedia.org/wiki/Special:SpecialPages "A list of all special pages [q]")
[  ](https://en.wikipedia.org/wiki/Main_Page)
[Search](https://en.wikipedia.org/wiki/Special:Search "Search Wikipedia [f]")
Appearance
- [Donate](https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikipedia.org&uselang=en)
- [Create account](https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Loop+%28statement%29 "You are encouraged to create an account and log in; however, it is not mandatory")
- [Log in](https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Loop+%28statement%29 "You're encouraged to log in; however, it's not mandatory. [o]")
Personal tools
- [Donate](https://donate.wikimedia.org/?wmf_source=donate&wmf_medium=sidebar&wmf_campaign=en.wikipedia.org&uselang=en)
- [Create account](https://en.wikipedia.org/w/index.php?title=Special:CreateAccount&returnto=Loop+%28statement%29 "You are encouraged to create an account and log in; however, it is not mandatory")
- [Log in](https://en.wikipedia.org/w/index.php?title=Special:UserLogin&returnto=Loop+%28statement%29 "You're encouraged to log in; however, it's not mandatory. [o]")
## Contents
move to sidebar
hide
- [(Top)](https://en.wikipedia.org/wiki/While_loop)
- [1 Overview](https://en.wikipedia.org/wiki/While_loop#Overview)
- [2 Conditional loop](https://en.wikipedia.org/wiki/While_loop#Conditional_loop)
Toggle Conditional loop subsection
- [2\.1 Pre-test loop](https://en.wikipedia.org/wiki/While_loop#Pre-test_loop)
- [2\.2 Post-test loop](https://en.wikipedia.org/wiki/While_loop#Post-test_loop)
- [2\.3 Three-part for loop](https://en.wikipedia.org/wiki/While_loop#Three-part_for_loop)
- [2\.4 Equivalent constructs](https://en.wikipedia.org/wiki/While_loop#Equivalent_constructs)
- [3 Enumeration](https://en.wikipedia.org/wiki/While_loop#Enumeration)
- [4 Loops in functional programming](https://en.wikipedia.org/wiki/While_loop#Loops_in_functional_programming)
- [5 Loop counter](https://en.wikipedia.org/wiki/While_loop#Loop_counter)
- [6 Early exit and continuation](https://en.wikipedia.org/wiki/While_loop#Early_exit_and_continuation)
- [7 Infinite loop](https://en.wikipedia.org/wiki/While_loop#Infinite_loop)
- [8 See also](https://en.wikipedia.org/wiki/While_loop#See_also)
- [9 References](https://en.wikipedia.org/wiki/While_loop#References)
Toggle the table of contents
# Loop (statement)
25 languages
- [Boarisch](https://bar.wikipedia.org/wiki/Schleifn_\(Programmierung\) "Schleifn (Programmierung) – Bavarian")
- [Български](https://bg.wikipedia.org/wiki/%D0%A6%D0%B8%D0%BA%D1%8A%D0%BB_\(%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%B8%D1%80%D0%B0%D0%BD%D0%B5\) "Цикъл (програмиране) – Bulgarian")
- [Català](https://ca.wikipedia.org/wiki/Bucle_\(programaci%C3%B3\) "Bucle (programació) – Catalan")
- [Deutsch](https://de.wikipedia.org/wiki/Schleife_\(Programmierung\) "Schleife (Programmierung) – German")
- [Español](https://es.wikipedia.org/wiki/Bucle_\(programaci%C3%B3n\) "Bucle (programación) – Spanish")
- [Eesti](https://et.wikipedia.org/wiki/Ts%C3%BCkkel_\(programmeerimine\) "Tsükkel (programmeerimine) – Estonian")
- [Euskara](https://eu.wikipedia.org/wiki/Begizta_\(programazioa\) "Begizta (programazioa) – Basque")
- [Suomi](https://fi.wikipedia.org/wiki/Toistorakenne "Toistorakenne – Finnish")
- [Gaeilge](https://ga.wikipedia.org/wiki/L%C3%BAb "Lúb – Irish")
- [עברית](https://he.wikipedia.org/wiki/%D7%9C%D7%95%D7%9C%D7%90%D7%94_\(%D7%AA%D7%9B%D7%A0%D7%95%D7%AA\) "לולאה (תכנות) – Hebrew")
- [Magyar](https://hu.wikipedia.org/wiki/Ciklus_\(programoz%C3%A1s\) "Ciklus (programozás) – Hungarian")
- [Italiano](https://it.wikipedia.org/wiki/Ciclo_\(informatica\) "Ciclo (informatica) – Italian")
- [日本語](https://ja.wikipedia.org/wiki/%E3%83%AB%E3%83%BC%E3%83%97_\(%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0\) "ループ (プログラミング) – Japanese")
- [Қазақша](https://kk.wikipedia.org/wiki/%D0%A6%D0%B8%D0%BA%D0%BB_\(%D0%B8%D0%BD%D1%84%D0%BE%D1%80%D0%BC%D0%B0%D1%82%D0%B8%D0%BA%D0%B0\) "Цикл (информатика) – Kazakh")
- [Latviešu](https://lv.wikipedia.org/wiki/Cikls_\(programm%C4%93%C5%A1ana\) "Cikls (programmēšana) – Latvian")
- [Олык марий](https://mhr.wikipedia.org/wiki/%D0%A6%D0%B8%D0%BA%D0%BB "Цикл – Eastern Mari")
- [Nederlands](https://nl.wikipedia.org/wiki/Repetitie_\(informatica\) "Repetitie (informatica) – Dutch")
- [Polski](https://pl.wikipedia.org/wiki/P%C4%99tla_\(informatyka\) "Pętla (informatyka) – Polish")
- [Português](https://pt.wikipedia.org/wiki/Estrutura_de_repeti%C3%A7%C3%A3o "Estrutura de repetição – Portuguese")
- [Русский](https://ru.wikipedia.org/wiki/%D0%A6%D0%B8%D0%BA%D0%BB_\(%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%BC%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5\) "Цикл (программирование) – Russian")
- [Српски / srpski](https://sr.wikipedia.org/wiki/Ra%C4%8Dunarska_petlja "Računarska petlja – Serbian")
- [Svenska](https://sv.wikipedia.org/wiki/Loop_\(programmering\) "Loop (programmering) – Swedish")
- [Українська](https://uk.wikipedia.org/wiki/%D0%A6%D0%B8%D0%BA%D0%BB_\(%D0%BF%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D1%83%D0%B2%D0%B0%D0%BD%D0%BD%D1%8F\) "Цикл (програмування) – Ukrainian")
- [粵語](https://zh-yue.wikipedia.org/wiki/%E8%BF%B4%E5%9C%88 "迴圈 – Cantonese")
- [中文](https://zh.wikipedia.org/wiki/%E8%BF%B4%E5%9C%88 "迴圈 – Chinese")
[Edit links](https://www.wikidata.org/wiki/Special:EntityPage/Q8868615#sitelinks-wikipedia "Edit interlanguage links")
- [Article](https://en.wikipedia.org/wiki/Loop_\(statement\) "View the content page [c]")
- [Talk](https://en.wikipedia.org/wiki/Talk:Loop_\(statement\) "Discuss improvements to the content page [t]")
English
- [Read](https://en.wikipedia.org/wiki/Loop_\(statement\))
- [Edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit "Edit this page [e]")
- [View history](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=history "Past revisions of this page [h]")
Tools
Tools
move to sidebar
hide
Actions
- [Read](https://en.wikipedia.org/wiki/Loop_\(statement\))
- [Edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit "Edit this page [e]")
- [View history](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=history)
General
- [What links here](https://en.wikipedia.org/wiki/Special:WhatLinksHere/Loop_\(statement\) "List of all English Wikipedia pages containing links to this page [j]")
- [Related changes](https://en.wikipedia.org/wiki/Special:RecentChangesLinked/Loop_\(statement\) "Recent changes in pages linked from this page [k]")
- [Upload file](https://en.wikipedia.org/wiki/Wikipedia:File_Upload_Wizard "Upload files [u]")
- [Permanent link](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&oldid=1343948330 "Permanent link to this revision of this page")
- [Page information](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=info "More information about this page")
- [Cite this page](https://en.wikipedia.org/w/index.php?title=Special:CiteThisPage&page=Loop_%28statement%29&id=1343948330&wpFormIdentifier=titleform "Information on how to cite this page")
- [Get shortened URL](https://en.wikipedia.org/w/index.php?title=Special:UrlShortener&url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FLoop_%28statement%29%23Pre-test_loop)
Print/export
- [Download as PDF](https://en.wikipedia.org/w/index.php?title=Special:DownloadAsPdf&page=Loop_%28statement%29&action=show-download-screen "Download this page as a PDF file")
- [Printable version](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&printable=yes "Printable version of this page [p]")
In other projects
- [Wikimedia Commons](https://commons.wikimedia.org/wiki/Category:Programming_loops)
- [Wikidata item](https://www.wikidata.org/wiki/Special:EntityPage/Q8868615 "Structured data on this page hosted by Wikidata [g]")
Appearance
move to sidebar
hide
From Wikipedia, the free encyclopedia
(Redirected from [While loop](https://en.wikipedia.org/w/index.php?title=While_loop&redirect=no "While loop"))
Control flow statement for executing code repeatedly
| | |
|---|---|
| [](https://en.wikipedia.org/wiki/File:Question_book-new.svg) | This article **needs additional citations for [verification](https://en.wikipedia.org/wiki/Wikipedia:Verifiability "Wikipedia:Verifiability")**. Please help [improve this article](https://en.wikipedia.org/wiki/Special:EditPage/Loop_\(statement\) "Special:EditPage/Loop (statement)") by [adding citations to reliable sources](https://en.wikipedia.org/wiki/Help:Referencing_for_beginners "Help:Referencing for beginners"). Unsourced material may be challenged and removed. *Find sources:* ["Loop" statement](https://www.google.com/search?as_eq=wikipedia&q=%22Loop%22+statement) – [news](https://www.google.com/search?tbm=nws&q=%22Loop%22+statement+-wikipedia&tbs=ar:1) **·** [newspapers](https://www.google.com/search?&q=%22Loop%22+statement&tbs=bkt:s&tbm=bks) **·** [books](https://www.google.com/search?tbs=bks:1&q=%22Loop%22+statement+-wikipedia) **·** [scholar](https://scholar.google.com/scholar?q=%22Loop%22+statement) **·** [JSTOR](https://www.jstor.org/action/doBasicSearch?Query=%22Loop%22+statement&acc=on&wc=on) *(January 2026)* *([Learn how and when to remove this message](https://en.wikipedia.org/wiki/Help:Maintenance_template_removal "Help:Maintenance template removal"))* |
In [computer programming](https://en.wikipedia.org/wiki/Computer_programming "Computer programming"), a **loop** is a [control flow](https://en.wikipedia.org/wiki/Control_flow "Control flow") [statement](https://en.wikipedia.org/wiki/Statement_\(computer_science\) "Statement (computer science)") that allows code to be executed repeatedly, usually with minor alterations between repetitions. Loops can be used to perform a repeated action on all items in a [collection](https://en.wikipedia.org/wiki/Collection_\(abstract_data_type\) "Collection (abstract data type)"), or to implement a long lived program.
## Overview
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=1 "Edit section: Overview")\]
Loops are a feature of [high-level programming languages](https://en.wikipedia.org/wiki/High-level_programming_languages "High-level programming languages"). In [low-level programming languages](https://en.wikipedia.org/wiki/Low-level_programming_languages "Low-level programming languages") the same functionality is achieved using [jumps](https://en.wikipedia.org/wiki/Control_flow#Jump "Control flow"). When a program is [compiled](https://en.wikipedia.org/wiki/Compiler "Compiler") to [machine code](https://en.wikipedia.org/wiki/Machine_code "Machine code"), looping may be achieved using jumps; but some loops can be [optimized](https://en.wikipedia.org/wiki/Code_optimization "Code optimization") to run without jumping.[\[1\]](https://en.wikipedia.org/wiki/While_loop#cite_note-1)
Usually, loops are expected to run for a finite number of iteration.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] Without proper care, loops may accidentally be created that have no possibility of terminating. Such loops are called [infinite loops](https://en.wikipedia.org/wiki/Infinite_loop "Infinite loop"). The problem of determining whether a program contains an infinite loop is known as the [halting problem](https://en.wikipedia.org/wiki/Halting_problem "Halting problem").
## Conditional loop
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=2 "Edit section: Conditional loop")\]
A **conditional loop** (also known as an **indeterminate loop**[\[2\]](https://en.wikipedia.org/wiki/While_loop#cite_note-joy-2)) is a loop that determines whether to terminate based on a logical condition.[\[3\]](https://en.wikipedia.org/wiki/While_loop#cite_note-bbc1-3) These loops are flexible, but there exact behavior can be difficult to reason about.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 368
A conditional loop is usually composed of two parts: a *condition* and a *body*. The *condition* is a logical statement depending on the state of the program and the *body* is a block of code that runs as long as the *condition* holds.[\[3\]](https://en.wikipedia.org/wiki/While_loop#cite_note-bbc1-3)
A common misconception is that the execution of the *body* terminates as soon as the *condition* does not hold anymore; but this is usually not the case.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 368 In most programming languages, the *condition* is checked once for every execution of the *body*. When the *condition* is checked is not standardized and some programming languages contain multiple conditional looping structures with different rules about when the *condition* is assessed.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
### Pre-test loop
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=3 "Edit section: Pre-test loop")\]
A **pre-test loop** is a conditional loop where the *condition* is checked before the *body* is executed. More precisely, the *condition* is checked and if it holds the *body* is execute. Afterwards, the *condition* is checked again, and if it holds the *body* is executed again. This process repeats until the *condition* does not hold. Many programming languages call this loop a *while loop* and refer to it with the [keyword](https://en.wikipedia.org/wiki/Reserved_word "Reserved word") *while*. They are commonly formatted in manner similar to
```
while condition do
body
repeat
```
Instead of the keywords *do* and *repeat* others methods are sometime use to indicate where the *body* begins and ends, such as [curly braces](https://en.wikipedia.org/wiki/Curly_braces "Curly braces")[\[5\]](https://en.wikipedia.org/wiki/While_loop#cite_note-5) or [whitespace](https://en.wikipedia.org/wiki/Whitespace_characters "Whitespace characters").[\[6\]](https://en.wikipedia.org/wiki/While_loop#cite_note-6)
For example, the following code fragment first checks whether *x* is less than five, which it is, so the *body* is entered. There, *x* is displayed and then incremented by one. After executing the statements in the *body*, the *condition* is checked again, and the loop is executed again. This process repeats until *x* has the value five.
```
x ← 0
while x < 5 do
display(x)
x ← x + 1
repeat
```
### Post-test loop
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=4 "Edit section: Post-test loop")\]
[](https://en.wikipedia.org/wiki/File:Do-while-loop-diagram.svg)
Do-While loop flow diagram
A **post-test loop** is a conditional loop where the *condition* is checked after the *body* is executed. More precisely, the *body* is executed and afterwards the *condition* is checked. If it holds the *body* is run again and then the *condition* is checked. This is repeated until the *condition* does not hold. This is sometimes called a *do-while loop* due to the syntax used in various programming languages,[\[7\]](https://en.wikipedia.org/wiki/While_loop#cite_note-7) although this can be confusing since [Fortran](https://en.wikipedia.org/wiki/Fortran "Fortran") and [PL/I](https://en.wikipedia.org/wiki/PL/I "PL/I") use the syntax "DO WHILE" for pre-test loops.[\[8\]](https://en.wikipedia.org/wiki/While_loop#cite_note-8)[\[9\]](https://en.wikipedia.org/wiki/While_loop#cite_note-9) Post-test loops are commonly formatted in manner similar to
```
do
body
repeat while condition
```
Instead of the keywords *do* and *repeat* others methods are sometime use to indicate where the *body* begins and ends, such as curly braces.[\[10\]](https://en.wikipedia.org/wiki/While_loop#cite_note-10)
Some [languages](https://en.wikipedia.org/wiki/Programming_language "Programming language") may use a different naming convention for this type of loop. For example, the [Pascal](https://en.wikipedia.org/wiki/Pascal_programming_language "Pascal programming language") and [Lua](https://en.wikipedia.org/wiki/Lua_\(programming_language\) "Lua (programming language)") languages have a "*repeat until*" loop, which continues to run *until* the control expression is true and then terminates.[\[11\]](https://en.wikipedia.org/wiki/While_loop#cite_note-11)[\[12\]](https://en.wikipedia.org/wiki/While_loop#cite_note-12)
### Three-part for loop
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=5 "Edit section: Three-part for loop")\]
[](https://en.wikipedia.org/wiki/File:For_loop_example.svg)
Flow diagram of a for loop that prints five asterisks.
A **three-part for loop**, popularized by C, has two additional parts: [*initialization*](https://en.wikipedia.org/wiki/Declaration_\(computer_programming\) "Declaration (computer programming)") ([loop variant](https://en.wikipedia.org/wiki/Loop_variant "Loop variant")), and *increment*, both of which are blocks of code. The *initialization* is intended as code that prepares the loop and is run once in the beginning and *increment* is used to update the state of the program after each iteration of the loop. Otherwise, the three-part for loop is a pre-test loop. They are commonly formatted in manner similar to
```
for initialization, condition, increment do
body
repeat
```
This syntax came from [B](https://en.wikipedia.org/wiki/B_\(programming_language\) "B (programming language)") and was originally invented by [Stephen C. Johnson](https://en.wikipedia.org/wiki/Stephen_C._Johnson "Stephen C. Johnson").[\[13\]](https://en.wikipedia.org/wiki/While_loop#cite_note-ken-13) The following C code is an example of a three part loop that prints the numbers from 0 to 4.
```
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
```
### Equivalent constructs
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=6 "Edit section: Equivalent constructs")\]
| | |
|---|---|
| [![\[icon\]](https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Wiki_letter_w_cropped.svg/20px-Wiki_letter_w_cropped.svg.png)](https://en.wikipedia.org/wiki/File:Wiki_letter_w_cropped.svg) | This section **needs expansion**. You can help by [adding missing information](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=). *(January 2026)* |
Assuming there is a function called *do\_work()* that does some work, the following are equivalent.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
As long as the *continue* statement is not used, the above is technically equivalent to the following (though these examples are not typical or modern style used in everyday computers):
```
while true do
do_work()
if condition is not true then
break
end if
repeat
```
or
```
LOOPSTART:
do_work()
if condition then
goto LOOPSTART
end if
```
## Enumeration
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=7 "Edit section: Enumeration")\]
[](https://en.wikipedia.org/wiki/File:For-Loop-Mint-Programming-Language-Type-2.gif)
`foreach` loops are almost always used to iterate over items in a sequence of elements.
An **enumeration** (also known as an **determinate loop**[\[2\]](https://en.wikipedia.org/wiki/While_loop#cite_note-joy-2)) is a loop intended to iterate over all the items of a collection.[\[14\]](https://en.wikipedia.org/wiki/While_loop#cite_note-14) It is not as flexible as a conditional loop; but it is more predictable.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] For example, it is easier to guarantee that enumerations [terminate](https://en.wikipedia.org/wiki/Terminate "Terminate") and they avoid potential [off-by-one errors](https://en.wikipedia.org/wiki/Off-by-one_error "Off-by-one error").\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] Enumerations can be implemented using an [iterator](https://en.wikipedia.org/wiki/Iterator "Iterator"), whether implicitly or explicitly. They are commonly formatted in manner similar to
```
for item in collection
body
repeat
```
Depending on the programming language, various keywords are used to invoke enumerations. For example, descendants of [ALGOL](https://en.wikipedia.org/wiki/ALGOL "ALGOL") use `for`,[\[15\]](https://en.wikipedia.org/wiki/While_loop#cite_note-15) while descendants of [Fortran](https://en.wikipedia.org/wiki/Fortran "Fortran") use `do`[\[16\]](https://en.wikipedia.org/wiki/While_loop#cite_note-16) and [COBOL](https://en.wikipedia.org/wiki/COBOL "COBOL") uses `PERFORM VARYING`.[\[17\]](https://en.wikipedia.org/wiki/While_loop#cite_note-17)
Enumerations are sometimes called "for loops," for example in [Zig](https://en.wikipedia.org/wiki/Zig_\(programming_language\) "Zig (programming language)") and [Rust](https://en.wikipedia.org/wiki/Rust_\(programming_language\) "Rust (programming language)").[\[18\]](https://en.wikipedia.org/wiki/While_loop#cite_note-18)[\[19\]](https://en.wikipedia.org/wiki/While_loop#cite_note-19) This can be confusing since many of the most popular[\[20\]](https://en.wikipedia.org/wiki/While_loop#cite_note-20) programming languages, such as C, [C++](https://en.wikipedia.org/wiki/C%2B%2B "C++"), and [Java](https://en.wikipedia.org/wiki/Java_\(programming_language\) "Java (programming language)"), use that term for the three-part for loop,[\[21\]](https://en.wikipedia.org/wiki/While_loop#cite_note-21)[\[22\]](https://en.wikipedia.org/wiki/While_loop#cite_note-22)[\[23\]](https://en.wikipedia.org/wiki/While_loop#cite_note-23) which is not an enumeration. Other programming languages, such as [Perl](https://en.wikipedia.org/wiki/Perl "Perl") and [C\#](https://en.wikipedia.org/wiki/C "C"), avoid this confusion by using the term "foreach loop."[\[24\]](https://en.wikipedia.org/wiki/While_loop#cite_note-24)[\[25\]](https://en.wikipedia.org/wiki/While_loop#cite_note-25)
The order in which the items in the collection are iterated through depends on the programming language. [Fortran 95](https://en.wikipedia.org/wiki/Fortran_95 "Fortran 95") has a loop, invoked using the keyword `FORALL`, that is independent of this order. It has the effect of executing each iteration of the loop at the same time. This feature was made obsolescent in [Fortran 2018](https://en.wikipedia.org/wiki/Fortran_2018 "Fortran 2018").[\[26\]](https://en.wikipedia.org/wiki/While_loop#cite_note-26)
## Loops in functional programming
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=8 "Edit section: Loops in functional programming")\]
| | |
|---|---|
| [![\[icon\]](https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Wiki_letter_w_cropped.svg/20px-Wiki_letter_w_cropped.svg.png)](https://en.wikipedia.org/wiki/File:Wiki_letter_w_cropped.svg) | This section **needs expansion**. You can help by [adding missing information](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=). *(January 2026)* |
In most [functional programming languages](https://en.wikipedia.org/wiki/Functional_programming "Functional programming"), [recursion](https://en.wikipedia.org/wiki/Recursion_\(computer_science\) "Recursion (computer science)") is used instead of traditional loops. This is due to the fact that variables are [immutable](https://en.wikipedia.org/wiki/Immutable_object "Immutable object"), and therefore the *increment* step of a loop cannot occur.[\[27\]](https://en.wikipedia.org/wiki/While_loop#cite_note-27)
To avoid running into [stack overflow](https://en.wikipedia.org/wiki/Stack_overflow "Stack overflow") errors for long loops, functional programming languages implement [tail call optimisation](https://en.wikipedia.org/wiki/Tail_call "Tail call"), which allows the same [stack frame](https://en.wikipedia.org/wiki/Call_stack "Call stack") to be used for each iteration of the loop, compiling to effectively the same code as a *while* or *for* loop.[\[28\]](https://en.wikipedia.org/wiki/While_loop#cite_note-28)
Some languages, such as [Haskell](https://en.wikipedia.org/wiki/Haskell "Haskell"), have a special syntax known as a [list comprehension](https://en.wikipedia.org/wiki/List_comprehension "List comprehension"), which is similar to enumeration, iterating over the contents of a list and transforming it into a new list.
## Loop counter
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=9 "Edit section: Loop counter")\]
A **loop counter** is a control variable that controls the iterations of a loop. Loop counters change with each iteration of a loop, providing a unique value for each iteration. The loop counter is used to decide when the loop should terminate. It is so named because most uses of this construct result in the variable taking on a range of integer values.
A common [identifier naming convention](https://en.wikipedia.org/wiki/Identifier_naming_convention "Identifier naming convention") is for the loop counter to use the variable names *i*, *j*, and *k* (and so on if needed),[\[29\]](https://en.wikipedia.org/wiki/While_loop#cite_note-29) where *i* would be the most outer loop, *j* the next inner loop, etc. This style is generally agreed to have originated from the early programming of Fortran\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\], where these variable names beginning with these letters were implicitly declared as having an integer type, and so were obvious choices for loop counters that were only temporarily required. The practice dates back further to [mathematical notation](https://en.wikipedia.org/wiki/Mathematical_notation "Mathematical notation") where [indices](https://en.wikipedia.org/wiki/Index_notation "Index notation") for [sums](https://en.wikipedia.org/wiki/Summation "Summation") and [multiplications](https://en.wikipedia.org/wiki/Multiplication "Multiplication") are often *i*, *j*, and *k*.
Using terse names for loop counters, like *i* and *j*, is discouraged by some since the purpose of the variables is not as clear as if they were given a longer more descriptive name.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 383–382
Different languages specify different rules for what value the loop counter will hold on termination of its loop, and indeed some hold that it becomes undefined. This permits a [compiler](https://en.wikipedia.org/wiki/Compiler "Compiler") to generate code that leaves any value in the loop counter, or perhaps even leaves it unchanged because the loop value was held in a register and never stored in memory. Actual behavior may even vary according to the compiler's optimization settings.
Modifying the loop counter within the body of the loop can lead to unexpected consequences. To prevent such problems, some languages make the loop counter [immutable](https://en.wikipedia.org/wiki/Immutable_object "Immutable object").\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] However, only overt changes are likely to be detected by the compiler. Situations where the address of the loop counter is passed as an argument to a [subroutine](https://en.wikipedia.org/wiki/Subroutine "Subroutine"), make it very difficult to check because the routine's behavior is in general unknowable to the compiler unless the language supports procedure signatures and argument intents.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
## Early exit and continuation
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=10 "Edit section: Early exit and continuation")\]
Some languages may also provide supporting statements for altering how a loop's iteration proceeds. Common among these are the [*break*](https://en.wikipedia.org/wiki/Break_statement "Break statement") statement, which terminates the current loop the program is in, and the [*continue*](https://en.wikipedia.org/wiki/Control_flow#Continuation_with_next_iteration "Control flow") statement, which skips to the next iteration of the current loop.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 379 These statements may have other names; For example in [Fortran 90](https://en.wikipedia.org/wiki/Fortran_90 "Fortran 90"), they are called *exit* and *cycle*.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
A loop can also be terminated by [returning](https://en.wikipedia.org/wiki/Return_statement "Return statement") from the function within which it is being executed.
In the case of nested loops, the *break* and *continue* statements apply to the inner most loop. Some languages allow loops to be labelled. These statements can then be applied to any of the loops in which the program is nested.
```
outer_loop: (This is a label for the outermost loop)
for 1 ≤ i ≤ 2 do
for 1 ≤ j ≤ 2 do
display(i, j)
if i = 2
continue outer_loop
end if
repeat
repeat
(This nested loop displays the pairs (1, 1), (1, 2), and (2, 1))
```
## Infinite loop
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=11 "Edit section: Infinite loop")\]
Main article: [Infinite loop](https://en.wikipedia.org/wiki/Infinite_loop "Infinite loop")
| | |
|---|---|
| [![\[icon\]](https://upload.wikimedia.org/wikipedia/commons/thumb/1/1c/Wiki_letter_w_cropped.svg/20px-Wiki_letter_w_cropped.svg.png)](https://en.wikipedia.org/wiki/File:Wiki_letter_w_cropped.svg) | This section **needs expansion**. You can help by [adding missing information](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=). *(January 2026)* |
An infinite loop is a loop which never terminates. This can be intentional, or the result of a [logic error](https://en.wikipedia.org/wiki/Logic_error "Logic error").
Systematically detecting infinite loops is known as the [halting problem](https://en.wikipedia.org/wiki/Halting_problem "Halting problem").[\[30\]](https://en.wikipedia.org/wiki/While_loop#cite_note-30)
Infinite loops are useful in applications which need to perform a repeated calculation until a program terminates, such as [web servers](https://en.wikipedia.org/wiki/Web_servers "Web servers").[\[31\]](https://en.wikipedia.org/wiki/While_loop#cite_note-31)
## See also
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=12 "Edit section: See also")\]
- [Primitive recursive function](https://en.wikipedia.org/wiki/Primitive_recursive_function "Primitive recursive function")
- [General recursive function](https://en.wikipedia.org/wiki/General_recursive_function "General recursive function")
- [Repeat loop (disambiguation)](https://en.wikipedia.org/wiki/Repeat_loop_\(disambiguation\) "Repeat loop (disambiguation)")
- [LOOP (programming language)](https://en.wikipedia.org/wiki/LOOP_\(programming_language\) "LOOP (programming language)") – a programming language with the property that the functions it can compute are exactly the primitive recursive functions
## References
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=13 "Edit section: References")\]
1. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-1)**
Angelou, Alexandros; Dadaliaris, Antonios; Dossis, Michael; Dimitriou, Georgios (2021-11-26). ["Branchless Code Generation for Modern Processor Architectures"](https://doi.org/10.1145/3503823.3503879). *25th Pan-Hellenic Conference on Informatics*. New York, NY, USA: ACM: 300–305\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1145/3503823.3503879](https://doi.org/10.1145%2F3503823.3503879). [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-1-4503-9555-7](https://en.wikipedia.org/wiki/Special:BookSources/978-1-4503-9555-7 "Special:BookSources/978-1-4503-9555-7")
.
2. ^ [***a***](https://en.wikipedia.org/wiki/While_loop#cite_ref-joy_2-0) [***b***](https://en.wikipedia.org/wiki/While_loop#cite_ref-joy_2-1)
Samanta, Debasis; Sarma, Monalisa (15 June 2023). [*Joy with Java: Fundamentals of Object Oriented Programming*](https://books.google.com/books?id=34GyEAAAQBAJ). Cambridge University Press. p. 124. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-1-009-21190-1](https://en.wikipedia.org/wiki/Special:BookSources/978-1-009-21190-1 "Special:BookSources/978-1-009-21190-1")
.
3. ^ [***a***](https://en.wikipedia.org/wiki/While_loop#cite_ref-bbc1_3-0) [***b***](https://en.wikipedia.org/wiki/While_loop#cite_ref-bbc1_3-1)
["Conditional loops - Computational constructs - National 4 Computing Science Revision"](https://web.archive.org/web/20211019111021/https://www.bbc.co.uk/bitesize/guides/zcg9kqt/revision/8). *BBC Bitesize*. Archived from [the original](https://www.bbc.co.uk/bitesize/guides/zcg9kqt/revision/8) on 19 October 2021. Retrieved 8 January 2026.
4. ^ [***a***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-0) [***b***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-1) [***c***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-2) [***d***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-3)
McConnell, Steve (9 June 2004). [*Code Complete*](https://books.google.com/books?id=LpVCAwAAQBAJ). Pearson Education. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-0-7356-3697-2](https://en.wikipedia.org/wiki/Special:BookSources/978-0-7356-3697-2 "Special:BookSources/978-0-7356-3697-2")
.
5. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-5)**
["while Statement (GNU C Language Manual)"](https://web.archive.org/web/20240712235744/https://www.gnu.org/software/c-intro-and-ref/manual/html_node/while-Statement.html). *www.gnu.org*. Archived from [the original](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/while-Statement.html) on 12 July 2024. Retrieved 8 January 2026.
6. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-6)**
["3. An Informal Introduction to Python"](https://web.archive.org/web/20251231204216/https://docs.python.org/3/tutorial/introduction.html). *Python documentation*. Archived from [the original](https://docs.python.org/3/tutorial/introduction.html) on 31 December 2025. Retrieved 8 January 2026.
7. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-7)**
["do...while - JavaScript \| MDN"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while). *MDN Web Docs*. 2025-07-08. Retrieved 2026-01-22.
8. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-8)**
["DO WHILE (FORTRAN 77 Language Reference)"](https://web.archive.org/web/20230314015649/https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn8e/index.html). *docs.oracle.com*. Archived from [the original](https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn8e/index.html) on 14 March 2023. Retrieved 8 January 2026.
9. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-9)**
["DO command (PL/I)"](https://web.archive.org/web/20250302164825/https://www.ibm.com/docs/en/wdfrhcw/1.4.0?topic=commands-do-command-pli). *www.ibm.com*. Archived from [the original](https://www.ibm.com/docs/en/wdfrhcw/1.4.0?topic=commands-do-command-pli) on 2 March 2025. Retrieved 22 January 2026.
10. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-10)**
["do-while Statement (GNU C Language Manual)"](https://web.archive.org/web/20240713012759/https://www.gnu.org/software/c-intro-and-ref/manual/html_node/do_002dwhile-Statement.html). *www.gnu.org*. Archived from [the original](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/do_002dwhile-Statement.html) on 13 July 2024. Retrieved 8 January 2026.
11. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-11)**
["The Repeat..until statement"](https://web.archive.org/web/20251109225700/https://www.freepascal.org/docs-html/ref/refsu60.html). *www.freepascal.org*. Archived from [the original](https://www.freepascal.org/docs-html/ref/refsu60.html) on 9 November 2025. Retrieved 22 January 2026.
12. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-12)**
["Programming in Lua : 4.3.3"](https://web.archive.org/web/20260102100039/https://www.lua.org/pil/4.3.3.html). *www.lua.org*. Archived from [the original](https://www.lua.org/pil/4.3.3.html) on 2 January 2026. Retrieved 22 January 2026.
13. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-ken_13-0)**
[Thompson, Ken](https://en.wikipedia.org/wiki/Ken_Thompson "Ken Thompson"). [*VCF East 2019 – Brian Kernighan interviews Ken Thompson*](https://www.youtube.com/watch?v=EY6q5dv_B-o&t=2330). *[YouTube](https://en.wikipedia.org/wiki/YouTube "YouTube")*. [Archived](https://ghostarchive.org/varchive/youtube/20211212/EY6q5dv_B-o) from the original on 2021-12-12. Retrieved 2020-11-16. "I saw Johnson's semicolon version of the for loop and I put that in \[B\], I stole it."
14. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-14)**
McConnell, Steve (9 June 2004). [*Code Complete*](https://books.google.com/books?id=LpVCAwAAQBAJ&q=code+complete). Pearson Education. p. 367. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-0-7356-3697-2](https://en.wikipedia.org/wiki/Special:BookSources/978-0-7356-3697-2 "Special:BookSources/978-0-7356-3697-2")
.
15. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-15)**
[Wirth, Niklaus](https://en.wikipedia.org/wiki/Niklaus_Wirth "Niklaus Wirth") (1973). "Preface". *Systematic Programming: An Introduction*. Prentice-Hall. pp. xiii. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[0138803692](https://en.wikipedia.org/wiki/Special:BookSources/0138803692 "Special:BookSources/0138803692")
.
16. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-16)**
["DO / END DO"](https://web.archive.org/web/20260108231419/https://www.ibm.com/docs/en/xl-fortran-linux/16.1.0?topic=directives-do-end-do). *www.ibm.com*. 24 April 2018. Archived from [the original](https://www.ibm.com/docs/en/xl-fortran-linux/16.1.0?topic=directives-do-end-do) on 8 January 2026. Retrieved 8 January 2026.
17. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-17)**
["PERFORM with VARYING Phrase"](https://web.archive.org/web/20260108231758/https://www.ibm.com/docs/en/i/7.5.0?topic=statement-perform-varying-phrase). *www.ibm.com*. June 2012. Archived from [the original](https://www.ibm.com/docs/en/i/7.5.0?topic=statement-perform-varying-phrase) on 8 January 2026. Retrieved 8 January 2026.
18. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-18)**
["Zig documentation"](https://web.archive.org/web/20260104121319/https://ziglang.org/documentation/master/#for). *ziglang.org*. Archived from [the original](https://ziglang.org/documentation/master/#for) on 4 January 2026. Retrieved 8 January 2026.
19. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-19)**
["Looping Through a Collection with for"](https://web.archive.org/web/20251219092527/https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html#looping-through-a-collection-with-for). *rust-lang.org*. Archived from [the original](https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html#looping-through-a-collection-with-for) on 19 December 2025. Retrieved 8 January 2026.
20. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-20)**
["TIOBE Index for September 2024"](https://web.archive.org/web/20260104151245/https://www.tiobe.com/tiobe-index/). Archived from [the original](https://www.tiobe.com/tiobe-index/) on January 4, 2026. Retrieved 2025-12-16.
21. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-21)**
["for Statement (GNU C Language Manual)"](https://web.archive.org/web/20240713014231/https://www.gnu.org/software/c-intro-and-ref/manual/html_node/for-Statement.html). *www.gnu.org*. Archived from [the original](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/for-Statement.html) on 13 July 2024. Retrieved 8 January 2026.
22. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-22)**
["for statement (C++)"](https://web.archive.org/web/20250928185930/https://learn.microsoft.com/en-us/cpp/cpp/for-statement-cpp?view=msvc-170). *learn.microsoft.com*. Archived from [the original](https://learn.microsoft.com/en-us/cpp/cpp/for-statement-cpp?view=msvc-170) on 28 September 2025. Retrieved 8 January 2026.
23. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-23)**
["The for Statement (The Java™ Tutorials \> Learning the Java Language \> Language Basics)"](https://web.archive.org/web/20251228051928/https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). *docs.oracle.com*. Archived from [the original](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) on 28 December 2025. Retrieved 8 January 2026.
24. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-24)**
["Iteration statements -for, foreach, do, and while - C\# reference"](https://web.archive.org/web/20251228021319/https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements). *learn.microsoft.com*. Archived from [the original](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements) on 28 December 2025. Retrieved 8 January 2026.
25. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-25)**
["Perl for Loop"](https://web.archive.org/web/20250607160051/https://www.perltutorial.org/perl-for-loop/). *Perl Tutorial*. Archived from [the original](https://www.perltutorial.org/perl-for-loop/) on 7 June 2025. Retrieved 8 January 2026.
26. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-26)**
["FORALL"](https://web.archive.org/web/20260101223642/https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-2/forall.html). *Intel*. Archived from [the original](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-2/forall.html) on 1 January 2026. Retrieved 8 January 2026.
27. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-27)**
Hinsen, Konrad (2009). ["The Promises of Functional Programming"](https://doi.org/10.1109/mcse.2009.129). *Computing in Science & Engineering*. **11** (4): 86–90\. [Bibcode](https://en.wikipedia.org/wiki/Bibcode_\(identifier\) "Bibcode (identifier)"):[2009CSE....11d..86H](https://ui.adsabs.harvard.edu/abs/2009CSE....11d..86H). [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1109/mcse.2009.129](https://doi.org/10.1109%2Fmcse.2009.129). [ISSN](https://en.wikipedia.org/wiki/ISSN_\(identifier\) "ISSN (identifier)") [1521-9615](https://search.worldcat.org/issn/1521-9615).
28. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-28)**
Clinger, William D. (1998). "Proper tail recursion and space efficiency". *Proceedings of the ACM SIGPLAN 1998 conference on Programming language design and implementation*. New York, NY, USA: ACM. pp. 174–185\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1145/277650.277719](https://doi.org/10.1145%2F277650.277719). [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[0-89791-987-4](https://en.wikipedia.org/wiki/Special:BookSources/0-89791-987-4 "Special:BookSources/0-89791-987-4")
.
29. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-29)** <http://www.knosof.co.uk/vulnerabilities/loopcntrl.pdf> Analysis of loop control variables in C
30. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-30)**
Lee, Sarah. ["Turing's Halting Problem Explained"](https://www.numberanalytics.com/blog/ultimate-guide-turing-halting-problem). *www.numberanalytics.com*. Retrieved 2026-01-22.
31. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-31)**
["What is an infinite loop (endless loop)?"](https://www.techtarget.com/whatis/definition/infinite-loop-endless-loop). *WhatIs*. Retrieved 2026-01-22.

Retrieved from "<https://en.wikipedia.org/w/index.php?title=Loop_(statement)&oldid=1343948330#Pre-test_loop>"
[Category](https://en.wikipedia.org/wiki/Help:Category "Help:Category"):
- [Control flow](https://en.wikipedia.org/wiki/Category:Control_flow "Category:Control flow")
Hidden categories:
- [Articles with short description](https://en.wikipedia.org/wiki/Category:Articles_with_short_description "Category:Articles with short description")
- [Short description is different from Wikidata](https://en.wikipedia.org/wiki/Category:Short_description_is_different_from_Wikidata "Category:Short description is different from Wikidata")
- [Articles needing additional references from January 2026](https://en.wikipedia.org/wiki/Category:Articles_needing_additional_references_from_January_2026 "Category:Articles needing additional references from January 2026")
- [All articles needing additional references](https://en.wikipedia.org/wiki/Category:All_articles_needing_additional_references "Category:All articles needing additional references")
- [All articles with unsourced statements](https://en.wikipedia.org/wiki/Category:All_articles_with_unsourced_statements "Category:All articles with unsourced statements")
- [Articles with unsourced statements from December 2025](https://en.wikipedia.org/wiki/Category:Articles_with_unsourced_statements_from_December_2025 "Category:Articles with unsourced statements from December 2025")
- [Articles with unsourced statements from January 2026](https://en.wikipedia.org/wiki/Category:Articles_with_unsourced_statements_from_January_2026 "Category:Articles with unsourced statements from January 2026")
- [Articles to be expanded from January 2026](https://en.wikipedia.org/wiki/Category:Articles_to_be_expanded_from_January_2026 "Category:Articles to be expanded from January 2026")
- [All articles to be expanded](https://en.wikipedia.org/wiki/Category:All_articles_to_be_expanded "Category:All articles to be expanded")
- [Articles with unsourced statements from August 2009](https://en.wikipedia.org/wiki/Category:Articles_with_unsourced_statements_from_August_2009 "Category:Articles with unsourced statements from August 2009")
- This page was last edited on 17 March 2026, at 10:34 (UTC).
- Text is available under the [Creative Commons Attribution-ShareAlike 4.0 License](https://en.wikipedia.org/wiki/Wikipedia:Text_of_the_Creative_Commons_Attribution-ShareAlike_4.0_International_License "Wikipedia:Text of the Creative Commons Attribution-ShareAlike 4.0 International License"); additional terms may apply. By using this site, you agree to the [Terms of Use](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Terms_of_Use "foundation:Special:MyLanguage/Policy:Terms of Use") and [Privacy Policy](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy "foundation:Special:MyLanguage/Policy:Privacy policy"). Wikipedia® is a registered trademark of the [Wikimedia Foundation, Inc.](https://wikimediafoundation.org/), a non-profit organization.
- [Privacy policy](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Privacy_policy)
- [About Wikipedia](https://en.wikipedia.org/wiki/Wikipedia:About)
- [Disclaimers](https://en.wikipedia.org/wiki/Wikipedia:General_disclaimer)
- [Contact Wikipedia](https://en.wikipedia.org/wiki/Wikipedia:Contact_us)
- [Legal & safety contacts](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Legal:Wikimedia_Foundation_Legal_and_Safety_Contact_Information)
- [Code of Conduct](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Universal_Code_of_Conduct)
- [Developers](https://developer.wikimedia.org/)
- [Statistics](https://stats.wikimedia.org/#/en.wikipedia.org)
- [Cookie statement](https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Cookie_statement)
- [Mobile view](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&mobileaction=toggle_view_mobile#Pre-test_loop)
- [](https://www.wikimedia.org/)
- [](https://www.mediawiki.org/)
Search
Toggle the table of contents
Loop (statement)
25 languages
[Add topic](https://en.wikipedia.org/wiki/While_loop) |
| Readable Markdown | From Wikipedia, the free encyclopedia
In [computer programming](https://en.wikipedia.org/wiki/Computer_programming "Computer programming"), a **loop** is a [control flow](https://en.wikipedia.org/wiki/Control_flow "Control flow") [statement](https://en.wikipedia.org/wiki/Statement_\(computer_science\) "Statement (computer science)") that allows code to be executed repeatedly, usually with minor alterations between repetitions. Loops can be used to perform a repeated action on all items in a [collection](https://en.wikipedia.org/wiki/Collection_\(abstract_data_type\) "Collection (abstract data type)"), or to implement a long lived program.
Loops are a feature of [high-level programming languages](https://en.wikipedia.org/wiki/High-level_programming_languages "High-level programming languages"). In [low-level programming languages](https://en.wikipedia.org/wiki/Low-level_programming_languages "Low-level programming languages") the same functionality is achieved using [jumps](https://en.wikipedia.org/wiki/Control_flow#Jump "Control flow"). When a program is [compiled](https://en.wikipedia.org/wiki/Compiler "Compiler") to [machine code](https://en.wikipedia.org/wiki/Machine_code "Machine code"), looping may be achieved using jumps; but some loops can be [optimized](https://en.wikipedia.org/wiki/Code_optimization "Code optimization") to run without jumping.[\[1\]](https://en.wikipedia.org/wiki/While_loop#cite_note-1)
Usually, loops are expected to run for a finite number of iteration.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] Without proper care, loops may accidentally be created that have no possibility of terminating. Such loops are called [infinite loops](https://en.wikipedia.org/wiki/Infinite_loop "Infinite loop"). The problem of determining whether a program contains an infinite loop is known as the [halting problem](https://en.wikipedia.org/wiki/Halting_problem "Halting problem").
A **conditional loop** (also known as an **indeterminate loop**[\[2\]](https://en.wikipedia.org/wiki/While_loop#cite_note-joy-2)) is a loop that determines whether to terminate based on a logical condition.[\[3\]](https://en.wikipedia.org/wiki/While_loop#cite_note-bbc1-3) These loops are flexible, but there exact behavior can be difficult to reason about.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 368
A conditional loop is usually composed of two parts: a *condition* and a *body*. The *condition* is a logical statement depending on the state of the program and the *body* is a block of code that runs as long as the *condition* holds.[\[3\]](https://en.wikipedia.org/wiki/While_loop#cite_note-bbc1-3)
A common misconception is that the execution of the *body* terminates as soon as the *condition* does not hold anymore; but this is usually not the case.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 368 In most programming languages, the *condition* is checked once for every execution of the *body*. When the *condition* is checked is not standardized and some programming languages contain multiple conditional looping structures with different rules about when the *condition* is assessed.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
A **pre-test loop** is a conditional loop where the *condition* is checked before the *body* is executed. More precisely, the *condition* is checked and if it holds the *body* is execute. Afterwards, the *condition* is checked again, and if it holds the *body* is executed again. This process repeats until the *condition* does not hold. Many programming languages call this loop a *while loop* and refer to it with the [keyword](https://en.wikipedia.org/wiki/Reserved_word "Reserved word") *while*. They are commonly formatted in manner similar to
```
while condition do
body
repeat
```
Instead of the keywords *do* and *repeat* others methods are sometime use to indicate where the *body* begins and ends, such as [curly braces](https://en.wikipedia.org/wiki/Curly_braces "Curly braces")[\[5\]](https://en.wikipedia.org/wiki/While_loop#cite_note-5) or [whitespace](https://en.wikipedia.org/wiki/Whitespace_characters "Whitespace characters").[\[6\]](https://en.wikipedia.org/wiki/While_loop#cite_note-6)
For example, the following code fragment first checks whether *x* is less than five, which it is, so the *body* is entered. There, *x* is displayed and then incremented by one. After executing the statements in the *body*, the *condition* is checked again, and the loop is executed again. This process repeats until *x* has the value five.
```
x ← 0
while x < 5 do
display(x)
x ← x + 1
repeat
```
[](https://en.wikipedia.org/wiki/File:Do-while-loop-diagram.svg)
Do-While loop flow diagram
A **post-test loop** is a conditional loop where the *condition* is checked after the *body* is executed. More precisely, the *body* is executed and afterwards the *condition* is checked. If it holds the *body* is run again and then the *condition* is checked. This is repeated until the *condition* does not hold. This is sometimes called a *do-while loop* due to the syntax used in various programming languages,[\[7\]](https://en.wikipedia.org/wiki/While_loop#cite_note-7) although this can be confusing since [Fortran](https://en.wikipedia.org/wiki/Fortran "Fortran") and [PL/I](https://en.wikipedia.org/wiki/PL/I "PL/I") use the syntax "DO WHILE" for pre-test loops.[\[8\]](https://en.wikipedia.org/wiki/While_loop#cite_note-8)[\[9\]](https://en.wikipedia.org/wiki/While_loop#cite_note-9) Post-test loops are commonly formatted in manner similar to
```
do
body
repeat while condition
```
Instead of the keywords *do* and *repeat* others methods are sometime use to indicate where the *body* begins and ends, such as curly braces.[\[10\]](https://en.wikipedia.org/wiki/While_loop#cite_note-10)
Some [languages](https://en.wikipedia.org/wiki/Programming_language "Programming language") may use a different naming convention for this type of loop. For example, the [Pascal](https://en.wikipedia.org/wiki/Pascal_programming_language "Pascal programming language") and [Lua](https://en.wikipedia.org/wiki/Lua_\(programming_language\) "Lua (programming language)") languages have a "*repeat until*" loop, which continues to run *until* the control expression is true and then terminates.[\[11\]](https://en.wikipedia.org/wiki/While_loop#cite_note-11)[\[12\]](https://en.wikipedia.org/wiki/While_loop#cite_note-12)
### Three-part for loop
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=5 "Edit section: Three-part for loop")\]
[](https://en.wikipedia.org/wiki/File:For_loop_example.svg)
Flow diagram of a for loop that prints five asterisks.
A **three-part for loop**, popularized by C, has two additional parts: [*initialization*](https://en.wikipedia.org/wiki/Declaration_\(computer_programming\) "Declaration (computer programming)") ([loop variant](https://en.wikipedia.org/wiki/Loop_variant "Loop variant")), and *increment*, both of which are blocks of code. The *initialization* is intended as code that prepares the loop and is run once in the beginning and *increment* is used to update the state of the program after each iteration of the loop. Otherwise, the three-part for loop is a pre-test loop. They are commonly formatted in manner similar to
```
for initialization, condition, increment do
body
repeat
```
This syntax came from [B](https://en.wikipedia.org/wiki/B_\(programming_language\) "B (programming language)") and was originally invented by [Stephen C. Johnson](https://en.wikipedia.org/wiki/Stephen_C._Johnson "Stephen C. Johnson").[\[13\]](https://en.wikipedia.org/wiki/While_loop#cite_note-ken-13) The following C code is an example of a three part loop that prints the numbers from 0 to 4.
```
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
```
### Equivalent constructs
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=6 "Edit section: Equivalent constructs")\]
Assuming there is a function called *do\_work()* that does some work, the following are equivalent.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
As long as the *continue* statement is not used, the above is technically equivalent to the following (though these examples are not typical or modern style used in everyday computers):
```
while true do
do_work()
if condition is not true then
break
end if
repeat
```
or
```
LOOPSTART:
do_work()
if condition then
goto LOOPSTART
end if
```
[](https://en.wikipedia.org/wiki/File:For-Loop-Mint-Programming-Language-Type-2.gif)
foreach loops are almost always used to iterate over items in a sequence of elements.
An **enumeration** (also known as an **determinate loop**[\[2\]](https://en.wikipedia.org/wiki/While_loop#cite_note-joy-2)) is a loop intended to iterate over all the items of a collection.[\[14\]](https://en.wikipedia.org/wiki/While_loop#cite_note-14) It is not as flexible as a conditional loop; but it is more predictable.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] For example, it is easier to guarantee that enumerations [terminate](https://en.wikipedia.org/wiki/Terminate "Terminate") and they avoid potential [off-by-one errors](https://en.wikipedia.org/wiki/Off-by-one_error "Off-by-one error").\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] Enumerations can be implemented using an [iterator](https://en.wikipedia.org/wiki/Iterator "Iterator"), whether implicitly or explicitly. They are commonly formatted in manner similar to
```
for item in collection
body
repeat
```
Depending on the programming language, various keywords are used to invoke enumerations. For example, descendants of [ALGOL](https://en.wikipedia.org/wiki/ALGOL "ALGOL") use `for`,[\[15\]](https://en.wikipedia.org/wiki/While_loop#cite_note-15) while descendants of [Fortran](https://en.wikipedia.org/wiki/Fortran "Fortran") use `do`[\[16\]](https://en.wikipedia.org/wiki/While_loop#cite_note-16) and [COBOL](https://en.wikipedia.org/wiki/COBOL "COBOL") uses `PERFORM VARYING`.[\[17\]](https://en.wikipedia.org/wiki/While_loop#cite_note-17)
Enumerations are sometimes called "for loops," for example in [Zig](https://en.wikipedia.org/wiki/Zig_\(programming_language\) "Zig (programming language)") and [Rust](https://en.wikipedia.org/wiki/Rust_\(programming_language\) "Rust (programming language)").[\[18\]](https://en.wikipedia.org/wiki/While_loop#cite_note-18)[\[19\]](https://en.wikipedia.org/wiki/While_loop#cite_note-19) This can be confusing since many of the most popular[\[20\]](https://en.wikipedia.org/wiki/While_loop#cite_note-20) programming languages, such as C, [C++](https://en.wikipedia.org/wiki/C%2B%2B "C++"), and [Java](https://en.wikipedia.org/wiki/Java_\(programming_language\) "Java (programming language)"), use that term for the three-part for loop,[\[21\]](https://en.wikipedia.org/wiki/While_loop#cite_note-21)[\[22\]](https://en.wikipedia.org/wiki/While_loop#cite_note-22)[\[23\]](https://en.wikipedia.org/wiki/While_loop#cite_note-23) which is not an enumeration. Other programming languages, such as [Perl](https://en.wikipedia.org/wiki/Perl "Perl") and [C\#](https://en.wikipedia.org/wiki/C "C"), avoid this confusion by using the term "foreach loop."[\[24\]](https://en.wikipedia.org/wiki/While_loop#cite_note-24)[\[25\]](https://en.wikipedia.org/wiki/While_loop#cite_note-25)
The order in which the items in the collection are iterated through depends on the programming language. [Fortran 95](https://en.wikipedia.org/wiki/Fortran_95 "Fortran 95") has a loop, invoked using the keyword `FORALL`, that is independent of this order. It has the effect of executing each iteration of the loop at the same time. This feature was made obsolescent in [Fortran 2018](https://en.wikipedia.org/wiki/Fortran_2018 "Fortran 2018").[\[26\]](https://en.wikipedia.org/wiki/While_loop#cite_note-26)
## Loops in functional programming
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=8 "Edit section: Loops in functional programming")\]
In most [functional programming languages](https://en.wikipedia.org/wiki/Functional_programming "Functional programming"), [recursion](https://en.wikipedia.org/wiki/Recursion_\(computer_science\) "Recursion (computer science)") is used instead of traditional loops. This is due to the fact that variables are [immutable](https://en.wikipedia.org/wiki/Immutable_object "Immutable object"), and therefore the *increment* step of a loop cannot occur.[\[27\]](https://en.wikipedia.org/wiki/While_loop#cite_note-27)
To avoid running into [stack overflow](https://en.wikipedia.org/wiki/Stack_overflow "Stack overflow") errors for long loops, functional programming languages implement [tail call optimisation](https://en.wikipedia.org/wiki/Tail_call "Tail call"), which allows the same [stack frame](https://en.wikipedia.org/wiki/Call_stack "Call stack") to be used for each iteration of the loop, compiling to effectively the same code as a *while* or *for* loop.[\[28\]](https://en.wikipedia.org/wiki/While_loop#cite_note-28)
Some languages, such as [Haskell](https://en.wikipedia.org/wiki/Haskell "Haskell"), have a special syntax known as a [list comprehension](https://en.wikipedia.org/wiki/List_comprehension "List comprehension"), which is similar to enumeration, iterating over the contents of a list and transforming it into a new list.
A **loop counter** is a control variable that controls the iterations of a loop. Loop counters change with each iteration of a loop, providing a unique value for each iteration. The loop counter is used to decide when the loop should terminate. It is so named because most uses of this construct result in the variable taking on a range of integer values.
A common [identifier naming convention](https://en.wikipedia.org/wiki/Identifier_naming_convention "Identifier naming convention") is for the loop counter to use the variable names *i*, *j*, and *k* (and so on if needed),[\[29\]](https://en.wikipedia.org/wiki/While_loop#cite_note-29) where *i* would be the most outer loop, *j* the next inner loop, etc. This style is generally agreed to have originated from the early programming of Fortran\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\], where these variable names beginning with these letters were implicitly declared as having an integer type, and so were obvious choices for loop counters that were only temporarily required. The practice dates back further to [mathematical notation](https://en.wikipedia.org/wiki/Mathematical_notation "Mathematical notation") where [indices](https://en.wikipedia.org/wiki/Index_notation "Index notation") for [sums](https://en.wikipedia.org/wiki/Summation "Summation") and [multiplications](https://en.wikipedia.org/wiki/Multiplication "Multiplication") are often *i*, *j*, and *k*.
Using terse names for loop counters, like *i* and *j*, is discouraged by some since the purpose of the variables is not as clear as if they were given a longer more descriptive name.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 383–382
Different languages specify different rules for what value the loop counter will hold on termination of its loop, and indeed some hold that it becomes undefined. This permits a [compiler](https://en.wikipedia.org/wiki/Compiler "Compiler") to generate code that leaves any value in the loop counter, or perhaps even leaves it unchanged because the loop value was held in a register and never stored in memory. Actual behavior may even vary according to the compiler's optimization settings.
Modifying the loop counter within the body of the loop can lead to unexpected consequences. To prevent such problems, some languages make the loop counter [immutable](https://en.wikipedia.org/wiki/Immutable_object "Immutable object").\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\] However, only overt changes are likely to be detected by the compiler. Situations where the address of the loop counter is passed as an argument to a [subroutine](https://en.wikipedia.org/wiki/Subroutine "Subroutine"), make it very difficult to check because the routine's behavior is in general unknowable to the compiler unless the language supports procedure signatures and argument intents.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
## Early exit and continuation
\[[edit](https://en.wikipedia.org/w/index.php?title=Loop_\(statement\)&action=edit§ion=10 "Edit section: Early exit and continuation")\]
Some languages may also provide supporting statements for altering how a loop's iteration proceeds. Common among these are the [*break*](https://en.wikipedia.org/wiki/Break_statement "Break statement") statement, which terminates the current loop the program is in, and the [*continue*](https://en.wikipedia.org/wiki/Control_flow#Continuation_with_next_iteration "Control flow") statement, which skips to the next iteration of the current loop.[\[4\]](https://en.wikipedia.org/wiki/While_loop#cite_note-codecomplete-4): 379 These statements may have other names; For example in [Fortran 90](https://en.wikipedia.org/wiki/Fortran_90 "Fortran 90"), they are called *exit* and *cycle*.\[*[citation needed](https://en.wikipedia.org/wiki/Wikipedia:Citation_needed "Wikipedia:Citation needed")*\]
A loop can also be terminated by [returning](https://en.wikipedia.org/wiki/Return_statement "Return statement") from the function within which it is being executed.
In the case of nested loops, the *break* and *continue* statements apply to the inner most loop. Some languages allow loops to be labelled. These statements can then be applied to any of the loops in which the program is nested.
```
outer_loop: (This is a label for the outermost loop)
for 1 ≤ i ≤ 2 do
for 1 ≤ j ≤ 2 do
display(i, j)
if i = 2
continue outer_loop
end if
repeat
repeat
(This nested loop displays the pairs (1, 1), (1, 2), and (2, 1))
```
An infinite loop is a loop which never terminates. This can be intentional, or the result of a [logic error](https://en.wikipedia.org/wiki/Logic_error "Logic error").
Systematically detecting infinite loops is known as the [halting problem](https://en.wikipedia.org/wiki/Halting_problem "Halting problem").[\[30\]](https://en.wikipedia.org/wiki/While_loop#cite_note-30)
Infinite loops are useful in applications which need to perform a repeated calculation until a program terminates, such as [web servers](https://en.wikipedia.org/wiki/Web_servers "Web servers").[\[31\]](https://en.wikipedia.org/wiki/While_loop#cite_note-31)
- [Primitive recursive function](https://en.wikipedia.org/wiki/Primitive_recursive_function "Primitive recursive function")
- [General recursive function](https://en.wikipedia.org/wiki/General_recursive_function "General recursive function")
- [Repeat loop (disambiguation)](https://en.wikipedia.org/wiki/Repeat_loop_\(disambiguation\) "Repeat loop (disambiguation)")
- [LOOP (programming language)](https://en.wikipedia.org/wiki/LOOP_\(programming_language\) "LOOP (programming language)") – a programming language with the property that the functions it can compute are exactly the primitive recursive functions
1. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-1)**
Angelou, Alexandros; Dadaliaris, Antonios; Dossis, Michael; Dimitriou, Georgios (2021-11-26). ["Branchless Code Generation for Modern Processor Architectures"](https://doi.org/10.1145/3503823.3503879). *25th Pan-Hellenic Conference on Informatics*. New York, NY, USA: ACM: 300–305\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1145/3503823.3503879](https://doi.org/10.1145%2F3503823.3503879). [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-1-4503-9555-7](https://en.wikipedia.org/wiki/Special:BookSources/978-1-4503-9555-7 "Special:BookSources/978-1-4503-9555-7")
.
2. ^ [***a***](https://en.wikipedia.org/wiki/While_loop#cite_ref-joy_2-0) [***b***](https://en.wikipedia.org/wiki/While_loop#cite_ref-joy_2-1)
Samanta, Debasis; Sarma, Monalisa (15 June 2023). [*Joy with Java: Fundamentals of Object Oriented Programming*](https://books.google.com/books?id=34GyEAAAQBAJ). Cambridge University Press. p. 124. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-1-009-21190-1](https://en.wikipedia.org/wiki/Special:BookSources/978-1-009-21190-1 "Special:BookSources/978-1-009-21190-1")
.
3. ^ [***a***](https://en.wikipedia.org/wiki/While_loop#cite_ref-bbc1_3-0) [***b***](https://en.wikipedia.org/wiki/While_loop#cite_ref-bbc1_3-1)
["Conditional loops - Computational constructs - National 4 Computing Science Revision"](https://web.archive.org/web/20211019111021/https://www.bbc.co.uk/bitesize/guides/zcg9kqt/revision/8). *BBC Bitesize*. Archived from [the original](https://www.bbc.co.uk/bitesize/guides/zcg9kqt/revision/8) on 19 October 2021. Retrieved 8 January 2026.
4. ^ [***a***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-0) [***b***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-1) [***c***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-2) [***d***](https://en.wikipedia.org/wiki/While_loop#cite_ref-codecomplete_4-3)
McConnell, Steve (9 June 2004). [*Code Complete*](https://books.google.com/books?id=LpVCAwAAQBAJ). Pearson Education. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-0-7356-3697-2](https://en.wikipedia.org/wiki/Special:BookSources/978-0-7356-3697-2 "Special:BookSources/978-0-7356-3697-2")
.
5. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-5)**
["while Statement (GNU C Language Manual)"](https://web.archive.org/web/20240712235744/https://www.gnu.org/software/c-intro-and-ref/manual/html_node/while-Statement.html). *www.gnu.org*. Archived from [the original](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/while-Statement.html) on 12 July 2024. Retrieved 8 January 2026.
6. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-6)**
["3. An Informal Introduction to Python"](https://web.archive.org/web/20251231204216/https://docs.python.org/3/tutorial/introduction.html). *Python documentation*. Archived from [the original](https://docs.python.org/3/tutorial/introduction.html) on 31 December 2025. Retrieved 8 January 2026.
7. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-7)**
["do...while - JavaScript \| MDN"](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while). *MDN Web Docs*. 2025-07-08. Retrieved 2026-01-22.
8. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-8)**
["DO WHILE (FORTRAN 77 Language Reference)"](https://web.archive.org/web/20230314015649/https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn8e/index.html). *docs.oracle.com*. Archived from [the original](https://docs.oracle.com/cd/E19957-01/805-4939/6j4m0vn8e/index.html) on 14 March 2023. Retrieved 8 January 2026.
9. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-9)**
["DO command (PL/I)"](https://web.archive.org/web/20250302164825/https://www.ibm.com/docs/en/wdfrhcw/1.4.0?topic=commands-do-command-pli). *www.ibm.com*. Archived from [the original](https://www.ibm.com/docs/en/wdfrhcw/1.4.0?topic=commands-do-command-pli) on 2 March 2025. Retrieved 22 January 2026.
10. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-10)**
["do-while Statement (GNU C Language Manual)"](https://web.archive.org/web/20240713012759/https://www.gnu.org/software/c-intro-and-ref/manual/html_node/do_002dwhile-Statement.html). *www.gnu.org*. Archived from [the original](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/do_002dwhile-Statement.html) on 13 July 2024. Retrieved 8 January 2026.
11. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-11)**
["The Repeat..until statement"](https://web.archive.org/web/20251109225700/https://www.freepascal.org/docs-html/ref/refsu60.html). *www.freepascal.org*. Archived from [the original](https://www.freepascal.org/docs-html/ref/refsu60.html) on 9 November 2025. Retrieved 22 January 2026.
12. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-12)**
["Programming in Lua : 4.3.3"](https://web.archive.org/web/20260102100039/https://www.lua.org/pil/4.3.3.html). *www.lua.org*. Archived from [the original](https://www.lua.org/pil/4.3.3.html) on 2 January 2026. Retrieved 22 January 2026.
13. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-ken_13-0)**
[Thompson, Ken](https://en.wikipedia.org/wiki/Ken_Thompson "Ken Thompson"). [*VCF East 2019 – Brian Kernighan interviews Ken Thompson*](https://www.youtube.com/watch?v=EY6q5dv_B-o&t=2330). *[YouTube](https://en.wikipedia.org/wiki/YouTube "YouTube")*. [Archived](https://ghostarchive.org/varchive/youtube/20211212/EY6q5dv_B-o) from the original on 2021-12-12. Retrieved 2020-11-16. "I saw Johnson's semicolon version of the for loop and I put that in \[B\], I stole it."
14. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-14)**
McConnell, Steve (9 June 2004). [*Code Complete*](https://books.google.com/books?id=LpVCAwAAQBAJ&q=code+complete). Pearson Education. p. 367. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[978-0-7356-3697-2](https://en.wikipedia.org/wiki/Special:BookSources/978-0-7356-3697-2 "Special:BookSources/978-0-7356-3697-2")
.
15. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-15)**
[Wirth, Niklaus](https://en.wikipedia.org/wiki/Niklaus_Wirth "Niklaus Wirth") (1973). "Preface". *Systematic Programming: An Introduction*. Prentice-Hall. pp. xiii. [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[0138803692](https://en.wikipedia.org/wiki/Special:BookSources/0138803692 "Special:BookSources/0138803692")
.
16. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-16)**
["DO / END DO"](https://web.archive.org/web/20260108231419/https://www.ibm.com/docs/en/xl-fortran-linux/16.1.0?topic=directives-do-end-do). *www.ibm.com*. 24 April 2018. Archived from [the original](https://www.ibm.com/docs/en/xl-fortran-linux/16.1.0?topic=directives-do-end-do) on 8 January 2026. Retrieved 8 January 2026.
17. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-17)**
["PERFORM with VARYING Phrase"](https://web.archive.org/web/20260108231758/https://www.ibm.com/docs/en/i/7.5.0?topic=statement-perform-varying-phrase). *www.ibm.com*. June 2012. Archived from [the original](https://www.ibm.com/docs/en/i/7.5.0?topic=statement-perform-varying-phrase) on 8 January 2026. Retrieved 8 January 2026.
18. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-18)**
["Zig documentation"](https://web.archive.org/web/20260104121319/https://ziglang.org/documentation/master/#for). *ziglang.org*. Archived from [the original](https://ziglang.org/documentation/master/#for) on 4 January 2026. Retrieved 8 January 2026.
19. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-19)**
["Looping Through a Collection with for"](https://web.archive.org/web/20251219092527/https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html#looping-through-a-collection-with-for). *rust-lang.org*. Archived from [the original](https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html#looping-through-a-collection-with-for) on 19 December 2025. Retrieved 8 January 2026.
20. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-20)**
["TIOBE Index for September 2024"](https://web.archive.org/web/20260104151245/https://www.tiobe.com/tiobe-index/). Archived from [the original](https://www.tiobe.com/tiobe-index/) on January 4, 2026. Retrieved 2025-12-16.
21. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-21)**
["for Statement (GNU C Language Manual)"](https://web.archive.org/web/20240713014231/https://www.gnu.org/software/c-intro-and-ref/manual/html_node/for-Statement.html). *www.gnu.org*. Archived from [the original](https://www.gnu.org/software/c-intro-and-ref/manual/html_node/for-Statement.html) on 13 July 2024. Retrieved 8 January 2026.
22. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-22)**
["for statement (C++)"](https://web.archive.org/web/20250928185930/https://learn.microsoft.com/en-us/cpp/cpp/for-statement-cpp?view=msvc-170). *learn.microsoft.com*. Archived from [the original](https://learn.microsoft.com/en-us/cpp/cpp/for-statement-cpp?view=msvc-170) on 28 September 2025. Retrieved 8 January 2026.
23. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-23)**
["The for Statement (The Java™ Tutorials \> Learning the Java Language \> Language Basics)"](https://web.archive.org/web/20251228051928/https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html). *docs.oracle.com*. Archived from [the original](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html) on 28 December 2025. Retrieved 8 January 2026.
24. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-24)**
["Iteration statements -for, foreach, do, and while - C\# reference"](https://web.archive.org/web/20251228021319/https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements). *learn.microsoft.com*. Archived from [the original](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/iteration-statements) on 28 December 2025. Retrieved 8 January 2026.
25. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-25)**
["Perl for Loop"](https://web.archive.org/web/20250607160051/https://www.perltutorial.org/perl-for-loop/). *Perl Tutorial*. Archived from [the original](https://www.perltutorial.org/perl-for-loop/) on 7 June 2025. Retrieved 8 January 2026.
26. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-26)**
["FORALL"](https://web.archive.org/web/20260101223642/https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-2/forall.html). *Intel*. Archived from [the original](https://www.intel.com/content/www/us/en/docs/fortran-compiler/developer-guide-reference/2025-2/forall.html) on 1 January 2026. Retrieved 8 January 2026.
27. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-27)**
Hinsen, Konrad (2009). ["The Promises of Functional Programming"](https://doi.org/10.1109/mcse.2009.129). *Computing in Science & Engineering*. **11** (4): 86–90\. [Bibcode](https://en.wikipedia.org/wiki/Bibcode_\(identifier\) "Bibcode (identifier)"):[2009CSE....11d..86H](https://ui.adsabs.harvard.edu/abs/2009CSE....11d..86H). [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1109/mcse.2009.129](https://doi.org/10.1109%2Fmcse.2009.129). [ISSN](https://en.wikipedia.org/wiki/ISSN_\(identifier\) "ISSN (identifier)") [1521-9615](https://search.worldcat.org/issn/1521-9615).
28. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-28)**
Clinger, William D. (1998). "Proper tail recursion and space efficiency". *Proceedings of the ACM SIGPLAN 1998 conference on Programming language design and implementation*. New York, NY, USA: ACM. pp. 174–185\. [doi](https://en.wikipedia.org/wiki/Doi_\(identifier\) "Doi (identifier)"):[10\.1145/277650.277719](https://doi.org/10.1145%2F277650.277719). [ISBN](https://en.wikipedia.org/wiki/ISBN_\(identifier\) "ISBN (identifier)")
[0-89791-987-4](https://en.wikipedia.org/wiki/Special:BookSources/0-89791-987-4 "Special:BookSources/0-89791-987-4")
.
29. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-29)** <http://www.knosof.co.uk/vulnerabilities/loopcntrl.pdf> Analysis of loop control variables in C
30. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-30)**
Lee, Sarah. ["Turing's Halting Problem Explained"](https://www.numberanalytics.com/blog/ultimate-guide-turing-halting-problem). *www.numberanalytics.com*. Retrieved 2026-01-22.
31. **[^](https://en.wikipedia.org/wiki/While_loop#cite_ref-31)**
["What is an infinite loop (endless loop)?"](https://www.techtarget.com/whatis/definition/infinite-loop-endless-loop). *WhatIs*. Retrieved 2026-01-22. |
| Shard | 152 (laksa) |
| Root Hash | 17790707453426894952 |
| Unparsed URL | org,wikipedia!en,/wiki/While_loop s443 |