Seleção: IF
IF … THEN on one line
- Selection chooses what to run.
- The 2026 guide puts
THENon the same line as the condition. - The block ends at
ENDIF.
DECLARE Age : INTEGER
Age ← 20
IF Age >= 18 THEN
OUTPUT "You may vote"
ENDIF
ELSE is the other path
ELSEruns when the condition isFALSE.- Exactly one of the two blocks runs.
- Nested
IFstatements keep the same shape.
DECLARE Speed : INTEGER
Speed ← 64
IF Speed > 60 THEN
OUTPUT "Slow down"
ELSE
OUTPUT "OK"
ENDIF
TRUE or FALSE — which block runs?
IF runs its THEN block when the test is TRUE; ELSE covers every other case.
Relational operators
=equal,<>not equal,<><=>=.AND,ORandNOTare logic operators. Their operands areBOOLEAN.
DECLARE Number : INTEGER
Number ← 7
IF Number MOD 2 = 0 THEN
OUTPUT "even"
ELSE
OUTPUT "odd"
ENDIF
AND, OR and NOT
ANDis true only when both tests are true. A shop is open when the hour is at least 9 and at most 17.ORis true when either test is true.NOTflipsTRUEandFALSE.- Write the variable on both sides.
Hour >= 9 AND Hour <= 17is the test.Hour >= 9 AND <= 17is not.
DECLARE Hour : INTEGER
Hour ← 14
IF Hour >= 9 AND Hour <= 17 THEN
OUTPUT "open"
ELSE
OUTPUT "closed"
ENDIF
Watch out
THENstays on theIFline. IGCSE 0478 putsTHENon the following line. Do not mix them.- In a test,
=means "is equal to". Storing a value still uses←. "Pass"and"Fail"need quotes.OUTPUT Passlooks up a name called Pass.
Common mistakes
- Every
IFneeds anENDIF. - An
ELSEbelongs to the nearestIFthat has not reached itsENDIFyet. Indentation shows this to the reader; theENDIFs decide it.
Now you try
- Read a number and choose with
IF…THEN…ELSE…ENDIF.
Independent transfer
The final task has no supplied algorithm. Plan its inputs, processing and outputs, then solve it before opening support. Check more than the sample case.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Read an INTEGER Mark. Output "Pass" when it is at least 50, otherwise "Fail".
Clique em Executar para ver a saída aqui.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Read an INTEGER Number. Output positive when it is greater than 0, otherwise not positive.
Clique em Executar para ver a saída aqui.
Dificuldade 1/5 · Moedas 100 + pequeno bônus na primeira conclusão hoje
Read an INTEGER Mark. Output valid when it is from 0 até 100 inclusive, using AND. Otherwise output invalid.
Clique em Executar para ver a saída aqui.
Dificuldade 3/5 · Moedas 200 + pequeno bônus na primeira conclusão hoje
Read integer LengthValue. Output allowed for the inclusive range 5 to 12, otherwise refused.
Clique em Executar para ver a saída aqui.