Using if statements

A basic if statement looks like this:

if condition {code}

The code in the curly braces is executed if the condition is true.

Let's implement an if statement now to see this in action: 

  1. Type in and run the following code:
// If statements execute code in curly braces if the condition is truelet isPictureVisible = true// if the value is changed to false nothing would be printedif isPictureVisible { print("Picture is visible")}

First, you created a constant, isPictureVisible, and assigned true to it. Next, you have an if statement that checks the value stored in isPictureVisible. Since the value is true, the print statement is executed and Picture is visible is printed in the Debug area.

  1. Try changing the value of isPictureVisible ...

Get iOS 13 Programming for Beginners - Fourth Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.