Showing posts with label conditionals. Show all posts
Showing posts with label conditionals. Show all posts

Monday, November 17, 2014

The Old Switcheroo

Let's talk about the 'switch' statement for a bit.  This is a form of conditional that is better suited to some tasks than if/else.

First, let's look at the structure of a switch:

switch(variable) {
    case value1:
        //Code to execute for value1
        break;

    case value2:
        //Code to execute for value1 or value2

    case value3:
        //Code to execute for value3
        break;

    default:
        //Code for any value other than the three above

}
// Done with switch

In detail.

If variable is equal to value1, we execute the code under 'case value1', then we 'break', which means we exit the current block (jump to 'Done with switch').

If variable is equal to value2, we execute the code under 'case value2', then continue to execute the code under 'case value3' before we hit the 'break' and head out to 'Done with switch'.

If variable has any other value, we execute the code under 'default'.

This is basically equivalent to:

if (variable == value1 || variable == value2) {
    if (variable == value1) {
        //Code to execute for value1;
    }
    //Code to execute for value1 or value2
}
else if (variable == value3) {
    //Code to execute for value3
}
else {
    Code for any value other than the three above
}

But I think in this case the switch is a bit easier to follow.

Sadly, the switch works with only a limited number of data types.  Basically it will work for atomic types that represent integer values, the objects that wrap those atomic types (like Integer or Long), and String.  It will also work with Enumerations, but I haven't written about those yet...  I guess I should get to those soon.

So, let's combine a for loop with a switch statement to get some output, shall we?

        for (int switchValue = 0; switchValue < 5; switchValue ++) {
            switch (switchValue) {
                case 0:
                    System.out.println("Just starting out.");
                    break;

                case 1:
                    System.out.println("Making progress.");
                    break;

                case 4:
                    System.out.println("All done.");
                    break;

                case 5:
                    System.out.println("We will never get here.");
                    break;

                default:
                    System.out.println("Getting there...");
                    break;
            }
        }


This runs a simple loop that goes from 0 to 4.  Along the way, we run a switch to execute various blocks of code based on that value.  The output from this block of code looks like this:

Just starting out.
Making progress.
Getting there...
Getting there...
All done.

If you follow the code, I hope this makes sense to you.  The 'default' statement catches anything that isn't 0, 1, 4 or 5, so it runs a couple of times in the middle.  If this is not clear, drop me a line and I'll try to improve it.

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