A PHP Error was encountered

Severity: Notice

Message: Only variable references should be returned by reference

Filename: core/Common.php

Line Number: 257

สอน Swift ตอนที่ 4.5 Control Transfer Statements คำสั่ง break และ continue | Share

สอน Swift ตอนที่ 4.5 Control Transfer Statements คำสั่ง break และ continue

สอน Swift ตอนที่ 4.5 Control Transfer Statements คำสั่ง break และ continue

Control Transfer Statements คือ คำสั่งที่ใช้เปลี่ยนลำดับการทำงานของโค้ดที่เราเขียน ในภาษา Swift มี Control Transfer Statements อยู่ 5 แบบ ที่ใช้บ่อยที่สุดคงไม่พ้น break, continue

Sep 10, 2016

แก้ไขล่าสุด มีผู้อ่าน 6,724  |  PROGRAMMING SWIFT

Control Transfer Statements คือ คำสั่งที่ใช้เปลี่ยนลำดับการทำงานของโค้ดที่เราเขียน ในภาษา Swift มี Control Transfer Statements อยู่ 5 แบบ

  1. break
  2. continue
  3. fallthrough
  4. return
  5. throw

พื้นฐานของ Control Transfer Statements ที่ใช้บ่อยๆ คงไม่พ้น break, continue ในบทความนี้ เราจะมาเจาะลึกไปที่ 2 ตัวนี้กันครับ

 

Continue

continue ถูกใช้เพื่อบอกให้ loop หยุดและเริ่มต้นรอบใหม่อีกครั้ง ดังนั้น continue จึงถูกนำมาใช้กับ for - in , while, repeat - while

 

ตัวอย่างการใช้ Continue ภายใน Loop

var number = 1 
while number < 200 {
    if (number % 13 == 0){ // หาร 13 ลงตัว
        number = number + 1 
        continue // ข้ามไปทำงานรอบต่อไป
    }
    print("\(number) is not divisible by 13.")
    number = number + 1; 
}

เมื่อเราใส่ continue เข้าไป จากตัวอย่างเมื่อ number เป็นค่าที่ 13 ลงตัว continue จะทำงานและข้ามไปรอบถัดไปเลย โดยคำสั่งที่อยู่ด้านล่าง continue จะไม่ทำงานในรอบนั้น 

 

 

Break


break ถูกใช้เพื่อออกจาก loop หรือ ออกจาก switch 

ตัวอย่างการใช้ Break ภายใน Loop

var number = 1 
while number < 200 { 
    if ((number % 5 == 0) && (number % 7 == 0)) { // หาร 5 และ หาร 7 ลงตัว
        print("The first number divisible by both 5 and 7 is \(number)") 
        break // ออกจาก While
    } 
    number = number + 1
}

เมื่อเราใส่ break เข้าไป จากตัวอย่างเมื่อ number เป็นเลขที่ หารด้วย 5 และ 7 ลงตัว นั้นคือเลข 35 คำสั่ง break จะทำงานและออกจาก While Loop ทันที

ตัวอย่างการใช้ Break ภายใน Switch - Case

var number = 1
switch number {
case 1:
    print("number is 1")
    break
    print("number is positive")
case 2:
    print("number is 2")
default:
    print("number isn't range.")
}

จากตัวอย่าง คำสั่ง "number is positive" จะไม่ทำงาน เนื่องจากเจอ break แล้ว ออกจาก switch ก่อน

 


แบ่งปัน

ชอบ +1

บันทึก

ฝากคำถาม คำแนะนำ ได้ที่

Facebook : Share.OlanLab.Com
LINE ID : @olanlab
อีเมล์ : olan@olanlab.com

หลักสูตร

สอน Swift สร้างแอพพลิเคชั่นบน IOS, macOS, tvOs, watchOs

Swift ภาษาเขียนแอพพลิเคชั่นที่ใช้งานง่าย แต่ทรงพลังออกแบบสร้างสรรค์โดย Apple เพื่อใช้สร้างแอพพลิเคชั่นสำหรับ IOS, Mac, Apple TV และ Apple Watch
Share คลังความรู้ด้านเทคโนโลยี สารสนเทศ นวัตกรรมคอมพิวเตอร์ สอนเขียนโปรแกรม Php Java Html CSS Javascript C C++ Objective-C และอื่นๆ บนระบบปฏิบัติการ Window Linux Unix CentOS IOS Android
โดยผู้เขียนที่มีความเชี่ยวชาญเฉพาะ ให้บริการพื้นที่สำหรับนักเขียนที่ต้องการแบ่งปันความรู้ พร้อมให้คำปรึกษาแก่ผู้ที่สนใจ โดยไม่เสียค่าใช้จ่าย