Am I too controlling?...

Well, we've discussed an overview or programming terms and discussed data. How do we get a program to do something interesting?

The answer...control structures!

Control structures are commands that check for some condition and then, base on the result of that check - run different blocks of code. The simplest type of check is whether or not something is 'true'. To accomplish this, we use If-Then.

For example, suppose we want to have a program either say 'Good Morning' if it is before 12 noon. I will be using pseudo-code so I get to ignore some of the details - like 'how do you know what time it is?':

  1. Find the time and store the hour of the day in a variable
  2. If the hour is less than 12 then
    1. Print 'Good Morning'
Easy-peasy!

But, if it is after noon, then our program won't do anything (at least as far as the user can tell). So, let's have the program say 'Good Afternoon' if it isn't morning. To do this, we move on to using If-Then-Else. This structure is intended when you have two different blocks of code and only want to run one of them based on some condition. Here is the pseudo-code:
  1. Find the time and store the hour of the day in a variable
  2. If the hour is less than 12 then
    1. Print 'Good Morning'
  3. Else
    1. Print 'Good Afternoon'
Make sense? This is something that you most likely do every day! And this pseudo-code is just what you are doing in your own head. You check the time and 'if' it is morning then you say 'Good Morning' else you say 'Good Afternoon'.

Well, you probably do something a little more complicated. Like, what it is after 6:00 PM? Would you still say 'Good Afternoon' or would you switch to 'Good Evening'?

This is still something that you would probably still use tests that are broken down into 'ifs'. Here is where you would 'nest' or 'cascade' those 'ifs'. Let's see about this in pseudo-code:
  1. Find the time and store the hour of the day in the variable
  2. If the hour is less than 12 then
    1. Print 'Good Morning'
  3. Else
    1. If the hour is less than 18 (using a 24 hour clock) then
      1. Print 'Good Afternoon'
    2. Else
      1. Print 'Good Evening'
Nice!

It kind of seems like If-Then-Else could handle every situation that we come across! And in fact, it could...

But, programs get messy and difficult to follow the more you nest. Normally, you would not want to nest any deeper than that example. And if you reorganize things then you should be able to make sure that you don't need to.

Let's leave If-Then for now. What if you have several specific conditions and each of them should trigger a different block of code? Here is where the Switch comes in. Going back to our greetings - let's have every even numbered hour just say 'Hello'. Well, there is a twist with Switch. That is, you have to have specific values that you are choosing from - not conditions like with If-Then. So, pseudo-code:
  1. Find the time and store the hour of the day in a variable
  2. Switch based on the hour
    1. In case it is 1 : Print 'Good Morning'
    2. In case it is 2 : Print 'Hello'
    3. In case it is 3 : Print 'Good Morning'
    4. (repeat this pattern up until 11)
    5. In case it is 12 : Print 'Hello'
    6. In case it is 13 : Print 'Good Afternoon'
    7. (repeat this pattern up until 17)
    8. In case it is 18 : Print 'Hello'
    9. In case it is 19 : Print 'Good Evening'
    10. you can see where this is going... 
Now, this isn't really a good use for Switch - I kind of forced it just for the example. In fact, you might find few instances where you need it...or you might end up using it often. But, remember that it is available for you.

One last topic before we can get on to something that is actually interesting - loops. Those will be next.

Comments

Popular posts from this blog

Is trying to program making you loopy...?

Laid out (arrayed) out before you...

Constants and Variables....