O'Reilly logo

Xcode 7 Essentials - Second Edition by Jayant Varma, Brett Ohland

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conditional statements

At their simplest, conditional statements let you check the condition of a variable or constant, and make a decision based on it. The if statement is meant for simple checks, the switch statement allows for complicated checks with multiple outcomes, and the guard statement acts like a bouncer.

The if statement

An if statement will run any code between its braces {} if the check is true:

var berry = "black"
if berry == "red" {
  print("berry is red")
}

The print statement is never run because berry is not equal to the string "red".

Adding an else statement lets run the code if the original if statement is false:

var berry = "black"
if berry == "red" {
  print("berry is red")
} else {
  print("berry is not red")
}

With the else statement, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required