Updated for Xcode 15
We’ve covered a lot about conditions and loops in the previous chapters, so let’s recap:
if
statements to check a condition is true. You can pass in any condition you want, but ultimately it must boil down to a Boolean.else
block, and/or multiple else if
blocks to check other conditions. Swift executes these in order.||
, which means that the whole condition is true if either subcondition is true, or &&
, which means the whole condition is true if both subconditions are true.switch
statement instead. These must always be exhaustive, which might mean adding a default case.switch
cases uses fallthrough
, it means Swift will execute the following case afterwards. This is not used commonly.for
loops let us loop over arrays, sets, dictionaries, and ranges. You can assign items to a loop variable and use it inside the loop, or you can use underscore, _
, to ignore the loop variable.while
loops let us craft custom loops that will continue running until a condition becomes false.continue
or break
respectively.That’s another huge chunk of new material, but with conditions and loops you now know enough to build some really useful software – give it a try!
SPONSORED Proxyman: A high-performance, native macOS app for developers to easily capture, inspect, and manipulate HTTP/HTTPS traffic. The ultimate tool for debugging network traffic, supporting both iOS and Android simulators and physical devices.
Sponsor Hacking with Swift and reach the world's largest Swift community!
Link copied to your pasteboard.