14 Conditional instructions (if-then rules)

If [latex]x[/latex] then [latex]y[/latex]

We’ve seen that algorithms often will use these logical rules with the structure below:

If Then
[latex]P[/latex] [latex]Q[/latex]

In these cases, P is some proposition or statement that can be evaluated as TRUE or FALSE and Q is the instructions to take.  For example, with a primes algorithm, we might have the instruction:

If Then
[latex]a[/latex] is not divisible by [latex]b[/latex] move on to the next divisor.

The statement ‘a is not divisible by b’ will either be true or false. If the statement ‘a is not divisible by b’ is FALSE, then ‘a is divisible by b’ is TRUE.  Double negatives like this can be useful, but make sure you keep track of them so that you don’t get confused later on.

If-then

For example, suppose we are checking if the number 251 is prime.

  1. First, we check whether 251 is not divisible by 2.  Clearly this is TRUE (as [latex]\frac{250}{2}=125[/latex] with a remainder of 1.  Therefore 251 is truly not divisible by two).
  2. Since the condition ‘a is not divisible by b’ has been met, the algorithm will move onto the ‘then’ part which will be the next number to check (3).

The problem is that we need to tell it how to do this as well, which we’ll look at when we come to variables.

If-then-else

If the statement is not true, the action after the ”then” won’t be executed and the algorithm will just act on the next line of script.  In some cases, we might have two potential actions depending on whether the first statement is true or false.  For example, with the primes, we might have the following:

If Then Else
[latex]a[/latex] is divisible by [latex]b[/latex] STOP.  The number is not prime. Move to the next divisor

In this case, we didn’t use the negative form of the statement P, but we could also have done it the other way around.

If Then Else
[latex]a[/latex] is not divisible by [latex]b[/latex] Move to the next divisor STOP.  The number is not prime.

Let’s have a look at some examples of how to use the if-then rules in Scratch.

Transcript

Draw an octogon

Can you create a program in Scratch that:

  1. make the cat trace out an octogon (you’ll need to use pen blocks)
  2. asks the user how many sides of a polygon they’d like drawn, and then draws it
  3. Asks for an input, and, if it’s divisible by 5, it does so.  If not, it doubles the number.

This is a short quiz that will help you get to know some of the terminology we will use when talking about Scratch and coding as well as some of the features of Scratch.

How are you enjoying Scratch?

Remember that to improve your coding, the more you practice and explore, the better you’ll get at debugging and ensuring that your code does what you want it to do.   A web search on how to do something will often be fruitful too (one of the pieces of advice in the What to do if you get stuck section of the chapter on attack).

License

Icon for the Creative Commons Attribution-NonCommercial 4.0 International License

Mathematical Reasoning and Investigation Copyright © 2023 by Deakin University is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License, except where otherwise noted.

Share This Book