Monday, November 17, 2014

If I, if I, if I, if I Didn't Love you else I'd Hate You

At this point I'm sure I've written a bunch of if statements in various examples, but I want to discuss a few specifics.

So far, I've used only the simplest form:

if ( someConditionIsTrue ) {
    executeThisCode();
}

It actually doesn't get TOO much more complicated than that, but let's go into it anyway.

Let me just talk about what I mean by 'someConditionIsTrue':  What I mean is that we are evaluating what is within the parenthesis in order to determine whether the Java language considers this to be a 'true' value.  That doesn't mean some kind of universal truth, and it doesn't mean that it knows better than we do.  All it means is that if the language structure causes the boolean value 'true' to come out, we pass the test.  All of the following are 'true' to Java:

true

! false

1 == 1

1 != 53

(1+1) == 2

(1+2) == (4-1)

And it gets more complicated than that.  There are all sorts of expressions one can evaluate for truth, many times they are based on previous work your program has done.  You might for instance have a user enter their birth date, compare it against the current date, and decide that 'true' means 'older than 21 years'.  All you have to do is construct the expression correctly in order to suit the language's ability to decide based on what it considers to be truth.

OK, but what if someCondition ISN'T true?  The block doesn't execute, and we go on to the next line after the closing bracket, right?  But what if we want to do something because the condition is not true?

I guess we could do this:

if (conditionIsTrue) {
    doStuff();
}

if (conditionIsTrue == false) {
    doOtherStuff();
}

In fact, that would work fine.  Of course, if 'doStuff()' as a side effect changed the condition to false you would wind up executing both blocks.  I mean sure, doStuff() SHOULDN'T change that value, that's bad practice, but as an old C programmer I know very well that "shouldn't" doesn't necessarily mean "doesn't".

Besides, there's a cleaner way to express that concept:

if (conditionIsTrue) {
    doStuff();
}
else {
    doOtherStuff();
}

Isn't that better?  It makes the intention clear, it's less prone to side effects, and you finally get that one joke some programmer made on that forum you don't want anyone to know you visit.

You use 'else' to do things when the condition in an 'if' statement is false.  This will come in handy, and I'm sure I'll be using it soon in examples, so I wanted to make you familiar with it.

Another thing I should point out is that sometimes we need to make decisions based on not just simple true/false values, but combinations of such values.  maybe a user can be considered a 'favored customer' if they have either been a customer for two years or they have spent at least $1,000.00 with us.  That's where we get into what is called 'boolean logic'.  This is not too difficult to understand, and it's critical to get it locked down into your memory.  Please see the earlier post 'Hooray for Boo...leans' to get more information about this.  Just understand that almost anywhere that we can do a simple check for true/false, we can probably extend it to a more involved expression involving multiple terms combined in various ways.



View code here