Ryans Tutorials
More great tutorials at RyansTutorials

Algorithms : Decisions

Which to choose?

Introduction

Flowchart decisionsIn the previous section we introduced the idea of algorithms and looked at the first basic structure which is sequence. In this section we will expand upon this and look at how we make decisions, also known as selection.

In this tutorial we will investigate two methods of algorithm description commonly used in software engineering :

  • Pseudocode - a text based method
  • Flowcharts - a graphical method

The two methods are comparable and have the same features. Anything you can represent in one, you can represent similarly in the other.

Binary Decisions

It is often the case that we want to perform an action but only under certain conditions.

IF condition THEN
    processes
ENDIF

Shapes used in Decisions

When doing flowcharts it is important to label the flow lines so it is clear which path is which.

Both methods of displaying a decision for flowcharts are valid. You may use whichever one you prefer. Think about which will make your diagram cleanest and easiest to read.

For instance, maybe there is a ride but you have to be over the age of 5 before you're allowed to go on it :

Binary Selection

  1. BEGIN
  2.   Get age
  3.   IF age > 5 THEN
  4.     Display "Great, you can go on the ride"
  5.   ENDIF
  6.   Display "Enjoy your day"
  7. END

Notice that:

  • The keywords IF, THEN and ENDIF are written in capitals.
  • The processes inbetween are indented.

Indenting is important. It makes the structure stand out and improves readability.

Flowchart for ride IFHere is the same algorithm as presented above but in flowchart :

Here I've made the decision to use the first style of layout as it keeps the shapes in-line and keeps the flowchart clean and easy to read. The alternate layout (with flowlines from either side of the diamond as opposed to one side and one down) would also be valid.

Now in the above example there is a process that either does or does not get run based upon if the condition is satisfied or not. Sometimes you want one set of processes to run if the condition is True and another set to run if the condition is False. For this we introduce the ELSE keyword.

IF condition THEN
    processes
ELSE
    processes
ENDIF

Let's take our example from above but expand it so that if the person is of age 5 years or under they get a different message :

Binary Selection with ELSE

  1. BEGIN
  2.   Get age
  3.   IF age > 5 THEN
  4.     Display "Great, you can go on the ride"
  5.   ELSE
  6.     Display "Sorry, you are not quite old enough for the ride"
  7.   ENDIF
  8.   Display "Enjoy your day"
  9. END

Flowchart for ride IF ELSEHere is the same algorithm as presented above but in flowchart :

Here I've made the decision to alter my flow arrows to go from either side instead of bottom and side as it balances the flowchart and improves readability. You could leave it in the previous style and it would still be valid however.

Conditions

So far the examples we have looked at have a simple condition, if the persons age is greater than 5 or not. We can be more complex in our conditions. We can use a range of operators, including logical operators.

Operators :

  • = - is equal to
  • < - is less than
  • <= - is less than or equal to
  • > is greater than
  • >= is greater than or equal to
  • != - is not equal to (sometimes written as <>)

In many programming languages we use == instead of = to ask the question of if two things are equal. In algorithms we can just use a single = however there is no harm in using two as well. Just be consistent in how you utilise them.

Logical operators :

  • and
  • or
  • not

Condition example

  1. BEGIN
  2.   Get age
  3.   Get height
  4.   IF age > 5 and height >= 80cm THEN
  5.     Display "Great, you can go on the ride"
  6.   ELSE
  7.     Display "Sorry, you are not quite old enough or tall enough for the ride"
  8.     Display "Maybe try a different ride that is more suitable"
  9.   ENDIF
  10.   Display "Enjoy your day"
  11. END

Some of you with programming experience may be looking at the above code and thinking that it should be height >= 80 (without the cm) as it would be entered as an integer. When implemented in code this is probably how it would be done and you can do similar in the algorithm if you like. It is perfectly ok to have the cm and can be beneficial as the purpose of an algorithm is to express the intended logic and to be as clear and easy to understand as possible. When the programmer turns the algorithm into functioning code they will make the necessary changes as they go.

Flowchart for ride conditionsHere is the same algorithm as presented above but in flowchart :

Nested Decisions

Often there is more than one or two paths that we could take based upon multiple conditions. This is where Nested decisions come in. Only one group of processes will be run however we have more fine grained control over which set it will be.

IF condition THEN
    processes
ELSEIF condition THEN
    processes
ELSE
    processes
ENDIF

You can have as many ELSEIF statements as you require. Once a particular condition is met the subsequent processes are run and then it jumps to the ENDIF and skips all other statements.

Let's say that if you are under 5 but have a height over 80cm you may go on the ride if your parent/carer gives consent :

Nested example

  1. BEGIN
  2.   Get age
  3.   Get height
  4.   IF age >= 5 THEN
  5.     Display "Great, you can go on the ride"
  6.   ELSEIF height > 80cm THEN
  7.     Display "You may go on the ride if your parent / carer agrees it is ok."
  8.   ELSE
  9.     Display "Sorry, you are not quite old enough or tall enough for the ride"
  10.     Display "Maybe try a different ride that is more suitable"
  11.   ENDIF
  12.   Display "Enjoy your day"
  13. END

For the second condition we don't need to put age <= 5 and height > 80cm. It is fine just to have the second part of the condition as the first part is implied by the first IF statement being False if we get to this condition.

Flowchart for ride nestedHere is the same algorithm as presented above but in flowchart :

As you can see here, flowcharts can very soon become quite large and unwieldly. This is one reason that people prefer to use pseudocode instead. They do, however, present the flow of the algorithm in a visual manner that can make understanding the logic easier when presented cleanly.

