3. Swift 运算条件循环.png

各种编程语言的运算符、条件语句、循环语句大同小异

运算符

可参考 JS 的说明:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators
注意运算符的优先级

算术运算符

算术运算符包括:+ - * / % ,但不包括: ++ --

比较运算符

比较运算符包括:> >= < <= == != === !==

逻辑运算符

逻辑运算符包括:&& || ! !!

位运算符

位运算符包括:& | ^ ~ << >> >>>

  1. /* Objective-C 左移枚举 */
  2. typedef NS_ENUM (NSInteger, DirectionType) {
  3. DirectionTypeLeft = 1 << 1,
  4. DirectionTypeRight = 1 << 2,
  5. DirectionTypeUp = 1 << 3,
  6. DirectionTypeDown = 1 << 4
  7. };
  8. DirectionType type = DirectionTypeLeft | DirectionTypeRight;
  9. NSLog(@"%ld", type & DirectionTypeLeft); // 2
  10. NSLog(@"%ld", type & DirectionTypeRight); // 4
  11. NSLog(@"%ld", type & DirectionTypeUp); // 0
  12. NSLog(@"%ld", type & DirectionTypeDown); // 0
  1. /* Swift 左移枚举 */
  2. struct Direction: OptionSet {
  3. let rawValue: Int
  4. static let Left = Direction(rawValue: 1 << 1)
  5. static let Right = Direction(rawValue: 1 << 2)
  6. static let Up = Direction(rawValue: 1 << 3)
  7. static let Down = Direction(rawValue: 1 << 4)
  8. }
  9. let direction: Direction = [.Left, .Up]
  10. print(direction.contains(.Left)) // true
  11. print(direction.contains(.Right)) // false
  12. print(direction.contains(.Up)) // true
  13. print(direction.contains(.Down)) // false

备注:[左移枚举1] [左移枚举2]

赋值运算符

赋值运算符包括:= += -= 等,也就是 “算术运算符”、”位运算符 与 “等号” 的组合

区间运算符

x...y[x, y] x..<y[x, y)

  1. for i in 1...5 {
  2. print(i)
  3. }

条件语句

if - else if - else

  1. let score = 70
  2. if score < 60 {
  3. print("不及格")
  4. } else if (score >= 60 && score <= 80) {
  5. print("良好")
  6. } else {
  7. print("优秀")
  8. }

switch - case - break

  1. var index = 10
  2. switch index {
  3. case 100:
  4. print( "index 的值为 100")
  5. break // 在 Swift 的 switch 语句中,break 默认隐式添加到 case 语句末尾,所以不必再添加了。
  6. case 10, 15:
  7. print( "index 的值为 10 或 15")
  8. fallthrough // 如果使用了fallthrough 语句,则会继续执行之后的 case 或 default 语句,不论条件是否满足都会执行。
  9. case 5: do {
  10. print( "index 的值为 5")
  11. } // 大语句块,使用 `do { /* code... */ }` 包裹
  12. break
  13. default:
  14. print( "默认 case")
  15. break
  16. }
  17. /*
  18. 输出:
  19. index 的值为 10 或 15
  20. index 的值为 5
  21. */

? : ??

Exp1 ? Exp2 : Exp3

  • 如果 Exp1 为真,则执行 Exp2
  • 如果 Exp1 为假,则执行 Exp3
  1. var age = 20
  2. var isAdult = age >= 18 ? "成年人" : "未成年"
  3. print(isAdult)

Value ?? DefaultValue

  • 如果 Value 不为 nil ,则返回 Value 值本身
  • 如果 Valuenil,则返回 DefaultValue 默认值
  1. var name: String?
  2. var thisName = name ?? "佚名"
  3. print(thisName)

循环语句

  1. for element in collection {
  2. statement
  3. }
  1. while condition {
  2. statement
  3. }
  1. repeat {
  2. statement
  3. } while condition

九九乘法表

  1. for i in 1...9 {
  2. for j in 1...i {
  3. print("\(j) x \(i) = \(i * j)", terminator: "\t")
  4. }
  5. print("")
  6. }
  1. 1 x 1 = 1
  2. 1 x 2 = 2 2 x 2 = 4
  3. 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
  4. 1 x 4 = 4 2 x 4 = 8 3 x 4 = 12 4 x 4 = 16
  5. 1 x 5 = 5 2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
  6. 1 x 6 = 6 2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
  7. 1 x 7 = 7 2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35 6 x 7 = 42 7 x 7 = 49
  8. 1 x 8 = 8 2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40 6 x 8 = 48 7 x 8 = 56 8 x 8 = 64
  9. 1 x 9 = 9 2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45 6 x 9 = 54 7 x 9 = 63 8 x 9 = 72 9 x 9 = 81

备注:[九九乘法表] [水平制表符]