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,

As long as this is true 🡆 do this

Which in diagram form looks something like this:

programacion-bucle-while

WHILE Scheme

In 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

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

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

Examples of WHILE loops in different languages

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

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

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

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 syntax of the While is identical:

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

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

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

In this case, it is not necessary to declare counter in any way to start it. Regarding the WHILE, Python defines blocks by significant indentation, instead of by braces. Additionally, 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 that 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.

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

Internal operation Advanced

At an internal level, the WHILE loop consists of a conditional jump. Unlike a GO-TO statement, conditional jumps perform a jump in code execution only if a condition is met.

If the condition is true, execution continues with the body of the loop. Upon reaching the end of this, the flow goes back to the condition and is re-evaluated.

programacion-bucle-while-saltos

Conditional jump generated by the WHILE

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

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