Making Decisions

//Making Decisions
Making Decisions 2016-12-23T10:21:45+00:00

Making Decisions (selection)

In a previous chapters you were introduced to, and used counted loops in which the computer program made decisions about the value in a control variable.  The computer was effectively deciding if the control variable was greater than the end value set at the start of the loop.  This ability to make decisions is crucial in building more complex programs and is not limited to the counted loops covered so far.

This chapter introduces the tools needed for you to set up conditional statements so that the program only executes selected lines of code when a specific condition is met.  They are called IF… …THEN… …ELSE statements.

If… …Then… …Else… …EndIf

These statements are used to create blocks of code that execute depending on a specific condition being true.  (The ‘…Else…’ bit is optional).  All conditional statements start with the decision statement – what do you want the computer to check before moving on.  In programming these conditions must return either “True” or “False” – there can be no “maybe”.

Available variations

This is the basic decision statement.  It allows a block of code to be executed if the condition(s) is met.

simple decisionsThe indented code will only be displayed on Saturdays with nothing displayed on any other day of the week.

This is a more normal decision block in that something is done if the decision returns ‘False’ as well as ‘True’.

decisions with two paths

In this format, multiple questions can be asked with a response for each ‘True’ result and a general statement if none are true.decisions with multiple pathsHowever, this same result could be achieved by ‘joining’ questions together using AND or OR.

Note: the Clock method should Clock.WeekDay in all the examples above.

Examples

This is a straight forward ‘decision block’.  It asked a single question and follows different paths depending on the result of the question asked: Is the time after 12 O’Clock.

Simple Conditional statement
The question (or condition) to check has been put inside brackets in the code but this is only to make it clearer later on.  You don’t need the brackets around the question but it can help when things get a bit more involved.

The question (condition) asked is: Is the number returned by Clock.Hour more than 12. 

If it is then the program displays the greeting ‘Good afternoon’, otherwise the program skips that section up to the ‘Else’ keyword and displays the greeting ‘Good morning’.  Only one of the messages is displayed.  The block then ends and the next line of code is  executed so you always see the message: ‘Have a good coding day’.

The above example is not strictly correct – it doesn’t quite work as expected.  Can you see why?  If you can then correct it.  You should also try using different questions as it can also be written in a number of different ways and still get the required output.  This is true of ALL conditional statements – there are always several different ways to write them.

Using AND

Sometimes we need to check more than one value at the same time and although this could be done by placing a second question inside the first, it is possible to combine the questions together using the keyword ‘AND’.  In this situation, both questions have to return ‘True’ in order for the decision itself to return ‘True’.  If either question is false then the whole question is false.  Se my example below.

Code and flowchart

Enter this code and see for yourself that it works.  This format is very useful if you need to select a range between two values.

It is possible to make it even more ‘selective’ by adding yet more ‘conditions’.  For example, you could also ask if the weekday is a school-day so that it it ‘deals’ with weekends.  However, beware of making them too complex – it can get very confusing.

Using OR

Another way of combining questions or conditions is with the use of the keyword OR.  This enables two (or more) questions to be asked with any of them being true making the whole process true.

Flowchart and coding for ORUsing OR enables you to test if values are outside a range of values.  In this case it tests for before 7am and after 9pm.  Enter this code to check that you can do it.  Try changing the conditions to see what happens.  Using decision making is a key skill.

You can  click on any of these images to view them full size.

Comparing Values

This is used to test if two values are exactly the same.  The values can be literal, i.e. a specific value entered directly into the code or variables.
Symbol used: =
Example: If ( Name = “Jane” ) Then… (Does the variable ‘Name‘ contains the letters making the word ‘Jane‘?)
This is used to test if two values are NOT exactly the same.  Again the values can be a literal and a variable or two variables.
Symbol used: <>
Example: If (Clock.Hour <> 6) Then… (It is not between 6 and 6:59:59 999 in the morning)?
This is used to see if the first value / variable provided is bigger then the second value/variable provided.  This works with words as well as numbers with “B” being greater than “A”.  Note that “a” is also greater than “A” because of the way characters are coded using the ASCII or UTF-8 coding formats.
Symbol used: >
Example: If (Clock.Day > 29) Then… (Is the day of the month greater than 29?  If it is it cannot be February!)
This tests both equality and being greater than.  It always compares the first value with the second so again to return “True” the first value must be the same or more than the second value.
Symbol used: >=
Example: If (TextWindow.CursorLeft >= 10) Then… (Is the cursor more than 10 character across the screen – something has already been typed on the screen or the cursor has been moved.)
You can use this to test  to see if the first value/variable is smaller than the second value/variable.  Again, this works with both letters and numbers.
Symbol used: <
Example:  If (Clock.Year < 2020) Then… (It’s not the Year 2020 yet)
This condition enables you to test for equality and being less than.  As before it always compares the first with the second value/variable and returns “True” if the first value is the same as or less than the second.
Symbol used: <=
Example: If (Clock.Month <= 3) Then… (It’s Winter)
In all the above examples, the brackets are not really required but it is a good habit to get into for when your decision rules become more complex.

End of Topic Tasks