ArticleslgStudy

science

Loop (statement)

Loop (statement) is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Loop (statement) rather than just read about it. In short: 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.

Loop (statement) — main illustration
Loop (statement) — illustration

Key takeaways

  • Loop (statement) belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Loop (statement) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Loop (statement) from memory before moving on to harder problems.

Reference excerpt

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.

Overview 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. Usually, loops are expected to run for a finite number of iteration. 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.

Conditional loop A conditional loop (also known as an indeterminate loop) is a loop that determines whether to terminate based on a logical condition. These loops are flexible, but their exact behavior can be difficult to reason about. 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. 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. 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.

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

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

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.

Equivalent constructs

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

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

An enumeration (also known as an determinate loop) is a loop intended to iterate over all the items of a collection. It is not as flexible as a conditional loop; but it is more predictable. For example, it is easier to guarantee that enumerations terminate and they avoid potential off-by-one errors. Enumerations can be implemented using an iterator, whether implicitly or explicitly. Depending on the programming language, various keywords are used to invoke enumerations. For example, descendants of ALGOL use for, while descendants of Fortran use do and COBOL uses PERFORM VARYING. Enumerations are sometimes called "for loops," for example in Zig and Rust. This can be confusing since many of the most popular programming languages, such as C, C++, and Java, use that term for the three-part for loop, which is not an enumeration. Other programming languages, such as Perl and C#, avoid this confusion by using the term "foreach loop." 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.

Loops in functional programming

… excerpt ends here. Continue reading the full article.

Illustrations

Loop (statement): Flow diagram of a for loop that prints five asterisks.
Flow diagram of a for loop that prints five asterisks.
Loop (statement): .mw-parser-output .monospaced{font-family:monospace,monospace}foreach loops are almost always used to iterate over items in a sequence of elements.
.mw-parser-output .monospaced{font-family:monospace,monospace}foreach loops are almost always used to iterate over items in a sequence of elements.

Worked examples

Example 1 — a first encounter with Loop (statement)

Start with the simplest possible case. Write down what Loop (statement) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Loop (statement) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Loop (statement) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Loop (statement)

In research
Loop (statement) appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Loop (statement) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Loop (statement) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Control flow, so understanding it makes those chapters shorter.
In everyday life
Look for Loop (statement) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Loop (statement)” →

How to study Loop (statement) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Loop (statement) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Loop (statement) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Loop (statement) in simple terms?

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.

Why does Loop (statement) matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Loop (statement)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Loop (statement).

Tags

  • Control flow

Keep exploring