Last active
March 27, 2016 05:14
-
-
Save gitbricho/3703f7cf498204f2831e to your computer and use it in GitHub Desktop.
swift:flow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
//## for-in | |
for i in 1...3 { | |
//3回繰り返し | |
} | |
// 配列の要素の数だけ繰り返して合計を求める | |
var v合計 = 0 | |
for num in [1,2,3,4,5] { | |
v合計 += num | |
} | |
print("合計は\(v合計)") //合計は15 | |
// べき演算 2**3 | |
let l底 = 2, l指数 = 3 | |
var v答え = 1 | |
// 繰り返しの回数だけが必要な場合 | |
for _ in 1...l指数 { | |
v答え *= l底 | |
} | |
print("答えは\(v答え)") //答えは8 | |
// 繰り替えしの数を数える場合 | |
for (index, item) in ["aa", "bb", "cc"].enumerate() { | |
print("インデックスが \(index) の要素は \(item)") | |
} | |
// (index, item) --> (0, "aa") ... (2, "cc") | |
// 役職手当計算 | |
// 以下の辞書データに対して給与と手当の合計を計算する | |
let l社員台帳: [String: (String, Int)] = [ | |
"山田":("部長",200000), | |
"太田":("課長", 150000), | |
"木村":("", 120000) | |
] | |
var v給与+手当: [String:Int] = [:] //空の辞書 | |
for (名前, var 給与) in l社員台帳 { | |
print("\(名前)の給与は\(給与.1)") | |
switch 給与.0 { | |
case "部長": | |
給与.1 += 50000 | |
break | |
case "課長": | |
給与.1 += 30000 | |
break | |
default: | |
break | |
} | |
v給与+手当[名前] = 給与.1 | |
} | |
print(v給与+手当) //["山田": 250000, "木村": 120000, "太田": 180000] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## while | |
var nums = [3,2,1] | |
var vカウンタ = nums.count | |
while vカウンタ > 0 { | |
//3回繰り返し | |
vカウンタ -= 1 | |
print(nums[vカウンタ]) | |
} | |
/* 結果: | |
1 | |
2 | |
3 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## repeat-while | |
repeat { | |
//3回繰り返し | |
print(nums[0]) | |
nums.removeFirst() | |
} while nums.count > 0 | |
/* 結果: | |
3 | |
2 | |
1 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## if | |
var v条件 = true | |
if v条件 { | |
//真の場合の処理 | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//## switch | |
for num in [2, 4, 5] { | |
switch num%2 { | |
case 0: | |
print("偶数") | |
case 1: | |
print("奇数") | |
default: | |
break | |
} | |
} | |
/* 結果: | |
偶数 | |
偶数 | |
奇数 | |
*/ | |
for 得点 in [99, 82, 33, 50] { | |
switch 得点 { | |
case 81...100: | |
print("A") | |
case 61...80: | |
print("B") | |
case 41...60: | |
print("C") | |
default: | |
print("D") | |
} | |
} | |
/* 結果: | |
A | |
A | |
D | |
C | |
*/ | |
// タプル | |
let l要求技能有無 = (java: true, mysql: true, jpa: false) | |
switch l要求技能有無 { | |
case (true, true, true): | |
print("プロジェクトA") | |
case (true, true, false): | |
print("プロジェクトB") | |
default: | |
print("該当プロジェクトなし") | |
} | |
// 結果:プロジェクトB | |
// where | |
let l要求技能レベル = (java: 80, mysql: 40, jpa: 30) | |
switch l要求技能レベル { | |
case let (java, mysql, jpa) | |
where java >= 80 && mysql >= 80 && jpa >= 50: | |
print("プロジェクトA") | |
case let (java, mysql, jpa) | |
where mysql > 50 && jpa > 50: | |
print("プロジェクトB") | |
case let (java, mysql, jpa) | |
where mysql > 30: | |
print("プロジェクトC") | |
default: | |
print("該当プロジェクトなし") | |
} | |
// 結果:プロジェクトC | |
// ### (注意) ### | |
// swift では、break がなくても単一のケースしか実行されない | |
// 複数ケースを実行したい場合は fallthrough を書く | |
let lケース = "A" | |
switch lケース { | |
case "A": | |
print("A") | |
fallthrough | |
case "B": | |
print("B") | |
default: break | |
} | |
/* 結果: | |
A | |
B | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment