Hey guys! It’s Ashank here again, and today we will be exploring conditional statements, one of the most basic but most important concepts of programming.
An “if statement” is a conditional statement, which has a condition, hence the name. If the condition is agreed to, then code within the body will be carried out. Otherwise, (or else) it should commit to another body of code. Let’s try to do something with this in XCode.
Create a variable called myNumber and set it equal to 100. Then make a second number called mySecondNumber and set it equal to 50. Next we create an if-statement. Say we wanted to compare whether mySecondNumber was less than myNumber. Well, you can add the following code to your playground:
As you can see, we just made an if-statement! We told Swift, that if mySecondNumber is less than myNumber, perform the following code. This code will go into the body of the if-statement. Let’s put a print statement in this block.
Sure enough, you can see that the print statement is being performed. Since mySecondNumber is less than myNumber, the String “myNumber is my number” is being printed. Now, if we change mySecondNumber to a number higher than myNumber, what will happen?
As you can see, after changing mySecondNumber to a number higher than myNumber, the print statement is not being performed. That’s because the numbers do not comply with the restriction we have set in the if-statement!
What if we wanted to tell Swift to do something else if the if-statement requirements weren’t met with? Well, we can simply add this underneath our if-statement.
Let’s walk through again what is happening here. If mySecondNumber is less than mNumber, then print: “myNumber is my number.” Otherwise, print: “We are in the else-statement block right now!” Since mySecondNumber is greater than myNumber, the code in the else block is being performed!
We can also have multiple if-statements chained as else-if statements if we want to consider more restrictions. However, all of these will have to go above the else-statement we just created.
Woah. That’s a lot of additional code added. However, it is all very simple. Think of else-if statements as a set of restrictions that you know may be complied with or not. Let’s walk through this code.
Our first if-statement says if mySecondNumber is less than myNumber, then print out: “myNumber is my number.” Otherwise, if mySecondNumber is equal to 50, then print out: “mySecondNumber is equal to 50.” Otherwise, if mySecondNumber is equal to 100, then print out: “mySecondNumber is equal to 100.” Otherwise, if mySecondNumber is equal to 150, then print out: “mySecondNumber is equal to 150.” If all of those restrictions are not complied with, then print out: “We are in the else-statement block right now!”
It’s as simple as that! Since mySecondNumber is 150, you can see that the 3rd else-if statement is being printed out. Try changing the value of mySecondNumber and see what the console prints out!
Our next topic is switch statements. Switch statements are used to check a variable and several cases of that variable. Suppose we have a random variable named myNumber which has a value set to 1. We could say “switch myNumber” and put several cases inside of that block of code. In the case that myNumber is equal to 1, 3, 5, 7, or 9, print: “myNumber is an odd number.” In the case that myNumber is equal to 2, 4, 6, 8, or 10, print: “myNumber is an even number.” And then, we can have a default message as well. Let’s try and coding this to see what happens.
As you can see, we have created our switch block. Inside of this block, we will define cases that myNumber can equal to.
We have now added 2 cases to the switch block. The first case says that in the case that myNumber is equal to 1, 3, 5, 7, or 9, print: “myNumber is an odd number.” The same logic applies for the 2nd case statement. However, you can see that we are getting an error. Why is that? The error says that we need to add a default clause. A default clause will tell Swift what to do if myNumber does not match to any number defined in the cases. Let’s add this clause!
And there you have it! Since “2” is defined in the second case, and myNumber equals to 2, the console prints out “myNumber is an even number.” The default clause will be used when myNumber is not defined in any of the cases. For example, if we change myNumber to 2.5, we can see that the default clause will be printed out.
There you go! The default clause is being printed out since 2.5 is not defined in any of the cases.
This was a basic tutorial of conditional statements. It is vital that you beginner programmers understand these concepts thoroughly since you will be using them countless times when developing in iOS and other languages as well! That’s it for this time! I’ll catch you guys later!