Swift: If – Else Conditional Statement

After having a good knowledge of Swift’s basics, let us now turn to one of the most important and used functions in programming, the if…else function.

If you have never programmed before, don’t worry, the if…else function is pretty simple to understand. Basically, we will state a condition and if the condition is true then we will execute a code, else we will execute another code. To have a better understanding, let’s take a simple example


Screen Shot 2017-04-15 at 09.57.04.png

So we declare a variable as an “Int” and we give it a value of 3. Afterwards we create a use the if…else condition in order to test if the value is 3 and if it is true, then we output a certain text (here: “The number is 3”) or else, if it false, then we will output another text (“The number is NOT 3”).

A few notes over the if…else function, before moving forward:

  • the condition that is tested, needs to be between parenthesis –> if ( condition ). Without it, SWIFT, will give as an error.
  • testing a condition uses comparison operators: recall that in order to test if two variables are the same, we do not simply use the equal sign but a double equal sign ” == ”  (Swift: Comparison Operators)
  • the code that will be executed if the condition is true or false needs to be between { }

The if…else function allows us also to test multiple conditions if we employ the AND (&&) and OR (||) as we have seen it in the previous lesson (Swift: Comparison Operators).

So let’s use the if…else function and test a multiple condition.

Screen Shot 2017-04-15 at 10.21.15.png

Here, we have 2 variables: “number” which has the value of 3 and “number2” which has the value of 0. The function tests at the same time if the variable “number” is equal (==) to 3 and if the variable “number2” is different than zero (!=). In order to execute the first part of the code, both conditions must be true, due to the AND (&&). Using in this case the OR (||) would have executed the first part of the code.

Given that “number2” has a value of zero, the second test fails and thus the second part of the code is executed.

We can also, spice up things a little bit and include if…else functions into another if else function. The idea remains the same.

So given the test we have already done above, we know that one of the variables is not true (here we know obviously that it is the second one but in the case of an user input variable, things are not so clear). So what we can do is add a second if…else test in order to see if it is the variable “number” or the variable “number2” that is false.

Screen Shot 2017-04-15 at 10.33.03.png

By introducing a second if…else test, we are able to precisely detect that it is the second condition of the first test that is not respected (number 2 !=0).



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

Up ↑

%d bloggers like this: