If statements in PSeInt: Si Entonces exercises
23 examples
How it works
A conditional lets the program do different things depending on the data. If the condition is true, one block runs; with SiNo (else) you say what to do when it's false. It all ends with FinSi.
The condition is a comparison that answers true or false: nota >= 60, edad < 18, respuesta = "yes". To require two things at once use Y (and); to accept one or the other, O (or).
When there are more than two cases (say, grades A, B, C or D) you put conditionals inside one another. If you always compare the same variable against fixed values, Segun is usually tidier.
Short example
The least you need to see it work. Copy it or open it in the editor.
Algoritmo EvenOrOdd Definir n Como Entero Escribir "Type a number:" Leer n Si n MOD 2 = 0 Entonces Escribir n, " is even" SiNo Escribir n, " is odd" FinSiFinAlgoritmoWhat it prints:
Type a number:User answer: 77 is odd
Opens in the browser editor, nothing to install.
Solved exercises
Each exercise comes with the full code, its flowchart and a sample run.
Can I Vote?
Tells you whether you can vote or how many years you have left.
BeginnerCoffee Shop Menu
Pick a drink from the menu and get the total to pay.
BeginnerSum Until Zero
Adds up the numbers you type until you enter a 0.
BeginnerBody Mass Index
Computes your BMI and classifies it as underweight, normal and so on.
IntermediateParking Fee
Charges every started hour, with a daily maximum.
IntermediateLeap Year
Applies the calendar rules to tell whether a year is a leap year.
IntermediateGuess the Number
Guess a secret number from 1 to 100 in seven tries.
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.
IntermediateBinary Search
Finds a value in a sorted array by halving the range every step.
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.
IntermediateBy Reference and by Value
Sorts three numbers by swapping them in subprocesses.
IntermediateQuadratic Equation
Solves ax² + bx + c = 0, with real or complex roots.
IntermediateShell 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.
AdvancedQuicksort
Picks a pivot, splits smaller from larger values and sorts each side recursively.
AdvancedHeap Sort
Arranges the array as a heap, then takes the largest out again and again.
AdvancedTowers of Hanoi
Solves the classic tower puzzle step by step.
AdvancedATM
Checks your PIN and lets you check, deposit and withdraw money.
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.
- Si Entonces (if)PSeInt Si Entonces (if statement): how to make a decision
- Si / SiNo (if-else)PSeInt Si SiNo (if-else): a decision with two paths
- Y (&&, and)PSeInt Y operator (and): when both conditions must hold
- O (||, or)PSeInt O operator (or): when one condition is enough
- Equal (=)Checking if two values are equal in PSeInt (=)
- Greater or equal (>=)Greater than or equal in PSeInt: the >= operator
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.