Chapter 4: Advanced Flow Control

大綱

Countable ranges

let closedRange = 0...5

let halfOpenRange = 0..<5

A Random Interlude

while Int.random(in: 1...6) != 6 {
  print("Not a six")
}

For loops

Switch statements

Key points

  • You can use ranges to create a sequence of numbers, incrementing to move from one value to another.

  • Closed ranges include both the start and end values.

  • Half-open ranges include the start value and stop one before the end value.

  • For loops allow you to iterate over a range.

  • The continue statement lets you finish the current iteration of a loop and begin the next iteration.

  • Labeled statements let you use break and continue on an outer loop.

  • You use switch statements to decide which code to run depending on the value of a variable or constant.

  • The power of a switch statement comes from leveraging pattern matching to compare values using complex rules.

Last updated