Variable
มารู้จักกับตัวแปรกันหน่อยเผื่อใครไม่รู้ ถ้าตามทฤษฎีนั่นก็คือ
These are named containers that are used in storing data that can be accessed and manipulated by the computer
แปลง่ายๆ ก็คือชื่อที่เราประกาศเพื่อเก็บค่าอะไรบางอย่าง และสามารถเข้าถึงค่านั้นผ่านชื่อได้ โดยสามารถประกาศโดยใช้ keyword var
Constant
Constant คือ ค่าคงที่ ซึ่งเมื่อประกาศแล้วจะไม่มาสามารถเปลี่ยนแปลงค่าได้ วิธีการประกาศโดยใช้ keyword let
Data Types
ประเภทของข้อมูลก็จะเป็นตัวบอกว่าตัวแปรนั้นเก็บข้อมูลประเภทอะไร
- Integer: ตัวเลข เช่น 1, 2, -1, -2
- Double or Float: ทศนิยม เช่น 1.3, -4.2
- Boolean: True or False
- String: ชุดของ Charactor เช่น Hello, สวัสดี
Example
ตัวอย่างการประกาศตัวแปรและ Data Types
- Integers
var age: Int = 25
- Doubles and Floats
var pi: Double = 3.14159265359
var temperature: Float = 98.6
- Booleans
var isRaining: Bool = true
- Strings
var name: String = "John Smith"
ลองแสดงค่าออกมาด้วยคำสั่ง print
print(name)
ก็จะแสดงค่า John Smith
ออกมา