Skip to content
EspañolWorkspace →

PSeInt Mientras (while loop): how it works, with an example

Repeats a group of instructions while the condition stays true. It checks the condition before every round, so if it's already false at the start, nothing repeats.

Template
Mientras
Open it in Codoritmo

Opens the editor with this example loaded. Run it step by step and watch the flowchart.

Worked example

A complete algorithm, ready to copy and run.

mientras-while-loop.psc · 7 lines
Algoritmo Example    contador <- 1    Mientras contador <= 3 Hacer        Escribir contador        contador <- contador + 1    FinMientrasFinAlgoritmo

Output

Shows 1, 2 and 3, one per line.

This is what the console shows when the example runs.

123

Common mistake

Something inside must change the condition, or the loop never ends.

In a flowchart

This is how it appears in a flowchart, with the same shapes Codoritmo draws.

contador <= 3Sí…No
In the flowchart. Flowchart: a decision diamond with the condition "contador <= 3"; on Sí (yes) it runs the body and loops back to the condition, on No it leaves the loop.
Draw the flowchart of your own algorithm →

Template

The general form. Replace what's inside < > with your own data.

Mientras · 3 lines
Mientras <condicion> Hacer  <acciones>FinMientras

Examples that use it

Programs from the example library where it appears, from the simplest to the most complete.

  • Sum Until Zero

    Adds up the numbers you type until you enter a 0.

    Beginner
  • Guess the Number

    Guess a secret number from 1 to 100 in seven tries.

    Intermediate
  • Linear Search

    Finds a name in a list, ignoring upper and lower case.

    Intermediate
  • Bubble Sort

    Sorts random numbers by swapping neighbors, and stops as soon as they're in order.

    Intermediate
  • Insertion Sort

    Inserts each number among the sorted ones, like arranging cards in your hand.

    Intermediate
  • Binary Search

    Finds a value in a sorted array by halving the range every step.

    Intermediate
See every Mientras example →

More structures

Codoritmo is an independent, open-source project inspired by PSeInt and compatible with its syntax. It is not affiliated with the PSeInt project. Official PSeInt website.