การเลือก: IF
หน้านี้ต้องใช้เบราว์เซอร์ล่าสุด (รองรับ SharedArrayBuffer) กรุณาอัปเดต Chrome, Edge, Firefox หรือ Safari เป็นเวอร์ชันล่าสุด
English
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.
English
DECLARE Age : INTEGER
Age ← 20
IF Age >= 18
THEN
OUTPUT "You may vote"
ENDIF
English
IF ... ELSE
ELSEgives the path to take when the test isFALSE.- Exactly one of the two blocks runs.
English
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.
English
Comparison operators
=equal to,<>not equal to.<><=>=compare sizes.
English
DECLARE Pupils : INTEGER
Pupils ← 23
IF MOD(Pupils, 4) = 0
THEN
OUTPUT "Teams of 4 work"
ELSE
OUTPUT MOD(Pupils, 4), " pupils left over"
ENDIF
English
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.
English
Common mistakes
- Write
IF ... THEN ... ELSE ... ENDIF— do not forgetENDIF. - Compare with
=in pseudocode conditions.
English
Now you try
- Read the input, then choose with
IF ... ELSE ... ENDIF. - Press Check answer to test it.
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read an INTEGER Number. If it is greater than 0, output positive, otherwise output not positive. For input 5, output positive.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read an INTEGER Mark. Output Pass if it is 50 or more, otherwise Fail. For input 40, output Fail.
คลิก Run เพื่อดูผลลัพธ์ที่นี่
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Read an INTEGER Number. Output even ถ้า MOD(Number, 2) = 0, otherwise odd. For input 8, output even.
คลิก Run เพื่อดูผลลัพธ์ที่นี่