programacion-bucle-while

The WHILE loop

  • 3 min

The WHILE loop is a control structure that repeats a block of code as long as a condition evaluates to true.

In natural language, the WHILE loop means,

While this is true 🡆 do this

Which in diagram form looks something like this:

programacion-bucle-while

WHILE Loop Diagram

In each iteration, the condition is checked:

  • If it is true, the code block is executed
  • If it is false, the loop stops and the program continues with the next instruction after the loop

In this way, the loop repeats as long as the condition is true (true).

The general syntax of the WHILE loop is as follows:

while(condition)
{
    // Block of code to execute while the condition is true
}

// rest of code
Copied!

The code block is executed repeatedly while the condition is true. If the condition evaluates to false from the beginning, the code block will not be executed at all.

Examples of WHILE loops in different languages

Let’s look at some examples of the WHILE loop syntax in different programming languages.

For example, in C, C++, C#, and Java a while loop looks like this:

int counter = 0;
while (counter < 10)
{
    // do something
    counter++;
}
Copied!

In the case of JavaScript the syntax is very similar. The only difference is that we will initialize the counter variable with let, instead of with the variable type. But the While syntax is identical:

let counter = 0;
while (counter < 10)
{
    counter++;
}
Copied!

Meanwhile, in Python, the syntax of the WHILE block is also very similar:

counter = 0
while counter < 10:    
    counter += 1
Copied!

In this case, to initialize counter it is not necessary to declare it in any way. Regarding the WHILE, Python defines blocks by significant indentation, rather than by braces. Also, it lacks the ++ operator, so we use +=1.

In this example, the WHILE loop executes while the counter variable is less than 10. In each iteration we would perform an action, which could use the value of counter.

After each iteration, the counter is incremented by 1. If we didn’t do this, the condition would never be met, and we would fall into an infinite loop.

Apart from the small syntax differences (which are even fun) we see that the concept and operation of the WHILE loop is identical in the different languages.

Internal Operation Advanced

Internally, the WHILE loop is formed by a conditional jump. Unlike a GO-TO instruction, conditional jumps perform a jump in code execution only if a condition is met.

If the condition is true, execution continues with the loop body. Upon reaching its end, the flow returns to the condition and is evaluated again.

programacion-bucle-while-saltos

Conditional jump generated by the WHILE

When the condition is not met, the conditional jump moves the execution flow outside the loop body, continuing with the program flow.

As we can see, the structure of a WHILE loop and an IF are very similar. The difference is that in the case of the loop, the jump in execution flow is “backwards”. This is what allows it to repeat the same code multiple times.