เรียนเขียน iOS App ด้วยภาษา Swift — ตอนที่ 6 : การวนซ้ำ Loops (for-in, while)

ODENZA
2 min readDec 19, 2020

--

Loops เป็นการทำงานแบบซ้ำๆ

หากเราต้องการรันคำสั่ง print(“Hello Swift”) สัก 100 ครั้ง มันก็คงไม่ดีแน่ๆ ที่เราจะเขียนคำสั่งนี้ถึง 100 บรรทัด ก็เลยมีตัวช่วยที่ชื่อว่า loop นี่แหละมาช่วยให้ code เราสั้นลงได้

Loops มีหลักๆอยู่ 2 แบบ ดังนี้

  • for-in Loops
  • While Loops

For-In Loops

เราใช้ for-in loop ในการรันคำสั่งการทำงานซ้ำๆ ในชุดข้อมุลที่เป็น array, ranges of numbers หรือ characters ใน string (ใช้กับสมาชิก collections เป็นหลัก)

รูปแบบ :

for member in collection types {
do something with member
}

ตัวอย่างการใช้ for-in กับ ข้อมูลที่เป็น array

let names = [“Anna”, “Alex”, “Brian”, “Jack”]for name in names {
print(“Hello, \(name)!”)
}
// Hello, Anna!
// Hello, Alex!
// Hello, Brian!
// Hello, Jack!

เรายังสามารถใช้ for-in กับ dictionary ได้ โดยการเข้าถึงข้อมูลสมาชิก (key, value)

let numberOfLegs = [“spider”: 8, “ant”: 6, “cat”: 4]for (animalName, legCount) in numberOfLegs {
print(“\(animalName)s have \(legCount) legs”)
}
// cats have 4 legs
// ants have 6 legs
// spiders have 8 legs

for-in กับ ranges of numbers

for index in 1...12 {
print("\(index) x 2 = \(index * 2)")
}
/*
Result:
1 x 2 = 2
2 x 2 = 4
3 x 2 = 6
4 x 2 = 8
5 x 2 = 10
6 x 2 = 12
7 x 2 = 14
8 x 2 = 16
9 x 2 = 18
10 x 2 = 20
11 x 2 = 22
12 x 2 = 24
*/

While Loops

เป็นการทำงานนซ้ำๆโดยอาศัยเงื่อนไขหรือ condition ที่เป็นจริง หากเงื่อนไขนั้นเป็นจริง ก็จะทำงานนั้นซ้ำๆ จนกว่าเงื่อนไขนั้นจะถูกเปลี่ยนเป็น false

รูปแบบของ while loop :

while (condition) {   // statements}

รูปแบบการทำงานของ while loop ก็คือ

ถ้า condition มีค่าเป็น true ก็จะทำงานกับคำสั่งที่อยู่ในปีกกาหรือ statements นั้นๆ

และถ้า condition นั้นมีค่าเป็น false ก็จะสิ้นสุดการทำงานของ loop นั้นทันที

Example :

Repeat while loop

การทำงานของ Repeat while loop ก็จะคล้ายๆกับ while loop เลย แต่จะต่างกันตรงที่ Repeat while loop จะทำ statement ก่อนโดยไม่สนใจ condition ว่าเป็น true or false

รูปแบบของ repeat…while loop :

repeat {   // statements} while (condition)

รูปแบบการทำงานของ repeat…while loop ก็คือ

  • เริ่มทำงานตรง statement ก่อน (ก่อนที่จะทำการตรวจสอบค่า condition) หลังจากนั้นก็ตรวจสอบค่า condition
  • ถ้า Condition เป็น true ก็จะวนกลับมาทำงานในส่วนของ statement อีกครั้ง แบบนี้ไปเรื่อยๆ
  • ถ้า condition = false ก็จะหยุดการทำงาน

Infinite while loop

ถ้า condition = true และไม่ถูกเปลียนค่าให้เป็น false การทำงานนั้นก็จะทำงานไปเรื่อยๆแบบไม่มีที่สิ้นสุด

//แบบที่ 1while (true) {
print(“Hello, World!”)
}
//แบบที่ 2
repeat {
print(“Hello, World!”)
} while (true)

เมื่อลองรันโปรแกรมแล้ว ผลลัพธ์ที่ได้ (เหมือนกันทั้งสองแบบ) คือ :

Hello, World!
Hello, World!
Hello, World!
.
.
.

สรุป

  • for-in ใช้กับสมาชิก collections เป็นหลัก
  • while loop เช็ค condition ก่อน run
  • repeat…while loop : run คำสั่งก่อน 1 ครั้ง แล้วค่อยไปเช็ค condition อีกที ถ้าเป็น true ก็ run ต่อ ถ้าไม่ก็เลิกทำงาน

เรียนเขียน iOS App ด้วยภาษา Swift แบบพื้นฐาน เรียนด้วยตัวเอง

ตอนที่ 1 : การประกาศตัวแปร (variable & constant)
https://bit.ly/3nFVBCc

ตอนที่ 2 : ชนิดของตัวแปร (Data Types)
https://bit.ly/3h2N4Xb

ตอนที่ 3 : Collection Types (Arrays, Dictionaries and Sets)
https://bit.ly/3pkJfzR

ตอนที่ 4: Operators ( การใช้เครื่องหมายทางคณิตศาสตร์)
https://bit.ly/2WrTcyW

ตอนที่ 5 : การทำงานแบบมีเงื่อนไข (Conditional statements)
https://bit.ly/3pe8lQR

ตอนที่ 6 : การวนซ้ำ Loops (for-in, while)
https://bit.ly/3h1aNae

ตอนที่ 7 : Functions (ฟังก์ชัน)
https://bit.ly/3rbAc5O

--

--

ODENZA
ODENZA

No responses yet