[Grammer]21 - Ternary Coditional Operator, 22 - Range Operators

in swift4 •  6 years ago 
  • Ternary Coditional Operator :
//condition ? expr1 : expr2
let condition = false
let result = condition ? "eat" : "sleep"
  • Range Operators
    a...b : Closed Range Operator
    a..<b : Half-open Range Operator
// 내림차순 구현
let range = 0...7
range.reversed()
  • One-Sided Ranges
    a..<b : Two-Sided
    ..<b : One-Sided
let r1 = 0...7
let r2 = 0..<7

for r in r1 {
   r
} 
// 0 1 2 3 4 5 6 7

for r in r2 {
   r
} 
// 0 1 2 3 4 5 6
var sum = 0
for i in 1...100 {
   sum += i
}
// sum = 5050

let list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

var result = list[0...5]
// [0, 1, 3, 4, 5]
result = list[..<5]
// [0, 1, 2, 3, 4]
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!