Loop structures and how they work.

Loop Structures — The Method Of Repeating Routines In Statements

Vince Tabora
Dec 2 · 4 min read

Repetition of code are called loops, and they are defined as statements that execute lines of code (or routines) repeatedly according to conditions or iterations. While the code repeats itself, it is performing an action that must terminate when a condition is met or when an iteration stops.

The convenience of loops helps in automating a method that would otherwise be manually executed every time. Writing loop structures automates tasks that rely on repeating lines of code. Take for example a routine that must write as output the string “Hello” 40 times. Without a loop structure, a coder would have to manually code the statement to write the string “Hello” 40 times, requiring 40 lines of redundant code. Instead the code can use a loop structure to go over a certain number of times to write “Hello” and terminate once it is done.

This diagram is a flow chart of how a loop works in a computer program. It begins with the initialization of a variable is passed to a condition. If the condition is TRUE, it will execute a routine and then go through a loop until the condition is FALSE. The routine then terminates to end the program.

Conditional Loop

In a conditional loop structure, a routine is repeated based on a condition. For example we can set a loop to print all numbers in sequence until the variable n, with a counter i set from 0 to n. The counter i is initialized with a value of 0.

  • Increment by a variable of x = 1, until i is less than n = 100. (We just substitute the values in the code)

Python (Example 1)

i = 0
while i < 100:
i += 1
print(i)

This will result in the following output:

1
2
3
.
.
.
100

Javascript (Example 1)

var i;
for (i = 0; i < 100; i++) {
console.log(+i);
}

This results in the following output:

0
1
2
.
.
.
99

There is a difference in how the sequence of numbers is processed between Python and Javascript. In Python, the sequence was initialized to 0 but begins with the number 1. In Javascript, i was also initialized to 0 and begins the counter at 0 rather than 1 and it ends in 99 instead of 100. To begin the counter at 1 and end in 100, the code needs to be changed to the following:

var i;
for (i = 1; i <= 100; i++) {
console.log(+i);
}

In Python the result will be different if we change the ‘<’ to ‘< =’ operator. Instead we will end in 101 instead of 100 since the value of i will continue to repeat the increment until i less than or equal to 100.

  • Decrement by a variable of x = 1, until i is equal to n = 0, the counter i is initialized with a value of 100. (We just substitute the values in the code)

Python (Example 2)

i = 100
while i > 0:
i -= 1
print(i)

This will result in the output:

99
98
97
.
.
.
0

Javascript (Example 2)

var i;
for(i=100; i>=0; i--) {
console.log(+i);
}

The resulting output is:

100
99
98
.
.
.
0

Iterative Loops

A loop can also be non-conditional using iterations. The iterations are steps in a process that go over different values, like one would encounter in lists or arrays. Each step is a repetition of a routine that performs an operation on an object that is stored in memory.

Python (Example 3)

An iterable is a collection of values stored in a list or tuple. These values include strings, list, tuple, dict, sets and frozensets. It uses the for keyword with the following syntax structure:

for <var> in <iterable>:
<statement(s)>

Let’s create a list called ‘superheroes’.

heroes = ['superman', 'batman', 'wonder woman', 'flash']

A list is created that has 4 elements or values. Each element is part of an iterable object called heroes. Using the for keyword, we can iterate the elements of that list.

heroes = ['superman', 'batman', 'wonder woman', 'flash']
for i in heroes:
print(i)

The resulting output will print each element of the list starting with ‘superman’.

superman
batman
wonder woman
flash

Javascript (Example 3)

In Javascript, iterations can use the for loop keyword for…in or for…of syntax:

for (<variable in object>)
<statement>

When using for…in, the iterations are the property names. We are going to create an array called ‘arr’ as a constant with 4 elements.

const arr = ["superman", "batman", "wonder woman", "flash"];for (let i in arr) {
console.log(i);
}

This returns only the property names of the elements (akin to the index).

0
1
2
3

When using for…of the property values are returned.

const arr = ["superman", "batman", "wonder woman", "flash"];for (let i of arr) {
console.log(i);
}

This will return the values of the array’s elements.

superman
batman
wonder woman
flash

Synopsis

These are basic loop structures that either iterate an object or repeat a routine based on a condition. Loops automate repetitive tasks that saves time in running applications.

0xCODE

Essays and Articles on software engineering, development and computer science.

Vince Tabora

Written by

Electrical Engineering, Data Science, Photography, Blockchain, AI, Cybersecurity, Multimedia, EV and Autonomous Vehicles

0xCODE

0xCODE

Essays and Articles on software engineering, development and computer science.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade