Hello!
Today we will be going over an extremely important concept of Objective-C, making decisions. This will be in the form of if statements and switch statements. Let's dive right in. For this tutorial you will need to download the starter project here.
Today we will be going over an extremely important concept of Objective-C, making decisions. This will be in the form of if statements and switch statements. Let's dive right in. For this tutorial you will need to download the starter project here.
If Statements
Open up the starter project. Navigate to the
MakingDecisions.m file. Now we will write our first if statement. If statements are structured so that when the code reads it, it checks to see if the condition(s) that we set is true then it runs the code in the curly braces. If it is not true it skips reading the block of code in the curly braces, and it moves on to the next block. Before we write out first if statement first declare a variable of type int that is called myInt and is equal to 0 inside of the ifStatementMethod. Now we are going to use that variable for our condition in the if statement.
Here's what the code completion tells us to write. if then parentheses. Inside those parentheses you will write some sort of condition that the compiler in Xcode checks to see if it is true or not. Then after the parentheses there are curly braces. Inside those curly braces you will write a block of statements that will be executed if the condition is true. Let's fill in the blanks now.
Congrats! You just wrote your first if statement! Now let's see what it does. First the compiler checks to see if the value of myInt is less than or equal to 5. If it is then it logs to the console "My first if statement." Run the app and see if the expected result happens!
It does! Now we will dive into if-else statements and if-else-if statements.
If-Else and If-Else-If Statements
First copy the code between the curly braces of the ifStatementMethod. Now comment out all of the code that we just wrote.
Navigate to the method called ifElseStatementMethod. Now we will just expand on the same exact code we just wrote. Paste the code you just copied into this method. First we will look at the if-else statement. What if we also wanted to execute code if myInt was greater than or equal to 5? Then we write an if-else statement like this.
In this the compiler checks to see if the condition of myInt <= 5 is true. If it is not true than it sees that there is an else keyword, and the code between the curly braces of the else keyword is executed. There is no parentheses and condition after the else keyword because it is saying that if the first condition is false then this code must be run. No condition must be true for the code in the else statement to be run. All that needs to happen is that the condition in the if statement is false. Now let's expand even more into if-else-if statements.
Here I added the else if part to the code. This acts just like the else statement except it specifies a condition. So the code in the else if part is run if the condition of the first if statement is false (myInt is not less than 5), and if the condition specified in the else if part is true (myInt equals 6). (NOTICE: When writing a condition to see if a variable equals a number, you use a double equals sign. This shows equality between two things. One equals sign shows that you are assigning a value to something.) If both conditions are false then the code in the else block is run. Run the code and see what happens. Change the value of myInt to different numbers and see how the output changes.
Switch Statements
Now comment out all that code. (HINT: To comment out more than one line of code at once, you can highlight those lines and then press command-backslash.) Now navigate to the switch statement. Switch statements are pretty much if-else-if statements except they are more convenient to write. I would only use a switch statement if you need to write 3 or more if-else-if statements. This improves readablility of the code.
So this is what the code says. First we declared a variable of type int called myInt with a value of 3. Then we write the switch keyword. In the parentheses after there is the variable name myInt. What those parentheses are is a place that we put a variable name. Then depending on the value of that variable a case's code is executed. In this situation myInt = 3 so case 3 will be executed. All the break statement does is make the compiler skip the rest of the code in the switch statement. Play around with this. Change the value of myInt and see what happens. Sometimes the best way to learn code is to write it and to guess and check the outcomes of your code.
Congratulations! Now your code can think as well as you do! Well, maybe not that well, but it can make some decisions on its own. That was a lot of material, so great job! Get ready though because next we will be learning about inheritance and scope. As always, feel free to leave a comment, suggestion, or question below, or email me at [email protected]
Thank you!
Zachary Cmiel
The PoKoBros
Congratulations! Now your code can think as well as you do! Well, maybe not that well, but it can make some decisions on its own. That was a lot of material, so great job! Get ready though because next we will be learning about inheritance and scope. As always, feel free to leave a comment, suggestion, or question below, or email me at [email protected]
Thank you!
Zachary Cmiel
The PoKoBros