Programming with Alice Decision Structures Programming with Alice









- Slides: 9

Programming with Alice Decision Structures

Programming with Alice Decisions Structures Up until now, our programs have followed our instructions in the order we gave them – The program follows the commands in the order they are given – They may occur at the same time, but always in the same order Start Step 1 Step 2 Step 3 a and Step 3 b Step 4 End This is known as SEQUENTIAL PROGRAMMING It fine as a method, but this means our program will always give the same result

Programming with Alice Decisions Structures In most programs however, we want the computer to behave differently depending on the conditions To make the computer respond in different ways, the program needs to offer choices – The simplest choices have two options, usually expressed as a pair of opposites: Yes/No Left/Right or True/False

Programming with Alice Decisions Structures We want our program to follow: 1. One set of commands for a given circumstance 2. Another set of commands for a different circumstances This is done with a DECISION STRUCTURE – all decisions in the computer are based on the idea of IF/ELSE decision structure IF this is true, do COMMAND A ELSE [this is false (not true)], do COMMAND B

Programming with Alice Decisions Structures In Alice, it looks like this… CONDITION IF/ELSE Structure – IF condition is true, do Command #1 – Else, do Command #2

Programming with Alice Decisions Structures IF/Else will only work with a true/false case – Therefore, our CONDITION must be a true/false value This is called a BOOLEAN VALUE Boolean values can be: – A BOOLEAN VARIABLE – The result of a BOOLEAN FUNCTION

Programming with Alice Decisions Structures Boolean Variables Recall that a variable is a named location in the computer's memory which stores useful information. A Boolean variable stores a Boolean value – True or False A boolean variable is often used to store the answer to a question that can be answered using a Boolean value (i. e. , only two possible answers) We try to name our variables to reflect Question the question. Do you want a cookie? Boolean Variable want. Cookie Do you wish to exit this program? ready. To. Exit Has the user entered a valid password? valid. Password

Programming with Alice Decisions Structures Boolean Functions Recall that functions are a special type of method that return a value. This value can be a number, string, or Boolean. Alice includes many primitive functions that return Boolean values Notice that many of them could easily be phrased as a question, where the answer could be Yes/No, or True/False. – Many Boolean functions can be identified by their use of the word, “is”, in order to form their question e. g.

Programming with Alice Decisions Structures For example, suppose we have a penguin trying to jump through a hole in the ice to reach the water below. – This plan will work fine if the hole in the ice is big enough. If the hole is too small (or the penguin is too big) then the penguin won't be able to go for a swim If the hole is wider than the penguin A Pseudocode version of this program might look something like this penguin falls down into the hole Else penguin says “Drats!” End If The condition, in this case, is whether or not the hole is wider than the penguin. – Notice that there is no possibility for both actions to be performed It is one or the other. Notice how we have used indenting to separate the branches of the decision statement. – Indenting is a good practise to help keep our code, or pseudocode, visually clear and organized.