L2P: the “Parts” of a Program - Decisions Decisions
You would be hard pressed to find a program that did not use some sort of logic or decision making during its execution. There is some form of “IF” in just about every programming language. So lets discuss how we use it in programs and how its implemented in Objective C!
In general most decisions are going to be made by comparing a known value to a variable. Below is an example if the “IF” structure used in Objective C.
if (expression) { // This code runs if the expression returns TRUE (1) }
So the expression is the test we want to perform. Examples could include if a variable is equal to a certain value, or if 1 variable is bigger than another. There are quite a few Boolean comparison operators we can use to build our expressions:
| Operator | Description |
|---|---|
| x == y | Returns true if x is equal to y |
| x > y | Returns true if x is greater than y |
| x >= y | Returns true if x is greater than or equal to y |
| x < y | Returns true if x is less than y |
| x <= y | Returns true if x is less than or equal to y |
| x != y | Returns true if x is not equal to y |
Most of these are pretty self explanatory. Do be sure to notice that the equal comparison uses two equals signs (==). This is because in Objective C one equals sign denotes setting a variable.
There are also some Boolean logical operators. They are used in situations where you need to preform more than one comparison. As an example:
// run code if both A == B AND C == D if ((a == b) && (c == d)) { // code }
I personally wrap each individual expression in parentheses for clarity sake.
You may not use them much to begin with, but they are nice to know since using AND or OR in your code will not work.
Below are the Boolean Logical Operators:
| Operator | Description |
|---|---|
| || | OR - This returns true if EITHER expression (or both) returns TRUE |
| && | AND - this returns true if BOTH expressions are TRUE |
| ^ | XOR - This returns true if ONLY one of the expressions is true (Exclusive OR) |
Now like I stated before, the code contained within an if is only ran when the expression is true. There are also cases where you may want to do something else if the expression is not true. we can do that using an "else". Here is an example:
if (expression) { // This code runs if the expression is true } else { // This code runs if the expression is NOT true }
You can also choose to string multiple ifs together using else if statements:
if (expression) { // this code runs if the expression is true } else if (expression2) { // this code runs if expression 2 is true and expression 1 is not true) } else { // this code runs if both expression 1 and 2 are not true. }
Remember with else if only the first expression that evaluates true is ran. The rest of the if chain is ignored. So even if multiple if else statements would be true, only the FIRST one is ran.
There is one type of if style decision, its called a "switch". Its use case is unique, but can be a time saver in certain cases. Essentially switch lets you check a single variable and let you run code based on whats inside it. Let me show you a code example which should make its use pretty clear:
// For this example, we have a variable (x) that has either a 1 2 or 3 assigned to it if (x == 1){ // code to be ran if x = 1 } else if (x == 2){ // code to be ran if x = 2 } else if (x == 3){ // code to be ran if x = 3 } else { // code to be ran if x previous statements fail } // now lets do it using the switch statement: switch (x){ case 1: // code to be ran if x = 1 break; case 2: // code to be ran if x =2 break; case 3: // code to be ran if x = 3 break ; default: // code to be ran if above statements failed break ; }
While it may not seem quicker, I personally think its easier to read. Its really great for testing to see what is assigned to a variable if you know what to expect inside that variable.
In the next post we will go over some looping. After that we will go over using both decisions and loops in our HelloOOP Program.
So a few house-keeping things. The "Spring 2010" session of CS193P is not going to be posted on iTunes U unfortunately. So instead, I'll be going though the Winter 2010 session. I'm planning a few more posts as an introduction, concluding with an actual "first" iPhone app post in the next 2 or 3 weeks. Then ill start into the CS193p classes. That's when things are going to shift gears a bit here, but ill discuss that more in another post when it happens.





Comments
Post new comment