Skip navigation

In the first edition in this series, I explained the concepts behind variables, and how they can be used to store values that your program might need to use. In this article, I shall endeavour to explain one of the things variables can be used for.

When writing a program, you need to look at variables and make a decision based upon the values of those variables. Most programming languages provide you with constructs that allow you to jump to a different part of your program based upon the value of a particular variable. The most common one is what is known as the ‘If’ statement. An If statement more often than not has an associated “Else” statement after it. In pseudocode, a simple If statement looks like this:

if Some Condition then
    Do Something
else
    Do Something else
end if

 
Let’s break that down. The If bit is computing “Some Condition”. “Some Condition” is what’s called an expression. An expression is a piece of code that equates to either true or false. For example, say we have a variable with the current temperature in it, our condition could be something along the lines of (currentTemperature = 22). That is, if the current value is 22, then this expression is true, otherwise it is false. An If statement will look at the value of an expression and “Do Something” if it is true, otherwise it will “Do Something Else”.

Note that although with (currentTemperature = 22 ) I am using the equals (=) operator it has a different meaning here. Here it is used to test whether or not currentTemperature’s value is equal to that of the value 22. It is testing equality. Previously the = operator was used for assignment, to assign a value to a variable. These two concepts are quite different. Think of it as the difference between flipping a switch and checking to see what position the switch is in. Many languages actually have different operators for these operations, as the confusion between the two can cause serious problems in complicated programs.

Let’s make a more interesting example. Imagine you want to write a program that controls a robotic bartender that serves alcohol responsibly. For the sake of this example, “responsible” means only serving 4 drinks to any one person. We will keeps tabs of how many drinks the current person has been served in a variable called “numberOfDrinksServed”. If “numberOfDrinksServed” is less than 4, we will execute the code to serve another drink, otherwise if it is 4 or greater, we will execute the code to call a taxi.

if numberOfDrinksServed < 4 then
    Serve Beer
    numberOfDrinksServed = numberOfDrinksServed + 1
else
    Call taxi
end if

Seems fairly straight forward, doesn’t it. Our expression is numberOfDrinksServed < 4. The < operator, as many will recognise, simply means “is less than”. If numberOfDrinksServed is less than 4, we will serve another beer, and then increment numberOfDrinksServed by one. Otherwise, we will call a taxi.

Let’s change things up a bit. Say we can make our robot bar tender a little less harsh by seeing if we have already served 4 drinks, and if we have, serve a light beer. We will still call a taxi though if the total number of drinks is above 4, or serve a normal beer if we are below 4. In pseudocode:

if numberOfDrinksServed < 4 then
    Serve Beer
    numberOfDrinksServed = numberOfDrinksServed + 1
else if numberOfDrinksServed = 4 then
    Serve Light Beer
    numberOfDrinksServed = numberOfDrinksServed + 1
else
    Call taxi
end if

 
Now we will step through this example. When we get to the first if statement, we check whether numberOfDrinksServed < 4. If it is, then we serve another beer, and increment numberOfDrinksServed by one. Then we exit the if statement. IE, we don’t bother checking any of the other conditions.

If numberOfDrinksServed is 4, the first statement is false, since we are checking if it is less than 4. We then check “else if” part, and see that yes, numberOfDrinksServed = 4, so we take pity on our customer and serve a light beer, and also increment numberOfDrinksServed.

If we set numberOfDrinksServed to 5, then the first two expressions equate to false, so we go with the default option of calling a taxi.

One neat thing to note about if statements is that you can have any number of “else if” statements in the middle of your block of code, so you can make quite complicated decisions. “If” statements can also be nested within each other. You would do this if you only want to check something after something else has been checked.

As an example, for our robot bartender to be responsible, it needs to check if the customer is 18 or over. If they are less than 18, throw them out. We don’t need to check how many drinks our under-18 customer has had, because we wont be serving any beer to them anyway so we will nest our previous example inside of another if statement that checks the customers age. We will create a variable customersAge to represent their age.

if customersAge < 18 then
    Tell bouncer robot to throw customer out
else
    if numberOfDrinksServed < 4 then
        Serve Beer
        numberOfDrinksServed = numberOfDrinksServed + 1
    else if numberOfDrinksServed = 4 then
        Serve Light Beer
        numberOfDrinksServed = numberOfDrinksServed + 1
    else
        Call taxi
end if

 
It should be pretty easy to follow this. Note that this is not the only way to write this, but generally you want any ‘Dealbreaker’ conditions to contain any of the conditions which concentrate on finer details. This is primarily because it is closer to how people decide on things. We look at the most critical issue, and then based upon that, use more detailed analysis to make our decisions. If we just sequentially went through all of the finer details we would be wasting our time when we could have done one check to come to our conclusion.

Well, hopefully this has been a good explanation of the basic ideas behind if statements. As always, I would love to hear your opinions on this article.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.