Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Mobile Programming Control Flow 2

Mobile Programming Control Flow 2

MobileProgrammingの授業で使うスライドです!

yuichiro_takahashi

October 15, 2018
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. ςΩετ FOR-INจ ▸ ܁Γฦ͠ॲཧΛ͢Δͱ͖ͷجຊతͳߏจ ▸ ߏจ͸ for <ม਺> in <Array

    or Range> { /* loop */ } ▸ Լهͷforͱ۠ผ͢ΔͨΊʹ for-inͱݺ͹Ε·͢ ▸ for (int i = 0; i <= 10, i++) { /* loop */ } ▸ ͷΑ͏ͳߏจ͸Swift 3.0 Ҏ߱ഇࢭ͞Ε·ͨ͠ ▸ ͜ͷΑ͏ͳforจΛC-style forͱݺͼ·͢ ▸ forจ͸ಛఆͷճ਺܁Γฦ͠ॲཧΛ͍ͨ͠ͱ͍͏Α͏ͳ৔߹ʹΑ͘࢖͍·͢
  2. ςΩετ ͳΜͰC-STYLE FOR ͸ফ͑ͨʁ ▸ ؆ܿʹ͍͏ͱSwiftΒ͘͠ͳ͍͔Β ▸ CݴޠܦݧऀҎ֎ʹ͸Θ͔Γʹ͍͘͠ ▸ ॳΊͯSwiftΛֶͿਓʹͱͬͯ͸֮͑ʹ͍͘

    ▸ ΠϯΫϦϝϯτɺσΫϦϝϯτʹΑΔ໰୊΋͋Δ͠ for (let i = 0; i <= 10; i++) { // loop... for (let j = 0; j <= 10;, i++) { print("it's a infinity loop!") } }
  3. ςΩετ RANGE(ൣғԋࢉࢠ)Λ࢖͏ ▸ Ұ൪؆୯ͳ΍Γํ ▸ switchͷ࣌ʹग़͖ͯͨΑ͏ʹ 0…10 ͱͯ͠ 0 ~

    10·Ͱͷ Sequence(Array)Λ࡞੒ ▸ ͦΕΛfor-inͱ૊Έ߹ΘͤΔ // Range let range = 0...10 for number in range { print("number: \(number)") }
  4. ςΩετ ARRAYΛ࢖͏ ▸ ͜Ε΋ඇৗʹΦʔιυοΫεͳ΍Γํ ▸ ఆٛࡁΈͷarrayʹରͯ͠for-inΛ࢖͏ ▸ indexͰΞΫηε͢Δํ๏ʹൺ΂ Array ͷൣғ֎ʹΞΫηε

    ͯ͠͠·͏͜ͱ͕ͳ͍ͨΊ҆શ // Array let array = ["sunday", "monday", "tuesday", "wednsday", "thursday", "friday", "Saturday"] for number in array { print("number: \(number)") }
  5. ςΩετ DICTIONARYΛ࢖͏ ▸ ͜Ε΋ׂͱΦʔιυοΫεͳ΍Γํ ▸ ఆٛࡁΈͷdictionaryʹରͯ͠for-inΛ࢖͏ ▸ key, value ͷܗʹͳ͍ͬͯΔͨΊ

    for ͷҾ਺͸ೋͭʹͳΔ // Dictionary let dic = ["Animal": "Cat", "Fruit": "Banana"] for (key, value) in dic { print("key: \(key)") print("value: \(value)") }
  6. ςΩετ WHILEจ ▸ ܁Γฦ͠ॲཧΛ͢Δͱ͖ͷجຊతͳߏจ ▸ ߏจ͸ while <ϧʔϓ৚݅> { /*

    loop */ } ▸ whileจ͸ಛఆͷ৚݅ʹϚον͢Δ·Ͱ܁Γฦ͠ॲཧΛͨ͠ ͍࣌ʹ࢖͏ var count = 0 var total = 0 while count < 5 { count += 1 total += count } print("count: \(count) total: \(total)")
  7. ςΩετ ੍ޚసૹ(CONTROL TRANSFER STATEMENTS)ʁ ▸ ੍ޚసૹ(Control Transfer Statements)͸ϓϩάϥϜͷॲཧ ΛΛ͋Δ৔ॴ͔Βผͷ৔ॴʹҠ͢ػೳͷ͜ͱ ▸

    ྫ͑͹continue ΍ break, switchͷfallthroughͳͲ͕͋Δ ▸ ؔ਺ͷreturn΍ Τϥʔॲཧʹग़ͯ͘Δ throw ΋ಉ͡ ▸ ࠓճ͸breakͱcontinueʹߜ͓ͬͯ࿩Λ͠·͢
  8. ςΩετ BREAK ▸ ଞͷݴޠಉ༷Swiftʹ΋ break ͕͋Γ·͢ ▸ ಛఆͷ৚݅ͷ࣌ʹϧʔϓ͔Βൈ͚͍ͨ৔߹ʹbreakΛ࢖༻͠ ·͢ for

    i in 0...10 { // 11ճϧʔϓͤ͞·͢ɻ if i >= 3 { // i ͕ 3 Ҏ্ʹͳͬͨͱ͖ʹൈ͚Δ break } print(i) }
  9. ςΩετ CONTINUE ▸ ଞͷݴޠಉ༷Swiftʹ΋ continue ͕͋Γ·͢ ▸ ಛఆͷ৚݅ͷ࣌ʹॲཧΛεΩοϓ͍ͨ͠৔߹͸continueΛ࢖ ༻͠·͢ ▸

    breakͱ͸ҧ͍loopΛൈ͚Δ͜ͱ͸͋Γ·ͤΜ for i in 0...10 { if i % 3 == 0 { // 3ͷഒ਺ͷ࣌͸εΩοϓ continue } print(i) }
  10. ςΩετ ϥϯμϜʹੜ੒ͨ͠਺஋ͷഒ਺ΛٻΊΔ // ফඅ੫཰
 let taxRate = 0.08 // ஋ஈͷϦετ

    let prices: [Double] = [10000, 9800, 34570, 29800, 114514] // ·ͣ͸ۭͷ഑ྻΛੜ੒ var newPrices = [Double]()
  11. ςΩετ ϥϯμϜʹੜ੒ͨ͠਺஋ͷഒ਺ΛٻΊΔ // ফඅ੫཰
 let taxRate = 0.08 // ஋ஈͷϦετ

    let prices: [Double] = [10000, 9800, 34570, 29800, 114514] // ·ͣ͸ۭͷ഑ྻΛੜ੒ var newPrices = [Double]()
 // for-inͰҰ౓഑ྻ͔ΒऔΓग़ͯ͠ɾɾɾ for price in prices { }
  12. ςΩετ ϥϯμϜʹੜ੒ͨ͠਺஋ͷഒ਺ΛٻΊΔ // ফඅ੫཰
 let taxRate = 0.08 // ஋ஈͷϦετ

    let prices: [Double] = [10000, 9800, 34570, 29800, 114514] // ·ͣ͸ۭͷ഑ྻΛੜ੒ var newPrices = [Double]()
 // for-inͰҰ౓഑ྻ͔ΒऔΓग़ͯ͠ɾɾɾ for price in prices { let newPrice = price * taxRate newPrices.append(newPrice) // ·ͨ഑ྻ΁ೖΕΔʂ }
  13. ςΩετ ߴ֊ؔ਺Λ࢖ͬͨ৔߹ ▸ ߴ֊ؔ਺(ࠓճ͸map)Λ࢖͏ͱ࣍ͷΑ͏ʹॻ͘͜ͱ͕Ͱ͖· ͢ // ফඅ੫཰
 let taxRate =

    0.08 // ஋ஈͷϦετ let prices: [Double] = [10000, 9800, 34570, 29800, 114514] let newPrices = prices.map { $0 * taxRate } // ׬੒ʂ
  14. ςΩετ ୹͍ʂ ▸ ߴ֊ؔ਺Λ࢖͏ͱͱͯ΋؆ܿʹ͔͚·͢ ▸ ࠓճ࢖ͬͨmapͱ͍͏ߴ֊ؔ਺͸഑ྻͷத਎ʹॱ൪ʹड͚ औͬͨॲཧΛద༻͠ɺ৽͍͠഑ྻΛੜ੒͢Δؔ਺Ͱ͢ ▸ { $0

    * taxRate } ͷ෦෼͕഑ྻͷத਎શͯʹద༻͞Εɺͦͷ݁ Ռ͕৽͍͠഑ྻͱͳΓ·͢ ▸ ߴ֊ؔ਺Λ࢖͏͜ͱͰ༨ܭͳίʔυΛল͖ɺͲΜͳॲཧΛ ͍ͨ͠ͷ͔ʁͱ͍͏෦෼ʹूதͰ͖·͢