Skip to content
EspañolWorkspace →

PSeInt Segun (switch): choose between several options

Picks one path out of many depending on a variable's value, like a menu. Tidier than chaining many Si blocks when you compare the same variable with several values.

Template
Segun
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.

segun-switch.psc · 8 lines
Algoritmo Example    opcion <- 2    Segun opcion Hacer        1: Escribir "Play"        2: Escribir "Quit"        De Otro Modo: Escribir "Not an option"    FinSegunFinAlgoritmo

Output

With opcion 2 it shows "Quit".

This is what the console shows when the example runs.

Quit

Common mistake

De Otro Modo catches any value that isn't listed.

In a flowchart

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

opcion12De Otro Modo
In the flowchart. Flowchart: a multiple-choice diamond on "opcion" with three exits: 1, 2 and De Otro Modo (otherwise).
Draw the flowchart of your own algorithm →

Template

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

Segun · 6 lines
Segun <expresion> Hacer  <valor>:    <acciones>  De Otro Modo:    <acciones>FinSegun

Examples that use it

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

  • Coffee Shop Menu

    Pick a drink from the menu and get the total to pay.

    Beginner
  • Count Vowels

    Counts the vowels, spaces and other characters in a text.

    Intermediate
  • Modular Menu

    A unit converter with a menu that keeps coming back.

    Intermediate
  • ATM

    Checks your PIN and lets you check, deposit and withdraw money.

    Advanced
  • Contact Book

    Add, list and search contacts in a book that grows.

    Advanced
See every Segun 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.