swft 3 for c programmers : control flow
TRANSCRIPT
![Page 1: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/1.jpg)
Swift 3 : Control Flow
군산대학교 컴퓨터정보통신공학부 컴퓨터정보공학전공
남 광 우
Swift 3 Tour and Language Guide by Apple꼼꼼한재은씨의 Swift 2 프로그래밍
![Page 2: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/2.jpg)
Control Flow
• Loop Statements• for~ in ~ () {}• while () {}• repeat {} while()
•조건문 Conditional Statements• if ~ else ~• switch• guard ( ) else {}• #available
• Control Transfer Statements• break, continue• fallthrough• return
![Page 3: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/3.jpg)
흐름 제어 : For 구문
• for 구문의구조
• ; 생략가능
for( 초기값; 비교값; 증가값) {
{
for var i = 0; I < 10; i++ {
print( “ \(i) 번째실행“){
for (var i = 0; I < 10; i++){
print( “ \(i) 번째실행“);{
![Page 4: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/4.jpg)
For ~in ~구문
• for ~ in ~ 구문의형태
• for ~ in 구문에서 루프상수의생략• 아래에서변수 i는필요없음• 그러므로 _ 로대체가능
for i in 1…100{
print( “Hello” )}
for _ in 1…100{
print( “Hello” )}
for 루프상수 in 순회대상 {
실행할구문들
}
{ 가필수
![Page 5: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/5.jpg)
For 구문
• for~ in 문으로구현한 9*9단
for i in 1…9{
for j in 1…9{
print( “ \(i) * \(j) = \(i*j) “);}
}
( ) 치면에러
![Page 6: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/6.jpg)
For 구문
• 예 : String을한글자씩출력하기
var str = “Swfit”
for c in str.characters {print( c );
}
![Page 7: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/7.jpg)
For 구문
• 예 : 배열의내용을하나씩프린트하기
let names = ["Anna", "Alex", "Brian", "Jack"]
for name in names {print("Hello, \(name)!")
}
// Hello, Anna!// Hello, Alex!// Hello, Brian!// Hello, Jack!
![Page 8: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/8.jpg)
For 구문
• 예 : dictionary의내용을프린트하기
let numberOfLegs = ["spider": 8, "ant": 6, "cat": 4]
for (animalName, legCount) in numberOfLegs {print("\(animalName)s have \(legCount) legs")
}
// ants have 6 legs// spiders have 8 legs// cats have 4 legs
![Page 9: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/9.jpg)
While 구문
• C와동일• while 문으로구현한 9*9단
var i = 1;while ( i<=9 ) {var j = 1;while ( j <= 9 ){
print( “ \(i) * \(j) = \(i*j) “);j += 1;
}i += 1;
}
i++ deprecated됨
![Page 10: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/10.jpg)
repeat~ while 구문
• do~ while 구문의변경• do ~ try~ cactch와 keyword 겹침으로인해변경
• repeat~while
var n = 1024;repeat {
n = n* 2;} while n < 1000;
n// n== 2048
![Page 11: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/11.jpg)
if 구문
• c의구문과동일• 예 : 브라우저명을입력받아한글로변환
var browser = “Safari”var browserName : String;
if browser==“IE” {browserName = “인터넷익스플로러“;
} else if browser ==“FF” {browserName = “파이어폭스”;
}
![Page 12: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/12.jpg)
switch 구문
switch 비교대상 {case value 1:
value1의작업
case value 2, value 3:value2, value3의작업
case value4:fallthrough
case value5:value4, value5의작업
default:otherwise, do something else
}
• c의구문과유사, 그러나• break 없음• case 두개이상적용할경우 fallthrough• 비교대상은꼭하나이상의 case에match 해야함
• default 거의필수
![Page 13: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/13.jpg)
let someCharacter: Character = "z“
switch someCharacter {
case "a“, “A”:print("The first letter of the alphabet")
case "z":print("The last letter of the alphabet")
case “c”:fallthrough
case “d”:print(“c or d”);
default:print("Some other character")
}
// Prints "The last letter of the alphabet"
• 예 : switch 구문의사용예
switch 구문
없으면 error
break 없음
두개가능
c, d 두개다처리
![Page 14: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/14.jpg)
switch 구문• 예 : 범위연산의사용
let approximateCount = 62let countedThings = "moons orbiting Saturn"var naturalCount: String
switch approximateCount {case 0:naturalCount = "no"
case 1..<5:naturalCount = "a few"
case 5..<12:naturalCount = "several"
case 12..<100:naturalCount = "dozens of"
case 100..<1000:naturalCount = "hundreds of"
default:naturalCount = "many"
}
print("There are \(naturalCount) \(countedThings).")// Prints "There are dozens of moons orbiting Saturn."
![Page 15: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/15.jpg)
switch 구문• 예 : Tuple의사용과 value binding
let somePoint = (1, 2)
switch somePoint {case (0, 0):print("(0, 0) is at the origin")
case ( _, 0):print("(\(somePoint.0), 0) is on the x‐axis")
case (0, let y):print("(0, \( y )) is on the y‐axis")
case (‐2...2, ‐2...2):print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:print("(\(somePoint.0), \(somePoint.1)) is outside of the box")
}// Prints "(1, 1) is inside the box"
_ 을이용한모든값대응
(1,2) 튜플의 0번째값 1somePoint.1은 2
y로 somePoint.1을대체
![Page 16: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/16.jpg)
let anotherPoint = (2, 0)
switch anotherPoint {case (let x, 0):print("on the x‐axis with an x value of \(x)")
case (0, let y):print("on the y‐axis with a y value of \(y)")
case (5…10, 5…10):print(" x, y is between 5 and 10")
case let (x, y):print("somewhere else at (\(x), \(y))")
}
// Prints "on the x‐axis with an x value of 2"
switch 구문• 예 : Tuple의사용과 value binding/범위
![Page 17: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/17.jpg)
switch 구문
• Where 구문의사용• 예 : where 를이용한직선의기울기
let yetAnotherPoint = (1, ‐1)switch yetAnotherPoint {
case let (x, y) where x == y:print("(\(x), \(y)) is on the line x == y")
case let (x, y) where x == ‐y:print("(\(x), \(y)) is on the line x == ‐y")
case let (x, y):print("(\(x), \(y)) is just some arbitrary point")
}// Prints "(1, ‐1) is on the line x == ‐y"
![Page 18: Swft 3 for C Programmers : Control flow](https://reader036.vdocuments.pub/reader036/viewer/2022082723/5878bb941a28ab724c8b7827/html5/thumbnails/18.jpg)
let stillAnotherPoint = (9, 0)switch stillAnotherPoint {
case (let distance, 0), (0, let distance):print("On an axis, \(distance) from the origin")
default:print("Not on an axis")
}// Prints "On an axis, 9 from the origin"
switch 구문• 예 : 복합구문의사용