iOS

iOS Lesson 06 – Conditional logics and conditional statements in swift

So with this knowledge bit, we are going to talk about Boolean and logical or conditional statements in swift. Let’s start right away with the playground.

 

Conditional logic and conditional statements in Swift

Open up your Xcode and create a new playground. We are going to call this one “ConditionalLogicsAndStatements” and delete the Hello world line.

Boolean:

Just to make things simple, a Boolean is either true or false. Nothing can define a Boolean in a simpler than this. We are going to go right in the code and create a Boolean variable, name it anything you want but we are going to name it “areWeCool”. Just so you don’t feel that we are exaggerating about ourselves we are going to put its value equals to false.

Code:



            var areWeCool = false

       

 

in this line of code we are using the var keyword to define that we are going to create a variable, then we give it a name “areWeCool” and then we use the equal sign “=” then a value(in this case “false”). So this is a Boolean and its false. It can also be true but can’t be anything else than these two values

  1. Q) Why doesn’t it even mean that we are storing a variable whose value can only be true or false?
  2. A) Most of the times you just need a Boolean to compare different things such as variables, returns of a function and much more. in short, if you want to do something based on some condition you would need a Boolean. Not a variable always but a statement that will return either true or false. If it sounds confusing then don’t worry. We are going to look deep into this knowledge bit. Let’s look at them in action

Write the following line of code.

Code:



            if areWeCool == true || areWeCool != false{

            print("what just happened???")

                }

                 else

                {

             print("If statement was not true to it printed this!")

            }

In these lines of code, we are checking whether the variable areWeCool is true “OR” it’s not equal to false. We have used “||” symbol to state the OR condition whether condition # 1 (areWeCool == true) OR condition # 2 (areWeCool != false) returns true. If any of these conditions returns true we are going to print “What just happened”

In our case, both conditions were false so it printed the second else condition print statement.

conditional statements in swift

we have used several operators in our code one of them is “==”. This is the comparison operator that returns true when the left-hand side is equal to the right-hand side. The second was “!=” this is the not equal to sign, returns true when the left-hand side is not equal to the right-hand side.

 

Now, let’s take another example in which you are working on an IOS app that downloads some files from the Internet. But you want to make sure that the app doesn’t look dead when it is downloading the file so you came up with an idea to show a progress bar while the downloading is in progress. Similarly, you would want to create a variable that will hold the value of either the data has finished downloading or not.

Anytime you want to get a true or false in the code. Boolean is the way to go. Boolean go hand-to-hand with comparison operators because comparison operator returns a Boolean.

Let’s talk about some more conditional operators

 

  • “>” name: Greater than the operator. It will return true when the left-hand side is greater than the right-hand side.
  • “<” name: less than. It will return true when the left-hand side is less than the right-hand side.
  • “>=” name: greater than or equal to. Will return true when the left-hand side is either greater or equal to the right-hand side
  • “<=” name: less than or equal to. It will return true when the left-hand side is either less than or equal to the right-hand side.

Now, let’s do some practice with these conditions.

Code:



if 3 < 4{ print("3 is surely less so we should see this") } if 3 > 4 {
    print("shouldn't see this as 3 is not greater than 4")
    }

   if 3 == 3{
    print("should see this as 3 is equal to 3")
    }
 if 3!= 4{
    print("lets see this as 3 is not equals to 4.")
  }

       

 

I’ve definitely used some of the most basic and self-explanatory examples that will make you think a little bit about Boolean and conditional operators. Remember, these conditions are returning a Boolean giving you an option to store this in a variable.

 

Code:



            var isThreeGreaterThanThree = 3 > 3 (returning false)

what I’ve done here is that I’ve used one of the  conditional statements in swift that is in a value field of a variable. As I said, conditional statements always return a Boolean so we are saving the result of “3 > 3” in the variable “isThreeGreaterThanThree”. Weird right? I know, but its simple enough to teach a school kid J

we can also use these operators on strings. Let’s get going with comparing strings with each other.

Code:



            var isTheNameCorrent = "geeks" == "Geeks" (returning false)

       

 

in this example, we’ve used two string comparison on a variable that is set to false because Swift is case sensitive. It means that “hello” and “Hello” are two different terms.

So in this knowledge bit, we have covered Boolean, conditional operators and Conditional statements in Swift that how we can use them efficiently in the code to make our lives easier. You can get all the code from here.

Please Like, share and subscribe to more!

 

You can have a look at our whole IOS series as well.

Click to comment

Most Popular

To Top