7 Control Flow and Looping

WHAT YOU WILL LEARN IN THIS CHAPTER:                              

  • How to make decisions using the If-Else statement
  • How to match numbers using the Switch statement
  • How to match characters using the Switch statement
  • How fallthroughs work in Swift
  • How to match a range of numbers using the Switch statement
  • How to match tuples using the Switch statement
  • How to perform value-binding using the Switch statement
  • How to use the Where clause in the Switch statement to indicate specific matches
  • How to perform looping using the For-In Loop
  • How to perform looping using the For Loop
  • How to use the Break and Continue control transfer statements

One of the most important aspects of a programming language is its ability to make decisions and perform repetitive tasks. In this aspect, Swift provides the usual flow control statements for making decisions through the use of the If-Else statement. For making multiple comparisons, Swift provides the Switch statement, which in addition to being similar to its counterpart in C, is much more powerful and flexible.

Swift also supports the C-style For and While loops and introduces the new For-In loop to iterate through arrays, dictionaries, and strings.

FLOW CONTROL

Swift primarily provides two types of statements for flow control: the If statement and the Switch statement.

If you have programmed before, you are familiar with the If statement. The If statement enables you to make a decision based on a certain condition(s). ...

Get Beginning Swift Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.