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.
- Group:
- Structures
- Template
Mientras
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.
Algoritmo Example contador <- 1 Mientras contador <= 3 Hacer Escribir contador contador <- contador + 1 FinMientrasFinAlgoritmoOutput
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.
Template
The general form. Replace what's inside < > with your own data.
Mientras <condicion> Hacer <acciones>FinMientrasExamples 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.
BeginnerGuess the Number
Guess a secret number from 1 to 100 in seven tries.
IntermediateLinear Search
Finds a name in a list, ignoring upper and lower case.
IntermediateBubble Sort
Sorts random numbers by swapping neighbors, and stops as soon as they're in order.
IntermediateInsertion Sort
Inserts each number among the sorted ones, like arranging cards in your hand.
IntermediateBinary Search
Finds a value in a sorted array by halving the range every step.
Intermediate
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.