O'Reilly logo

Mastering Swift 4 - Fourth Edition by Jon Hoffman

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

The if statement

The if statement will check a conditional statement and, if it is true, it will execute the block of code. This statement takes the following format:

if condition {  
  block of code 
} 

Now, let's look at how to use the if statement:

let teamOneScore = 7  
let teamTwoScore = 6 
if teamOneScore>teamTwoScore {  
  print("Team One Won") 
} 

In the preceding example, we begin by setting the teamOneScore and teamTwoScore constants. We then use the if statement to check whether the value of teamOneScore is greater than the value of teamTwoScore. If the value is greater, we print Team One Won to the console. If we run this code, we will indeed see that Team One Won is printed to the console, but if the value of teamTwoScore is greater than ...

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