SlideShare a Scribd company logo
1 of 37
制御構文2
MOBILEPROGRAMMING OCT, 15TH
テキスト
制御構文!
▸ ifやfor-inなど分岐や繰り返し処理を特定の文に構造化したもの
▸ プログラムの構造は 順次、選択、反復の三つに分類される
▸ これらを組み合わせてプログラミングする方法を構造化プログラ
ミングと呼ぶ
▸ 今回は繰り返しの構文に絞ってやって行きます
▸ 言語によってはforやwhileという構文が存在しない(or ただのシ
ンタックスシュガー)
1. FOR-IN
テキスト
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文は特定の回数繰り返し処理をしたいというような場合によく使います
テキスト
なんで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!")
}
}
ではどうやって
ループさせる?🤔
1-1.RANGE
テキスト
RANGE(範囲演算子)を使う
▸ 一番簡単なやり方
▸ switchの時に出てきたように 0…10 として 0 ~ 10までの
Sequence(Array)を作成
▸ それをfor-inと組み合わせる
// Range
let range = 0...10
for number in range {
print("number: (number)")
}
テキスト
FOR-IN STLIDE
▸ for-inと stlide を使用することで間隔を開けてループさせる
ことができます
// 0から10まで二つとびでループ
for number in stride(from: 0, to: 10, by: 2) {
print("number: (number)")
}
1-2. ARRAY
テキスト
ARRAYを使う
▸ これも非常にオーソドックスなやり方
▸ 定義済みのarrayに対してfor-inを使う
▸ indexでアクセスする方法に比べ Array の範囲外にアクセス
してしまうことがないため安全
// Array
let array = ["sunday", "monday", "tuesday", "wednsday", "thursday",
"friday", "Saturday"]
for number in array {
print("number: (number)")
}
1-3.DICTIONARY
テキスト
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)")
}
2. WHILE
テキスト
WHILE文
▸ 繰り返し処理をするときの基本的な構文
▸ 構文は while <ループ条件> { /* loop */ }
▸ while文は特定の条件にマッチするまで繰り返し処理をした
い時に使う
var count = 0
var total = 0
while count < 5 {
count += 1
total += count
}
print("count: (count) total: (total)")
3. 制御転送
テキスト
制御転送(CONTROL TRANSFER STATEMENTS)?🤔
▸ 制御転送(Control Transfer Statements)はプログラムの処理
ををある場所から別の場所に移す機能のこと
▸ 例えばcontinue や break, switchのfallthroughなどがある
▸ 関数のreturnや エラー処理に出てくる throw も同じ
▸ 今回はbreakとcontinueに絞ってお話をします
3-1.BREAK
テキスト
BREAK
▸ 他の言語同様Swiftにも break があります
▸ 特定の条件の時にループから抜けたい場合にbreakを使用し
ます
for i in 0...10 { // 11回ループさせます。
if i >= 3 { // i が 3 以上になったときに抜ける
break
}
print(i)
}
3-2. CONTINUE
テキスト
CONTINUE
▸ 他の言語同様Swiftにも continue があります
▸ 特定の条件の時に処理をスキップしたい場合はcontinueを使
用します
▸ breakとは違いloopを抜けることはありません
for i in 0...10 {
if i % 3 == 0 { // 3の倍数の時はスキップ
continue
}
print(i)
}
おまけ
テキスト
FORやWHILEを使わない?🤔
▸ 冒頭で少しお話した通り言語によってはforやwhileが存在し
ないです
▸ ではどうやって繰り返しを表現するのでしょうか?
▸ 方法はいくつかあり一つは高階関数を使う方法
▸ もう一つは再帰関数を使う方法です
おまけ: 高階関
数
テキスト
高階関数?🤔
▸ 関数を引数にとったり、関数を返す関数
▸ ややこしいので今は詳しくは説明しません
▸ 繰り返しのための有名どころでは map, や forEach などがあ
ります
▸ for-inを使う方法より簡潔に書くことができます
テキスト
たとえば?🤔
▸ 値段のリストに対して消費税をかけた後の値段のリストを生
成する例を考えます
▸ これをfor-inで書くと次のようになります
テキスト
ランダムに生成した数値の倍数を求める
// 消費税率

let taxRate = 0.08
// 値段のリスト
let prices: [Double] = [10000, 9800, 34570, 29800, 114514]
// まずは空の配列を生成
var newPrices = [Double]()
テキスト
ランダムに生成した数値の倍数を求める
// 消費税率

let taxRate = 0.08
// 値段のリスト
let prices: [Double] = [10000, 9800, 34570, 29800, 114514]
// まずは空の配列を生成
var newPrices = [Double]()

// for-inで一度配列から取り出して・・・
for price in prices {
}
テキスト
ランダムに生成した数値の倍数を求める
// 消費税率

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) // また配列へ入れる!
}
控えめに言って
面倒😩
テキスト
高階関数を使った場合
▸ 高階関数(今回はmap)を使うと次のように書くことができま
す
// 消費税率

let taxRate = 0.08
// 値段のリスト
let prices: [Double] = [10000, 9800, 34570, 29800, 114514]
テキスト
高階関数を使った場合
▸ 高階関数(今回はmap)を使うと次のように書くことができま
す
// 消費税率

let taxRate = 0.08
// 値段のリスト
let prices: [Double] = [10000, 9800, 34570, 29800, 114514]
let newPrices = prices.map { $0 * taxRate } // 完成!
短い!😳
テキスト
短い!😳
▸ 高階関数を使うととても簡潔にかけます
▸ 今回使ったmapという高階関数は配列の中身に順番に受け
取った処理を適用し、新しい配列を生成する関数です
▸ { $0 * taxRate } の部分が配列の中身全てに適用され、その結
果が新しい配列となります
▸ 高階関数を使うことで余計なコードを省き、どんな処理を
したいのか?という部分に集中できます
ちょっと待って!
(TWITTER並感)
そもそも関数って
なんだっけ?🤔

More Related Content

Recently uploaded

LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイスCRI Japan, Inc.
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。iPride Co., Ltd.
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。iPride Co., Ltd.
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video UnderstandingToru Tamaki
 
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptxsn679259
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Gamesatsushi061452
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルCRI Japan, Inc.
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...Toru Tamaki
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。iPride Co., Ltd.
 
Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsWSO2
 

Recently uploaded (10)

LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
LoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイスLoRaWANスマート距離検出センサー  DS20L  カタログ  LiDARデバイス
LoRaWANスマート距離検出センサー DS20L カタログ LiDARデバイス
 
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その32024/04/26の勉強会で発表されたものです。
 
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
Amazon SES を勉強してみる その22024/04/26の勉強会で発表されたものです。
 
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
論文紹介:Selective Structured State-Spaces for Long-Form Video Understanding
 
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
知識ゼロの営業マンでもできた!超速で初心者を脱する、悪魔的学習ステップ3選.pptx
 
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
論文紹介: The Surprising Effectiveness of PPO in Cooperative Multi-Agent Games
 
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアルLoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
LoRaWAN スマート距離検出デバイスDS20L日本語マニュアル
 
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
論文紹介:Video-GroundingDINO: Towards Open-Vocabulary Spatio-Temporal Video Groun...
 
新人研修 後半 2024/04/26の勉強会で発表されたものです。
新人研修 後半        2024/04/26の勉強会で発表されたものです。新人研修 後半        2024/04/26の勉強会で発表されたものです。
新人研修 後半 2024/04/26の勉強会で発表されたものです。
 
Utilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native IntegrationsUtilizing Ballerina for Cloud Native Integrations
Utilizing Ballerina for Cloud Native Integrations
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Mobile programming control_flow2