Download presentation
Presentation is loading. Please wait.
1
Programming with Alice
Decision Structures
2
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 This is known as SEQUENTIAL PROGRAMMING It fine as a method, but this means our program will always give the same result Step 3a and Step 1 Step 2 Start Step 4 End Step 3b
3
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
4
Programming with Alice Decisions Structures
We want our program to follow: One set of commands for a given circumstance 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
5
Programming with Alice Decisions Structures
In Alice, it looks like this… IF condition is true, do Command #1 Else, do Command #2 CONDITION IF/ELSE Structure
6
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
7
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 the question. Question Boolean Variable Do you want a cookie? wantCookie Do you wish to exit this program? readyToExit Has the user entered a valid password? validPassword
8
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.
9
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 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. A Pseudocode version of this program might look something like this
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.