| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Do-while_loop |
| 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.
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 repeatInstead 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 repeatA 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 conditionInstead 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.
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 repeatThis 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 repeator alternatively,
LOOPSTART: do_work() if condition then goto LOOPSTART end ifAn 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
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.
| View original on Wikipedia | Back | FazBrowse Home |