Published: 25 Jan 2019 › Updated: 25 Jan 2019![[Grammer]21 - Ternary Coditional Operator, 22 - Range Operators](/nhc-explorer-cover.webp)
![[Grammer]21 - Ternary Coditional Operator, 22 - Range Operators](/nhc-explorer-cover.webp)
[Grammer]21 - Ternary Coditional Operator, 22 - Range Operators
- 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]
Leave [Grammer]21 - Ternary Coditional Operator, 22 - Range Operators to:
Read more #swift4 posts
Best Posts From gunw
We have not curated any of gunw's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From gunw
- [Grammer] 26 - Custom Operators
- [Grammer] 25 - Operator Methods
- [Grammer] 23 - Bitwise Operators, 24 - Identity Operator
- [Grammer]21 - Ternary Coditional Operator, 22 - Range Operators
- [Grammer] 19 - Short-circuit Evaluation, Side Effect, 20 - Assignment Operators
- [Grammer] 16 - Overflow Operators, 17 - Comparison Operators, 18 - Logical Operators
- [Grammer] 13 - Data Types with Memory, 14 - Operator Basics, 15 - Arithmetic Operators
- [Grammer] 11 - Type Inference, Type Safety, 12 - Type Conversion, Type Aliases
- [Grammer] 09 - Boolean, 10 - Strings and Characters
- [Grammer] 07 - Naming Convention, 08 - Numbers