FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Do_while_loop

Loop (statement)

Contents
    Pre-test loop
    Post-test loop
    Three-part for loop
Enumeration
Loop counter
See also
References

In computer programming, a loop is a control flow construct 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.

Pre-test loop

edit

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 executed. 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, other methods are sometimes used to indicate where the body begins and ends, such as curly braces or whitespace.

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
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, although this can be confusing since Fortran and PL/I use the syntax "DO WHILE" for pre-test loops. 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.

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.

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. The following C code is an example of a three part loop that prints the numbers from 0 to 4.

[</span>icon<span typeof=\"mw:Entity\" id=\"mw-A\">]</span>","txt":"[icon]"}]]}'>

Assuming there is a properly declared function or method called do_work(), the following are equivalent in programming languages which support post-test loops.

do do_work() repeat while condition do_work() while condition do do_work() repeat

Furthermore, given that a 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 alternatively,

LOOPSTART: do_work() if condition then goto LOOPSTART end if

Enumeration

edit
foreach loops are almost always used to iterate over items in a sequence of elements.

An enumeration (also known as an determinate loop) is a loop intended to iterate over all the items of a collection. Enumerations can be implemented using an iterator, whether implicitly or explicitly.do and COBOL uses

Loop counter

edit

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), 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

Systematically detecting infinite loops is known as the halting problem. It is known that there is no general algorithm which decides whether a particular loop is infinite or not.

Infinite loops are useful in applications which need to perform a repeated calculation until a program terminates, such as web servers.

See also

edit

References

edit
"Conditional loops - Computational constructs - National 4 Computing Science Revision". BBC Bitesize. Archived from the original on 19 October 2021. Retrieved 8 January 2026.
  • 1 2 3 4 "while Statement (GNU C Language Manual)". www.gnu.org. Archived from the original on 12 July 2024. Retrieved 8 January 2026.
  • ↑ "DO WHILE (FORTRAN 77 Language Reference)". docs.oracle.com. Archived from the original on 14 March 2023. Retrieved 8 January 2026.
  • ↑ "The Repeat..until statement". www.freepascal.org. Archived from the original on 9 November 2025. Retrieved 22 January 2026.
  • ↑ Language Basics)"},"url":{"wt":"https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html"},"access-date":{"wt":"2026-05-16"},"website":{"wt":"docs.oracle.com"}},"i":0}}]}'/>"The while and do-while Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)". docs.oracle.com. Retrieved 2026-05-16.
  • ↑ Parlante, Nick. "While Loop". Stanford University. Retrieved 16-05-26.
  • ↑ Wirth, Niklaus (1973). "Preface". Systematic Programming: An Introduction. Prentice-Hall. pp. xiii. ISBN 0138803692.
  • ↑ "Zig documentation". ziglang.org. Archived from the original on 4 January 2026. Retrieved 8 January 2026.
  • ↑ "for Statement (GNU C Language Manual)". www.gnu.org. Archived from the original on 13 July 2024. 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.
  • ↑ 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.
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home