# Inner loop

> Mediated Wiki article. Canonical URL: https://mediated.wiki/source/Inner_loop
> Markdown URL: https://mediated.wiki/source/Inner_loop.md
> Source: https://en.wikipedia.org/wiki/Inner_loop
> Source revision: 1270224872
> License: Creative Commons Attribution-ShareAlike 4.0 International (https://creativecommons.org/licenses/by-sa/4.0/)

For other uses, see [Inner loop (disambiguation)](/source/Inner_loop_(disambiguation)).

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed. Find sources: "Inner loop" – news · newspapers · books · scholar · JSTOR (September 2018) (Learn how and when to remove this message)

In [computer programs](/source/Computer_program), an important form of [control flow](/source/Control_flow) is the [loop](/source/Loop_(computing)) which causes a block of code to be executed more than once. A common idiom is to have a loop [nested](/source/Nested_loop) inside another loop, with the contained loop being commonly referred to as the inner loop.

## Program optimization

Because the entire inner loop is performed for each iteration of the outer loop, [optimizations](/source/Loop_optimization) of the inner loop will have much greater effect than optimizations of the outer loop.

In many languages there are at least two types of loops – [for loops](/source/For_loop) and [while loops](/source/While_loop) – and they can be nested within each other.[1] Tosin P. Adewumi has shown that performance of a while loop with an inner for loop is better than of a while loop without the inner for loop.[2]

It was observed that more computations are performed per unit time when an inner for loop is involved than otherwise. This implies, given the same number of computations to perform, the one with an inner for loop will finish faster than the one without it. This technique of [loop optimization](/source/Loop_optimization) was observed across several programming languages and compilers or interpreters. In some cases, a while loop with an inner while loop performed slower than a while loop without an inner loop.

The two examples below, written in Python, present a while loop with an inner for loop and a while loop without an inner loop. Although both have the same terminating condition for their while loops, the first example will finish faster because of the inner for loop. The variable *innermax* is a fraction of the *maxticketno* variable in the first example.[3]

while ticket_no * innermax < max_ticket_no:
    for j in range(0, innermax):
        if (ticket_no * innermax + j) == jackpot_no:
            return
    ticket_no += 1

while ticket_no < max_ticket_no:
    if ticket_no == jackpot_no:
        return
    ticket_no += 1

## References

1. **[^](#cite_ref-1)** Ghezzi, C; Jazayeri, M (1996). *Programming Language Concepts (3rd edn.)*. John Wiley & Sons.

1. **[^](#cite_ref-2)** Adewumi, Tosin (August 2018). ["Inner loop program construct: a faster way for program execution"](https://doi.org/10.1515%2Fcomp-2018-0004). *Open Computer Science*. **8**: 115–122. [doi](/source/Doi_(identifier)):[10.1515/comp-2018-0004](https://doi.org/10.1515%2Fcomp-2018-0004).

1. **[^](#cite_ref-3)** Vishal (2021-06-09). ["Nested Loops in Python"](https://pynative.com/python-nested-loops/). *PYnative*. Retrieved 2022-09-07.

4. [Python Nested For Loop From Techgeekbuz](https://www.techgeekbuzz.com/blog/nested-loops-in-python/)

This computer-programming-related article is a stub. You can help Wikipedia by adding missing information.

- [v](https://en.wikipedia.org/wiki/Template:Compu-prog-stub)
- [t](/source/Template_talk%3ACompu-prog-stub)
- [e](https://en.wikipedia.org/wiki/Special:EditPage/Template:Compu-prog-stub)

---
Adapted from the Wikipedia article [Inner loop](https://en.wikipedia.org/wiki/Inner_loop) by Wikipedia contributors ([contributor history](https://en.wikipedia.org/wiki/Inner_loop?action=history)). Available under [Creative Commons Attribution-ShareAlike 4.0 International](https://creativecommons.org/licenses/by-sa/4.0/). Changes may have been made.
