{ What is a LOOP in programming } – Programming theory

The loop is a repetitive control structure that allows us to repeat a sequence of instructions until a condition is no longer true.

In a computer program, instructions are executed instruction by instruction in a sequential manner, this is known as the program flow. The control structures allow us to alter this execution flow, enabling us to make jumps, make the flow go backwards or move to another region.

So a LOOP in programming is a control structure that makes the flow of the program go back and repeat exactly the same instructions defined in a region, it is not just an elegant and compact way to write a series of instructions, because the fact that the instructions are exactly the same does not mean that the data used inside the loop are also the same, the information can be transformed and then the following instructions will be applied on those new data, giving rise to a huge amount of possibilities.

What is an INFINITE LOOP in programming?

An infinite loop is a situation in which the program flow is confined to a region from which it cannot escape, endlessly repeating the same instructions over and over again, without reaching a condition that allows it to exit the loop.

When an infinite loop occurs, applications can freeze, become unresponsive and have to be forced to close. This usually occurs because the code that allows processing user interactions, such as screen clicks or keyboard inputs, is not executing because the program flow is trapped in the loop. In applications such as games where all the logic is processed between each picture produced by the game, if an infinite loop occurs, the program flow does not finish processing the logic and the game cannot produce the next picture.

Safety measures can be applied to prevent these loops from occurring, e.g. counters that increment in each iteration and if they exceed a certain value, force the loop’s escape condition.

REPETITIVE control structures and syntaxis

Below we will look at the most common repetitive control structures found in all programming languages.

For Loop

A FOR loop allows us to repeat instructions a fixed number of times that we indicate with a series of conditions on an iterator variable. We set the iterator variable, the initial value, the condition that the iterator variable must meet to execute the loop, and a form of increment for the iterator variable.

Some applications can be to traverse a collection of objects applying actions to each of them and making use of the iterator variable in different ways, we can perform operations such as summation, finding values within a collection, sorting elements and more.

A FOR loop does not usually produce infinite loops, unless its conditions are incorrectly set or the iterator variable is modified within the loop in a way that produces such a situation, it is not good practice to modify the iterator variable within a for loop.

Syntax of a FOR LOOP

Let n be an integer value such as the size of a collection, the FOR loop syntax in C# is as follows:

for(int i=0; i<n ; i++){
//Instructions to be performed inside the loop

//The variable i is incremented at each iteration.


A WHILE loop allows us to repeat instructions until a certain condition is no longer true, it is very useful when we do not know how many times we will have to repeat a task until we get what we need.

A possible application can be the selection of random numbers or objects without repetition, in that case there is always the probability of choosing a value that had been chosen before, so it may be necessary to repeat the operation until a new value is found.

In a WHILE loop it is more common to have infinite loops since the loop output condition depends on what we need in each particular case.

Syntax of a WHILE LOOP

Given a variable name “condition”, which is a Boolean value, for example a Boolean variable, the result of a logical operation or the result of the execution of a programming method which returns a boolean value, the syntax of a WHILE loop in C# is as follows

//Instructions to be performed inside the loop

//”condition must change to false to exit the loop.


A FOREACH loop is used to go through each one of the elements of a collection, for example an array or a list, the idea is that inside the loop we have the reference of each one of the objects but without the need of an iteration variable.

As an application example it can be any task that requires to traverse a collection of elements, for example given a collection of enemy objects, apply to each of them a certain damage by magic. Another example can be to apply a certain initial configuration to a set of elements.

A FOREACH loop does not usually produce infinite loops, unless there is some way to increase the number of elements of the collection being traversed within the same loop, that would cause us to add a new element to traverse on each iteration and we would never finish.

Syntax of a FOREACH LOOP

For this example we are going to consider any programming class that we will call “DataType”, we are going to use the word “obj” to refer to each element within the collection to be traversed and finally we are going to use a collection of objects that we will call “collection” and that will be of the same type as “DataType”. The syntax of a FOREACH loop in C# is the following:

foreach( DataType obj in collection){
// Instructions to be performed inside the loop

//”obj” represents each element of the collection

The following example goes through a collection of GameObjects that represent enemies in a hypothetical game and proceeds to destroy each of those objects.

foreach(GameObject g in enemies){

Scroll to Top
Secured By miniOrange