Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width.

Similar presentations


Presentation on theme: "Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width."— Presentation transcript:

1 Computer Science 1000 Algorithms III

2 Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width in other words, we will have to read two values from the user how do we do this?

3

4 Multiple Inputs our process seems correct, as the system is asking the correct questions, but our output is wrong. why? the answer block can only hold one value when we ask the first question, it reads the length, and stores it in answer when we read the width, it clobbers the value that was read for length hence, it is incorrectly computing area as width x width to see this, we can watch what value is stored in answer by checking the checkbox beside its block

5

6 Multiple Inputs if answer can only hold one value at once, how can we read in two values? we need to store the first value somewhere where it won’t get overwritten to do this, we will declare a variable

7 Variable a variable is a named location in memory for storing a value simply stated, it is a place for our program to store a value to use a variable, we must first declare it you must give your variable a name it is helpful to use a name that you will remember, and that is meaningful

8

9 Variable to store a value in a variable, use the following block: in the right input, you can put a value, or an expression that value will be stored in the variable

10 Notice that the value changed.

11 Obtaining the value of a variable the variable’s block can be used as the input to another block modify our previous example so that the sprite says what is stored by the variable.

12

13 Back to our example … recall that in our previous example, we could not store two inputs, as the second input clobbered the first to mitigate this, let’s store our first input in a variable called length let’s store the second input in a variable called width

14

15 Variables notice that the answer block and a variable block behave very similarly in fact, answer is just a predefined variable, with the exception that you cannot delete it

16 Boolean Expressions like Excel, Scratch supports Boolean operators two values true false three operators less than equal greater than

17

18 Write a Scratch program that asks the user for their age, and prints true if that user is old enough to vote in Canada, and false otherwise

19

20

21 Boolean Operators like Excel operators, we can use the Boolean operators to compare strings note that they are case-insensitive

22 Example: Write a program that asks for the user’s name, and prints true if the user has the same name as you, and false otherwise.

23

24 Events and Booleans you may have noticed that both the Boolean operators and the event operators have the same hexagon shape in fact, the two are very similar the event operators can be considered to be true/false statements true when the event occurs false otherwise for example, consider the touching event this event is true when Sprite 1 is touching Sprite 2, and false otherwise

25 Events and Booleans because of their similarity, we can use events in a similar way to Booleans recall how we could print true and false from our sprites using Boolean expressions write a program for Sprite 1 that prints true if it is touching Sprite 2, and false otherwise

26

27

28 Events and Booleans in some cases, we can use Booleans where we previously used events example: the y position block holds the vertical position of the sprite higher the sprite, greater than y we can see the value in this block by checking the box beside it

29

30 Events and Booleans write a program where the sprite prints “Put me down” whenever it is lifted above 50 units

31


Download ppt "Computer Science 1000 Algorithms III. Multiple Inputs suppose I ask you to write a program that computes the area of a rectangle area = length * width."

Similar presentations


Ads by Google