Download presentation
Presentation is loading. Please wait.
Published byEdward Higgins Modified over 9 years ago
1
in
2
This presentation will only make sense if you view it in presentation mode (hit F5). If you don’t do that there will be multiple slides that are incomprehensible. When you are done you will have a Scratch program that has code to demonstrate the programming concept ‘Conditional Statements’. The easiest way to move back and forth between this presentation and Scratch is to use alt-tab.
3
Conditional statements are a useful and POWERFUL concept in computer programming. Conditional statements are what allow the computer to ‘think’. They do this by comparing and analyzing data. The computer will make a decision about what to do based on the condition of the data.
4
Computers do REALLY well with simple comparisons. Conditional statements work by evaluating a condition to see if it is TRUE or FALSE. If the condition is TRUE one set of actions can be performed. If the condition is FALSE a different set of actions can be performed. Lets see what that looks like in action…
5
1) Lets say that you needed to write a program to perform two different actions based on whether or not it was raining. 2) The condition that will be tested is a simple one. 3) IS IT RAINING? 4) There are only two possible outcomes to this condition. 5) It can be either: TRUE or FALSE. 6)This program would be written so that if it was raining you will bring your umbrella with you. 7)If it is not raining you will go to the beach. 8)The computer can perform two separate actions based on one simple question. 9)That’s a very simple explanation of how Conditional Statements allow the computer to think. Is it raining?
6
You will create a program that will have a Sprite move 10 steps to the right any time it is located on the left hand side of the screen. Things to note: The movement will be triggered by the space key The starting location of the sprite should be X -200, Y 130 If the sprite is located on the right hand side of the screen then nothing should happen when the space key is pressed DO NOT DO THIS ON YOUR OWN! Simply keep going through this presentation and you will be walked through this one step at a time.
7
Make sure Scratch is open. Choose ‘File/New’ to get a new Scratch file. The file name for this assignment will be ‘Conditional Statements Intro’
8
This program would be impossible to complete without conditional statements. Conditional statements will allow the computer to ‘think’ and to determine a course of action based on the sprites current location on the screen. Lets take a look at how to make this work…
9
The ‘If’ statement is a conditional statement In Scratch it looks like this…
10
For an IF statement to work it needs to have something to compare. Our example will look at the position of our sprite on the screen. It will then evaluate whether or not the sprite is located on the right half of the screen. If we are on the right half of the screen nothing should happen. If we are on the left half we should move 10 steps to the right.
11
Here is a quick review of how the Scratch screen is arranged. The Scratch stage is 480 pixels wide and 360 pixels high. -240 240 180 -180 0,0 at the center of the stage Moving right increases the x value Moving left decreases the x value X Y Moving up increases the y value Moving down decreases the y value X Scratch will always show you the X and Y position of your Sprites. Y
12
Remember how we said that the IF statement needed something to compare? The first block has nothing to compare… Go ahead and make yours look like the second block. I will explain exactly what that means on the next slide.
13
X position will tell the computer what the CURRENT x position of your sprite is. 0 represents the middle of the screen both vertically (up and down) and horizontally (side to side). This statement will compare the X position of your Sprite to see if it is less than 0. If your sprite is on the left hand side of the screen it will perform whatever actions you place inside of this block. It will do that because the condition you are testing is TRUE.
14
So now that we have the comparison figured out we need to figure out what we want the sprite to do when x position is < 0. We want it to move 10 steps to the right. In order to do that we simply insert one line of code inside the if block. Go ahead and add that code into the block.
15
The trigger is the space key. The condition will only be tested when the space key is pressed. Remember that the condition being tested only returns one of two values. It is either TRUE or it is FALSE.
16
Go ahead and test your code. What happens? Is it what you expected? Look at the x position of your sprite every time you hit ‘space’ Is it changing? What happens when X is greater than 0?
17
If statements test a single condition. If that condition is true whatever code is inside the if block will be executed. If that condition is false NOTHING will happen.
18
If / else statements test a single condition. If that condition is true whatever code is inside the top portion will be executed. If that condition is false whatever code is in the bottom or ‘else’ portion will execute.
19
Additional Specs Trigger is the ‘m’ key Action: If your sprite is on the left hand side of the screen it should move ten steps to the right. If it is on the right side of the screen it should say ‘Stop trying to make me move. I am already on the right side.’ for 2 seconds. Hint 1 Hint 2
20
o The two blocks of code to the left do the exact same thing. o However, one block is BETTER than the other. o Can you figure out which block is better AND what makes it better?
21
o In the top block only 1 comparison is needed. o The bottom block uses 2 comparisons to accomplish the same thing. o Therefore you can say that the first block is more efficient because the computer only has to do 1 comparison. o It is VERY important as a programmer to write efficient code. 1 1 1 1 2 2
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.