Skip to content
EspañolWorkspace →

PSeInt Para Cada (for each): walk an array without indexes

1 example

How it works

Para Cada walks an array from start to end. On every pass, the variable you choose takes the value of the next slot, so you need no counter and don't have to know how many slots there are.

It's ideal for adding up, printing or searching every element. If you need to know your position (to print "Student 3", say) or to walk only part of the array, use an indexed Para.

Some profiles don't allow Para Cada; there, the same walk is written as Para i <- 1 Hasta n Hacer.

Short example

The least you need to see it work. Copy it or open it in the editor.

for-each-para-cada.psc · 13 lines
Algoritmo AddPrices    Definir total Como Real    Dimension precios[4]    precios[1] <- 3.5    precios[2] <- 12    precios[3] <- 7.25    precios[4] <- 1    total <- 0    Para Cada precio De precios Hacer        total <- total + precio    FinPara    Escribir "Total: ", totalFinAlgoritmo

What it prints:

Total: 23.75
Open it in Codoritmo

Opens in the browser editor, nothing to install.

Solved exercises

Each exercise comes with the full code, its flowchart and a sample run.

  • Shopping List

    Adds items to a list that grows until you type FIN.

    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.