PSeInt for loop (Para) exercises
24 examples
How it works
Para repeats a block a known number of times. A counter variable starts at one value, moves on every pass, and the loop ends once it goes past the final value: Para i <- 1 Hasta 10 Hacer makes ten passes.
Con Paso (step) changes how much the counter moves. Con Paso 2 counts in twos, and Con Paso -1 counts backwards, which is handy for countdowns or walking an array from the end.
Pick Para when you know the number of passes before you start (the 10 rows of a table, the n items of an array). If it depends on something that happens inside, use Mientras or Repetir.
Short example
The least you need to see it work. Copy it or open it in the editor.
Algoritmo TimesTable Definir n, i Como Entero Escribir "Which times table?" Leer n Para i <- 1 Hasta 5 Hacer Escribir n, " x ", i, " = ", n * i FinParaFinAlgoritmoWhat it prints:
Which times table?User answer: 77 x 1 = 77 x 2 = 147 x 3 = 217 x 4 = 287 x 5 = 35
Opens in the browser editor, nothing to install.
Solved exercises
Each exercise comes with the full code, its flowchart and a sample run.
Times Table
Prints the multiplication table for the number you choose.
BeginnerCountdown
A rocket launch countdown that ticks second by second.
BeginnerGrade Average
Stores five grades in an array and finds the average and the highest.
BeginnerName Initials
Gets the initials of a full name.
BeginnerFibonacci Sequence
Prints the first terms of the Fibonacci sequence.
BeginnerDrawing Shapes
Draws a square and a triangle with asterisks.
IntermediateStore Inventory
Records the stock of each product and flags what to restock.
IntermediateBubble Sort
Sorts random numbers by swapping neighbors, and stops as soon as they're in order.
IntermediateSelection Sort
Finds the smallest of the remaining numbers and puts it in its place.
IntermediateInsertion Sort
Inserts each number among the sorted ones, like arranging cards in your hand.
IntermediateCounting Sort
Counts how many times each value appears, without comparing numbers.
IntermediateLinear vs. Binary Search
Counts the comparisons each search needs on the same sorted list.
IntermediatePalindromes
Checks whether a phrase reads the same forwards and backwards.
IntermediateCount Vowels
Counts the vowels, spaces and other characters in a text.
IntermediateFactorial and Power
Functions that compute a factorial and a power.
IntermediatePrime Numbers
Finds every prime number up to a limit.
IntermediateCompound Interest
Watch your savings grow year by year.
IntermediateWeekly Sales
A table of sales by branch and day, with totals.
AdvancedShell Sort
Insertion sort between far-apart elements, with a gap that shrinks every pass.
AdvancedMerge Sort
Splits the array in halves, sorts each one recursively and merges them.
AdvancedHeap Sort
Arranges the array as a heap, then takes the largest out again and again.
AdvancedCaesar Cipher
Encrypts a message by shifting each letter of the alphabet.
AdvancedRoman Numerals
Converts a number from 1 to 3999 into Roman numerals.
AdvancedClass Grade Book
Records three grades per student and builds a class report.
Advanced
In the language reference
The instructions involved, explained one by one.
Other concepts
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.