Sitemap

COMPUTER SCIENCE

CS Issue 20: Loop constructs and control-flow patterns

3 min readAug 31, 2026

--

A glass falls, it shatters. But we never see the reverse! Moreover, astronomical data shows that time flows forward rather than repeating in a circle. Heck, even for angels, time moves forward! But, nevertheless, in computer programming, we may want things like loops.

A loop doesn’t need you! In Engineering, it’s used to remove humans-in-the-middle. But we will discuss loops in Computer Science.

Section A: for/while loops are how repetition is written

(i) The for loop is a count-controlled structure. This means that it will be run for a total number of iterations which are hard-coded or determined at runtime.

The following general form is written in pseudocode, which is an informal way of describing the procedure and/or structure of a program in plain text. It is useful for describing a program or algorithm without a specific language.

for variable in [value_1, value_2, value_3, value_etc.]:
statement
statement
statement_etc.

Focusing on Python, we would have the following.

for i in range(n):
...

(ii) The while loop is a condition-controlled loop. It will continue as long as a certain condition is met i.e. until a Boolean condition evaluates to false in a programming language. Because of this, it has more programming errors than a for loop e.g. it’s easier to program an infinite loop where the program continues running forever or until the program halts with an error.

It has the following general form.

while condition:
statement
statement
etc.

To STOP an infinite loop in PyCharm, click the red Stop icon next to the pane which shows the output. To AVOID an infinite loop, create a variable that will count how many times the loop has run. It should be assigned before the loop itself. For our purposes, have this variable start at 1. In the loop code, add a line that increments this variable by 1 each time the loop code is executed [Thus it’s usually at the bottom of the code inside the loop]. Now we have a counter variable — it starts at 1 for the first run of the loop, and then increases to 2 the second time, 3 the third, etc.

Get Celestine Emmanuel’s stories in your inbox

Join Medium for free to get updates from this writer.

Printing 1 to 10 in Python: You can experiment with other increments besides + 1. While loops offer total control over how things are incremented; you could easily square your counter variable each time if that would suit your purposes.

i=1
while i<11:
print(i) #we can print anything here ten times, not just numbers from 1 to 10
i=i+1

Section B: control-flow patterns

(i) A single loop is one loop whose body doesn’t contain another loop. So this simple common pattern describes just one repetition structure.

On the other hand, a nested loop describes repetition inside repetition. In a conventional nested loop, the outer loop ‘moves slower’ than the inner loop — i.e. the element stored in outer loop variable keeps its value while the inner loop runs. Once the inner loop finishes, the outer loop advances to its next iteration, causing the inner loop to execute again.

  • A triangular loop is a special kind of nested loop.

(ii) Sequential loops describe a composition pattern. The general form is loop a → loop b → loop c. So loop a is first coded, then loop b next, then loop c. The following is in Python.

for i in range(n):
...
for j in range(n):
...

(iii) A halving loop divides a value by two during each cycle. The following is in Python.

while n > 1:
n = n // 2

(iv) Loop + Binary Search is a term used to describe when loop constructs are used with binary search.

💥Food for thought: Permutations are combinatorial patterns. They can be implemented using for, while, recursion, library functions, etc.

Sources:

Worldquant.

Instagram — algomasterio.

An article about time by Medium writer Rudi Widiyanto.

--

--

Celestine Emmanuel
Celestine Emmanuel

Written by Celestine Emmanuel

✔️BSc in Actuarial Science ✔️MSc in Quantitative Finance and Risk Management ✔️MSc in Financial Engineering CGPA 92% ✔️MA in Data Science Left school half-term