Selection: IF
This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · 이 페이지는 최신 브라우저(SharedArrayBuffer 지원)가 필요합니다. Chrome, Edge, Firefox 또는 Safari를 최신 버전으로 업데이트해 주세요.
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 Mark : INTEGER
Mark ← 72
IF Mark >= 50 THEN
OUTPUT "Pass"
ELSE
OUTPUT "Fail"
ENDIF
Explore · 탐색하기
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 N : INTEGER
N ← 7
IF N MOD 2 = 0 THEN
OUTPUT "even"
ELSE
OUTPUT "odd"
ENDIF
AND, OR and NOT
ANDis true only when both tests are true. A mark is in range when it is at least 0 and at most 100.ORis true when either test is true.NOTflipsTRUEandFALSE.- Write the variable on both sides.
Mark >= 0 AND Mark <= 100is the test.Mark >= 0 AND <= 100is not.
DECLARE Mark : INTEGER
Mark ← 80
IF Mark >= 0 AND Mark <= 100 THEN
OUTPUT "valid"
ELSE
OUTPUT "invalid"
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. ELSEbelongs to theIFabove it, not to a nested one, unless you indent it that way.
Now you try
- Read a number and choose with
IF…THEN…ELSE…ENDIF.
Read an INTEGER Mark. Output "Pass" when it is at least 50, otherwise "Fail".
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Read an INTEGER N. Output positive when it is greater than 0, otherwise not positive.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.
Read an INTEGER Mark. Output valid when it is from 0 to 100 inclusive, using AND. Otherwise output invalid.
Click Run to see the output here. · 출력을 보려면 '실행'을 클릭하세요.