{ What is a LOOP in programming } — Programming theory

The loop is a repet­i­tive con­trol struc­ture that allows us to repeat a sequence of instruc­tions until a con­di­tion is no longer true.

In a com­put­er pro­gram, instruc­tions are exe­cut­ed instruc­tion by instruc­tion in a sequen­tial man­ner, this is known as the pro­gram flow. The con­trol struc­tures allow us to alter this exe­cu­tion flow, enabling us to make jumps, make the flow go back­wards or move to anoth­er region.

So a LOOP in pro­gram­ming is a con­trol struc­ture that makes the flow of the pro­gram go back and repeat exact­ly the same instruc­tions defined in a region, it is not just an ele­gant and com­pact way to write a series of instruc­tions, because the fact that the instruc­tions are exact­ly the same does not mean that the data used inside the loop are also the same, the infor­ma­tion can be trans­formed and then the fol­low­ing instruc­tions will be applied on those new data, giv­ing rise to a huge amount of possibilities.

CHECK OUT THIS CRAZY EXPERIMENT

What is an INFINITE LOOP in programming?

An infi­nite loop is a sit­u­a­tion in which the pro­gram flow is con­fined to a region from which it can­not escape, end­less­ly repeat­ing the same instruc­tions over and over again, with­out reach­ing a con­di­tion that allows it to exit the loop.

When an infi­nite loop occurs, appli­ca­tions can freeze, become unre­spon­sive and have to be forced to close. This usu­al­ly occurs because the code that allows pro­cess­ing user inter­ac­tions, such as screen clicks or key­board inputs, is not exe­cut­ing because the pro­gram flow is trapped in the loop. In appli­ca­tions such as games where all the log­ic is processed between each pic­ture pro­duced by the game, if an infi­nite loop occurs, the pro­gram flow does not fin­ish pro­cess­ing the log­ic and the game can­not pro­duce the next picture.

Safe­ty mea­sures can be applied to pre­vent these loops from occur­ring, e.g. coun­ters that incre­ment in each iter­a­tion and if they exceed a cer­tain val­ue, force the loop's escape condition.

REPETITIVE control structures and syntaxis

Below we will look at the most com­mon repet­i­tive con­trol struc­tures found in all pro­gram­ming languages.

For Loop

A FOR loop allows us to repeat instruc­tions a fixed num­ber of times that we indi­cate with a series of con­di­tions on an iter­a­tor vari­able. We set the iter­a­tor vari­able, the ini­tial val­ue, the con­di­tion that the iter­a­tor vari­able must meet to exe­cute the loop, and a form of incre­ment for the iter­a­tor variable.

Some appli­ca­tions can be to tra­verse a col­lec­tion of objects apply­ing actions to each of them and mak­ing use of the iter­a­tor vari­able in dif­fer­ent ways, we can per­form oper­a­tions such as sum­ma­tion, find­ing val­ues with­in a col­lec­tion, sort­ing ele­ments and more.

A FOR loop does not usu­al­ly pro­duce infi­nite loops, unless its con­di­tions are incor­rect­ly set or the iter­a­tor vari­able is mod­i­fied with­in the loop in a way that pro­duces such a sit­u­a­tion, it is not good prac­tice to mod­i­fy the iter­a­tor vari­able with­in a for loop.

Syntax of a FOR LOOP

Let n be an inte­ger val­ue such as the size of a col­lec­tion, the FOR loop syn­tax in C# is as follows:

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

//The vari­able i is incre­ment­ed at each iter­a­tion.
}

WHILE Loop

A WHILE loop allows us to repeat instruc­tions until a cer­tain con­di­tion is no longer true, it is very use­ful when we do not know how many times we will have to repeat a task until we get what we need.

A pos­si­ble appli­ca­tion can be the selec­tion of ran­dom num­bers or objects with­out rep­e­ti­tion, in that case there is always the prob­a­bil­i­ty of choos­ing a val­ue that had been cho­sen before, so it may be nec­es­sary to repeat the oper­a­tion until a new val­ue is found.

In a WHILE loop it is more com­mon to have infi­nite loops since the loop out­put con­di­tion depends on what we need in each par­tic­u­lar case.

Syntax of a WHILE LOOP

Giv­en a vari­able name "con­di­tion", which is a Boolean val­ue, for exam­ple a Boolean vari­able, the result of a log­i­cal oper­a­tion or the result of the exe­cu­tion of a pro­gram­ming method which returns a boolean val­ue, the syn­tax of a WHILE loop in C# is as follows

while(condition){
//Instruc­tions to be per­formed inside the loop

//"condition must change to false to exit the loop.
}

FOREACH Loop

A FOREACH loop is used to go through each one of the ele­ments of a col­lec­tion, for exam­ple an array or a list, the idea is that inside the loop we have the ref­er­ence of each one of the objects but with­out the need of an iter­a­tion variable.

As an appli­ca­tion exam­ple it can be any task that requires to tra­verse a col­lec­tion of ele­ments, for exam­ple giv­en a col­lec­tion of ene­my objects, apply to each of them a cer­tain dam­age by mag­ic. Anoth­er exam­ple can be to apply a cer­tain ini­tial con­fig­u­ra­tion to a set of elements.

A FOREACH loop does not usu­al­ly pro­duce infi­nite loops, unless there is some way to increase the num­ber of ele­ments of the col­lec­tion being tra­versed with­in the same loop, that would cause us to add a new ele­ment to tra­verse on each iter­a­tion and we would nev­er finish.

Syntax of a FOREACH LOOP

For this exam­ple we are going to con­sid­er any pro­gram­ming class that we will call "DataType", we are going to use the word "obj" to refer to each ele­ment with­in the col­lec­tion to be tra­versed and final­ly we are going to use a col­lec­tion of objects that we will call "col­lec­tion" and that will be of the same type as "DataType". The syn­tax of a FOREACH loop in C# is the following:

fore­ach( DataType obj in col­lec­tion){
// Instruc­tions to be per­formed inside the loop

//"obj" rep­re­sents each ele­ment of the col­lec­tion
}

The fol­low­ing exam­ple goes through a col­lec­tion of GameOb­jects that rep­re­sent ene­mies in a hypo­thet­i­cal game and pro­ceeds to destroy each of those objects.

foreach(GameObject g in ene­mies){
Destroy(g);
}

YouTube
Scroll to Top
Secured By miniOrange