If - Then - Else
6 Feb 2008 @ 07:32AM

Updated: 25 Jan 2010 @ 07:33AM
The If - Then - Else is probably the most basic decision-making code you'll run across. It exists in every language I've ever learned and it always follows the same format. First a screenshot of a simple if - then - else statement.

If-Then-Else At Its Simplest

This is basically what I'm doing. I'm taking the name variable and seeing if it equals "Satis". "Satis" is a string (you can tell because it's surrounded by double quotes) and you know from the code before that name is a string. Thus I can use == to compare their contents. This is the If part of the if - then - else statement. If name does indeed == "Satis", the first section of the if statement is executed. This is the then section.

The then section adds "You are my master.
" to the end of the contentString string. However, if name does not == "Satis", the else statement is executed. In that case, instead of appending the master bit, I append "Serve me.
". Go ahead and try it out in a browser. So let's try something just a tad more complicated.
Comments (0)

If-Then-Else More Complicated

There's a lot more to this. Let's go over it peice by peice. First, you may notice it's not just if - then - else. In this case it's if - then - else if - else if - else if - else. The else if statements basically let you test a whole range of different statements. The first thing we do is check if name == "Satis" && occupation == "Code Monkey". The && means "and". Basically if the name is set to Satis and the occupation is set to Code Money, we append the "You are my master.<br>" string to contentString and then continue executing code. Note, we do NOT keep checking through the else ifs. Once you match one, you're done.

If the name is "Satis" and occupation is not "Code Monkey", we check the next statement. Now we're seeing if the name is "Satis" (again). However, intead of && we have ||. || means or. Thus if the name is Satis or the occupation is Goober, you get "That sucks.<br>" appended to the contentString.

If we still haven't made a match, we check if name == "Shiny" and, if so, we append "You rock.<br>" to the contentString. Pretty straight forward, right?

The next statement has some parenthesis in it. You may remember from math the concept of grouping operations. Basically any operations inside parenthesis are executed first, then operations are executed in order. In this case we're seeing if the name is either "Pig" or "Ox". Once we've checked that, we see if their occupation == "Grabber". If the name is either Pig or Ox and they're Grabbers, we append the "Don't grab me.<br>" string to contentString. What would have happened if we didn't have those parenthesis in there? Due to order of operations the statement would have returned true if the name was Pig, period. The occupation would never have been checked. Meanwhile, Ox would have had to be a Grabber before this statement evaluated as true. When doing statements like this, always remember to group properly. Grouping items that don't necessarily need to be grouped won't cause any problems, but forgetting to group things can cause confusing behavior from your application.

The only time you really need to worry about grouping is when using an or (||) operator. If all you have is and (&&) operators, grouping doesn't really affect much. However, if you're using math in your comparison than group may very well be important. For instance, you could do something like: if((x^2 + 5)/10 >= 8). In this case, we take the variable x, square it, add 5, then divide all of that by 10. If the result is greater than or equal to 8, the if statement is satisfied. However, what happens if you write it without the parenthesis? if(x^2 + 5 / 10 >=8). Since division comes before addition in order of operations, you would get x squared + 2. Big difference.

Finally, if none of the other criteria are met, we return the default response of "Serve me.<br>". Do we need this else on there? No. If you have a series of conditions like above but don't want an else statement, just leave it off. The following is perfectly valid code, though you may notice the last statement lacks parenthesis around the or section. As such, Pig will always match it, while Ox will only match if he's a grabber..

Else Isn't Required

Once again, this is a really important section to understand. If there's any confusion, play around with it. Try matching occupations, names, dates, whatever. On the next page we'll continue with ternary operations, a special version of the if - then - else statement.
Comments (0)