Here is another example. Let's say that if you are exactly 5 you may go on the ride but only if an adult is with you instead :

Nested example 2

  1. BEGIN
  2.   Get age
  3.   IF age > 5 THEN
  4.     Display "Great, you can go on the ride"
  5.   ELSEIF age < 5 THEN
  6.     Display "Sorry, you are not quite old enough or tall enough for the ride"
  7.     Display "Maybe try a different ride that is more suitable"
  8.   ELSE
  9.     Display "You may go on the ride if your parent / carer goes with you."
  10.   ENDIF
  11.   Display "Enjoy your day"
  12. END

We know that if the algorithm gets to the ELSE section then the person must be exactly 5 as they are not less than 5 and they are not more than 5.

When you are setting up nested decisions the conditions can often be in any order. Often, one particular order is nicer and more elegant than others.

Multiway Decisions - CASEWHERE

Sometimes you have a situation where there is one variable and you have multiple processes that may run based upon a series of possible values for that variable. This could be achieved with a nested IF statement however there is a shorthand method which is CASEWHERE :

CASEWHERE express evaluates to
    value a : processes
    value b : processes
    value c : processes
    OTHERWISE : processes
END CASE

The values may be exact items or can include operators such as > or <

Maybe we are creating a system to help a vehicle respond when approaching traffic lights :

Condition example

  1. BEGIN
  2.   Get lightColour
  3.   CASEWHERE lightColour is
  4.     "red" : Set action to "stop"
  5.     "amber" : Set action to "slow down"
  6.     "green" : Set action to "continue"
  7.     OTHERWISE : Set action to "proceed with caution"
  8.   END CASE
  9.   Display action
  10. END

Flowchart for casewhereHere is the same algorithm as presented above but in flowchart :

Most of the time you will just have a single process after each value but it is possible to have several processes listed after a value. (you should generally try to set up your logic to avoid this, or use nested IF statements instead, however as it tends to hinder readability). Here is a similar algorithm but with the amber option expanded a bit :

CASEWHERE example

  1. BEGIN
  2.   Get lightColour
  3.   CASEWHERE lightColour is
  4.     "red" : Set action to "stop"
  5.     "amber" : Get distance to lights
  6.               IF distance < 10 meters THEN
  7.                 Set action to "continue"
  8.               ELSE
  9.                 Set action to "stop"
  10.               ENDIF
  11.     "green" : Set action to "continue"
  12.     OTHERWISE : Set action to "proceed with caution"
  13.   END CASE
  14.   Display action
  15. END

Now let's have a look at an example where we use ranges instead of specific values. In the algorithms below we will convert a students mark into a grade :

CASEWHERE example 2

  1. BEGIN
  2.   Get mark
  3.   CASEWHERE mark is
  4.     > 85 : Set grade to "A"
  5.     > 75 : Set grade to "B"
  6.     > 65 : Set grade to "C"
  7.     > 50 : Set grade to "D"
  8.     OTHERWISE : Set grade to "E"
  9.   END CASE
  10.   Display grade
  11. END

The algorithm checks the condition for each case, starting from the top, and the first case which matches is the only case that gets run. As such, it is important to consider the order in which you put your cases. If we reversed them and put the case for "D" first, a mark of 90 would be given a "D" as it would check this case first and 90 is greater than 50.

Flowchart for grades

Other examples

Here is a simple algorithm for calculating the area of a few shapes :

Calculate area

  1. BEGIN
  2.   Get shape
  3.   IF shape is "square" THEN
  4.     Get side
  5.     Set area to side x side
  6.   ELSEIF shape is "rectangle" THEN
  7.     Get side1
  8.     Get side2
  9.     Set area to side1 x side2
  10.   ELSEIF shape is "circle" THEN
  11.     Get radius
  12.     Set area to Pi x radius x radius
  13.   ELSE
  14.     Display "Sorry, I am not able to calculate the area for that shape."
  15.     Set area to 0
  16.   ENDIF
  17.   Display area
  18. END

Setting a variable

It is often the case that you want to set a variable as one of two values based upon a condition. An obvious way to do this is as follows :

Binary Selection Patern 1

  1. BEGIN
  2.   Get level
  3.   IF level > 10 THEN
  4.     Set difficulty to "high"
  5.   ELSE
  6.     Set difficulty to "low"
  7.   ENDIF
  8.   Display difficulty
  9. END

This works nicely but there is another way we may approach this which is a little bit more compact :

Binary Selection Patern 2

  1. BEGIN
  2.   Get level
  3.   Set difficulty to "low"
  4.   IF level > 10 THEN
  5.     Set difficulty to "high"
  6.   ENDIF
  7.   Display difficulty
  8. END

It is minor, and both methods effectively work exactly the same, but some people believe the second method improves readability as it implies that difficulty of "low" is the default and will be changed under the right conditions.

Summary

IF condition THEN processes ENDIF
Basic binary decision.
IF condition THEN processes ELSE processes ENDIF
Basic binary decision with alternate processing.
IF condition THEN processes ELSE processes ELSEIF processes ENDIF
Nested decisions.
CASEWHERE expression Choice 1 : processes Choice 2 processes OTHERWISE processes END CASE
Multiway decision.
and or not
Boolean operators used in decisions.
= < <= > >= !=
Comparison operators used in decisions.
Don't forget closing keywords
Each type of decision has a closing keyword.
Indenting
Important to make the structure of the algorithm clear.