O'Reilly logo

Swift Quick Syntax Reference by Matthew Campbell

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

Chapter    23

if Statements

if statements are used when you want to make a choice to execute code based on the result of a comparison expression. To make this work, you evaluate an expression that uses comparison and logical operators to yield a true or false result. If you evaluate an expression to be true, then you can execute the code; otherwise, you can ignore the code.

You need the if keyword and an expression along with a code block to use the if statement (see Listing 23-1).

Listing 23-1. if Statement

if (1 < 2){    println("This is true")}

The statement is saying that if 1 is less than 2, then execute the code that will print out the string “That is true” to the console log.

else Keyword

You can also define an alternate action with the ...

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