Language: EN

programacion-bucle-while

The WHILE loop

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

In natural language, the WHILE loop means,

While this is true -> do this

Which in diagram form looks like this:

programacion-bucle-while

On each iteration, the condition is checked:

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

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 the code

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

Examples of WHILE loops in different programming languages

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

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

int contador = 0;
while (contador < 10)
{
	// do something
    contador++;
}

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

let contador = 0;
while (contador < 10)
{
    contador++;
}

For its part in Python, the syntax of the WHILE loop is also very similar:

contador = 0
while contador < 10:    
    contador += 1

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

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

In this case, to start the counter it is not necessary to declare it in any way. Regarding the WHILE loop, Python defines blocks by significant indentation, instead of by braces. It also lacks the ++ operator, so we use +=1.

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

Internal operation Advanced

Internally, the WHILE loop is made up of a conditional jump. Unlike a GO-TO statement, conditional jumps make a jump in the code execution only if a condition is met.

Therefore, our code would reach a conditional jump. If the condition is true, the execution continues with the loop body. Upon reaching the end of this, the flow returns to the condition and is evaluated again.

programacion-bucle-while-saltos

When the condition is not met, the conditional jump moves the execution flow out of 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 flow of execution jump is “backward”. This is what allows it to repeat the same code several times.