Conditionals
Conditionals are expressions that evaluate to either true
or false
and determine what the program will do next. As programs become more complex, conditionals can make programs more flexible and robust by addressing multiple scenarios.
As we write more complex programs, conditionals allow us to address multiple scenarios and make our programs more robust.
if
Statement
An if
statement executes a code block when its condition evaluates to true. If the condition is false, the code block does not execute.
var halloween = trueif halloween {print("Trick or treat!")}// Output: Trick or treat!
else if
Statement
An else if
statement provides additional conditions to check for within a standard if
/else
statement. else if
statements can be chained and exist only after an if
statement and before an else
.
var weather = "rainy"if weather == "sunny" {print("Grab some sunscreen")} else if weather == "rainy" {print("Grab an umbrella")} else if weather == "snowing" {print("Wear your snow boots")} else {print("Invalid weather")}// Output: Grab an umbrella
else
Statement
An else
statement is an optional partner to an if
statement. When the condition for the if
statement evaluates to false
, the code within the body of the else
will execute.
var turbulence = falseif turbulence {print("Please stay seated.")} else {print("You may freely move around.")}// Output: You may freely move around.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Swift on Codecademy
- Skill path
Build iOS Apps with SwiftUI
Learn how to build iOS applications with Swift and SwiftUI and publish them to Apples' App Store.Includes 7 CoursesWith CertificateBeginner Friendly13 hours - Free course
Learn Swift
A powerful programming language developed by Apple for iOS, macOS, and more.Beginner Friendly12 hours