Variables, DECLARE and CONSTANT · ตัวแปร, DECLARE และ CONSTANT
A variable stores a value
- A variable holds a value you can use and change.
- The arrow
←puts a value into a variable (this is assignment). - On your keyboard, type
<-(less-than, minus) — it means the same as←.
ตัวแปรเก็บค่า
- ตัวแปร เก็บค่าที่คุณสามารถใช้และเปลี่ยนแปลงได้
- ลูกศร
←วางค่าลงในตัวแปร (นี่คือ การกำหนดค่า), - ที่คีย์บอร์ดของคุณ พิมพ์
<-(เครื่องหมายน้อยกว่า,符ลบ) — มันมีความหมายเดียวกับ←
Age ← 16
OUTPUT Age // → 16
OUTPUT "Age" // → Age
DECLARE names the type
DECLAREstates a variable's name and its data type.- It is good style to declare a variable before you use it.
DECLARE ใช้ระบุประเภท
DECLAREระบุชื่อตัวแปรและ ประเภทข้อมูล ของมัน- เป็นธรรมเนียมที่ดีในการประกาศตัวแปรก่อนนำไปใช้
DECLARE Count : INTEGER
Count ← 0
OUTPUT Count // → 0
You can change a variable
- Assign again to give a variable a new value.
- The right-hand side is worked out first, then stored.
คุณสามารถเปลี่ยนค่าตัวแปรได้
- กำหนดค่าใหม่เพื่อให้ตัวแปรมีค่าใหม่
- ด้านขวาของสัญลักษณ์จะถูกคำนวณ ก่อน แล้วจึงเก็บผลลัพธ์
Total ← 10
Total ← Total + 5
OUTPUT Total // → 15
Watch the value change · ดูการเปลี่ยนแปลงของค่า
← puts a value into a box. Assign again, and the box holds the new value. · ← ใส่ค่าลงในกล่อง เมื่อกำหนดค่าใหม่ กล่องจะเก็บค่าใหม่แทน
A constant does not change
- A constant is a name for a value the program must not change, such as a pass mark or an hourly rate.
- The 2026–2028 syllabus writes
CONSTANT HourlyRate ← 6.50— the arrow, then a literal only. - If the number changes next year, you edit that one line and every use of the name follows.
ค่าคงที่เปลี่ยนแปลงไม่ได้
- ค่าคงที่ คือชื่อที่ใช้แทนค่าที่โปรแกรมห้ามเปลี่ยน เช่น คะแนนผ่านหรืออัตราต่อชั่วโมง
- หลักสูตรปี 2026–2028 เขียน
CONSTANT HourlyRate ← 6.50—ลูกศร ตามด้วยค่าลiterals เท่านั้น - หากตัวเลขเปลี่ยนในปีหน้า คุณแก้ไขแค่บรรทัดเดียว และทุกการใช้งานของชื่อนั้นจะตามไป
CONSTANT PassMark ← 50
OUTPUT PassMark // → 50
Watch out
←(or<-) means store into. It is not the=used to test equality.- A variable keeps its last value until you assign a new one.
OUTPUT Ageprints the number in the box.OUTPUT "Age"prints the word Age.
ระวัง
←(หรือ<-) หมายถึง เก็บค่าลงใน ไม่ใช่=ที่ใช้สำหรับ ทดสอบ ความเท่ากัน- ตัวแปรจะคงค่าล่าสุดไว้จนกว่าคุณจะกำหนดค่าใหม่
OUTPUT Ageจะพิมพ์ตัวเลขในกล่องOUTPUT "Age"จะพิมพ์คำว่า Age
Common mistakes
DECLARE <identifier> : <data type>before you use a variable.- A constant is
CONSTANT <identifier> ← <value>. Assign a variable with←.
ข้อผิดพลาดที่พบบ่อย
DECLARE <identifier> : <data type>ก่อนที่คุณจะใช้ตัวแปร- ค่าคงที่เป็น
CONSTANT <identifier> ← <value>กำหนดค่าตัวแปรด้วย←
Now you try
- Type
<-for the←arrow. - Press Run to see the output, then Check answer.
ลองดูเลย
- พิมพ์
<-สำหรับลูกศร← - กด Run เพื่อดูผลลัพธ์ จากนั้นกด Check answer
Put 16 into a variable called Age, then OUTPUT it. (Type <- for the ← arrow.) · ใส่ 16 ลงในตัวแปรชื่อ Age จากนั้น OUTPUT ตัวแปรนั้น (พิมพ์ <- สำหรับลูกศร ←)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
DECLARE a variable Price as INTEGER, set it to 3, then OUTPUT Price * 4 (the answer is 12). · DECLARE ตัวแปร Price เป็น INTEGER, ตั้งค่าเป็น 3, แล้ว OUTPUT Price * 4 (คำตอบคือ 12)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Set Total to 10, then add 5 to it, then OUTPUT Total (the answer is 15). · กำหนดให้ Total มีค่าเท่ากับ 10, จากนั้นบวก 5 เข้าไป, แล้ว OUTPUT Total (คำตอบคือ 15)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Declare a constant PassMark with the value 50, using ←, then OUTPUT it. · ประกาศค่าคงที่ PassMark ที่มีค่า 50 โดยใช้ ← แล้ว OUTPUT ค่าดังกล่าว.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่