Selection: IF · Selección: SI
IF makes a decision
- Selection chooses what to do, based on a test.
IFruns itsTHENblock only when the test isTRUE.- The block ends at
ENDIF.
IF toma una decisión
- Selección elige qué hacer, basándose en una prueba.
IFejecuta su bloqueTHENsolo cuando la prueba esTRUE.- El bloque termina con
ENDIF.
DECLARE Age : INTEGER
Age ← 20
IF Age >= 18
THEN
OUTPUT "You may vote"
ENDIF
IF ... ELSE
ELSEgives the path to take when the test isFALSE.- Exactly one of the two blocks runs.
IF ... ELSE
ELSEproporciona el camino a seguir cuando la prueba esFALSE.- Exactamente uno de los dos bloques se ejecuta.
DECLARE Mark : INTEGER
Mark ← 72
IF Mark >= 50
THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
TRUE or FALSE — which block runs? · VERDADERO o FALSO — ¿qué bloque se ejecuta?
IF runs its THEN block when the test is TRUE; ELSE covers every other case. · IF ejecuta su bloque THEN cuando la condición es TRUE; ELSE cubre todos los demás casos.
Comparison operators
=equal to,<>not equal to.<><=>=compare sizes.
Operadores de comparación
=igual a,<>no igual a.<><=>=comparan tamaños.
DECLARE N : INTEGER
N ← 7
IF MOD(N, 2) = 0
THEN
OUTPUT "even"
ELSE
OUTPUT "odd"
ENDIF
Watch out
- In a test,
=means "is equal to". (The arrow←is for storing a value.) - Don't forget
THENafter the test andENDIFto close the block.
Ten cuidado
- En una prueba,
=significa "es igual a". (La flecha←es para almacenar un valor.) - No olvides
THENdespués de la prueba yENDIFpara cerrar el bloque.
Common mistakes
- Write
IF ... THEN ... ELSE ... ENDIF— do not forgetENDIF. - Compare with
=in pseudocode conditions.
Errores comunes
- Escribe
IF ... THEN ... ELSE ... ENDIF— no olvidesENDIF. - Compara con
=en las condiciones del pseudocódigo.
Now you try
- Read the input, then choose with
IF ... ELSE ... ENDIF. - Press Check answer to test it.
Ahora tú intentas
- Lee la entrada, luego elige con
IF ... ELSE ... ENDIF. - Presiona Check answer para probarlo.
Read an INTEGER N. If it is greater than 0, output positive, otherwise output not positive. For input 5, output positive. · Lee un INTEGER N. Si es mayor que 0, imprime positive, de lo contrario imprime not positive. Para entrada 5, imprime positive.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Read a Mark. Output Pass if it is 50 or more, otherwise Fail. For input 40, output Fail. · Lee una Mark. Imprime Pass si es 50 o más, de lo contrario Fail. Para entrada 40, imprime Fail.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.
Read an INTEGER N. Output even if N MOD 2 = 0, otherwise odd. For input 8, output even. · Lee un INTEGER N. Imprime even si N MOD 2 = 0, de lo contrario odd. Para entrada 8, imprime even.
Click Run to see the output here. · Haz clic en Ejecutar para ver la salida aquí.