Presentation is loading. Please wait.

Presentation is loading. Please wait.

Conditionals.

Similar presentations


Presentation on theme: "Conditionals."— Presentation transcript:

1 conditionals

2 if .. then .. else .. if light is red then stop car else keep going
true or false if light is red then stop car else keep going happens when true happens when false

3 if .. then .. else .. if hungry then eat else do nothing true or false
happens when true happens when false

4 exercise Find 2 situations in your life where you use if then else if then else Situation 1 Situation 2

5 true/false maths known as Boolean algebra
a Boolean condition may be true or false or true false and true false not true false

6 exercises ► not true = ? ► not (not true) = ? ► true and false = ? ► false or true = ? ► true or (not true) = ? ► (not true) and (not false) = ? ► (not true) or (not false)= ? in doubt, use the tables…

7 inequalities Arithmetic inequalities evaluate to true or false x < y = true if x is strictly less than y; otherwise it is false ► 1 < 0 = ? ► 0 > 1 = ? ► -10 < 1 = ? ► 5 ≤ 5 = ? ► 0 < 1 or 1 > 0 = ? ► x < x + 5 = ?

8 exercises-solve ► not (0 < 1) = ? ► not (not false) = ? ► false and true = ? ► false or (0 < 1)= ? ► true or (5 > 10) = ? ► true and (false or not false) = ?

9 true/false maths known as Boolean algebra
complete true or false tables or true false and true false not true false

10 exit task - create 1) six booleans Qs front 2) booleans answers back

11 magic-8-ball famous fortune telling game
“think really hard about your question, ask the ball, get funny answer”

12 vocabulary math->random(n) -> 0,1,2…., n-1 returns a number in [0,n] ie. math -> random (3) -> 0,1,2

13 question var x := math -> random (3) if x = 0 or x = 2 then
What does this do? var x := math -> random (3) if x = 0 or x = 2 then 1 -> post to wall else x -> post to wall

14 exercise write the magic-8 ball on shake,
use math->random to get a random number (say ‘x’) use if statements to pick a predefined answer based on ‘x’ (you need at least 4 different answers).

15 pseudo-code var x := math->random(3) // x is a random number of value 0, 1 or 2 if x = 0 then // display a message “this is looking good”->post to wall else // deal with x = 1, or x = 2 if x = 1 then “no way”->post to wall …

16 homework design your magic-8-ball game 6 different possible answers
1 option should draw a turtle shape 1 option should change the phone color 3 options should provide a picture 1 ‘customization’ of your own (make it fun, this is your app!)

17 reference For reference, these are answers from the original game. ● It is certain● It is decidedly so● Without a doubt● Yes – definitely● You may rely on it● As I see it, yes● Most likely● Outlook good● Signs point to yes● Yes ● Reply hazy, try again● Ask again later● Better not tell you now● Cannot predict now● Concentrate and ask again ● Don't count on it● My reply is no● My sources say no● Outlook not so good● Very doubtful

18 if then else choice, part iii

19 exercise Write a script that prints the numbers 1 to 10 to the wall, skipping 4. There are two good ways of doing this, but both use for loops. Once you have one, see if you can find the other.

20 question var x := math -> random (3) if x = 0 or x = 2 then
What does this do? var x := math -> random (3) if x = 0 or x = 2 then 1 -> post to wall else x -> post to wall

21 practice for 0 ≤ i < 4 do var x := i * 3 if x > 5 then else
x → post to wall else i → post to wall var x := 3 if x = 2 then “Yes” → post to wall else “No” → post to wall if x > 2 then

22 if statements choice, part vi

23 homework publish your script to get the script id log in
open homework script tap on the “up button”, “publish” wait still synching is finishing mark id and return paper

24 if – then – else What will this code do? var x := 5 if x < 4 then
“purple” -> post to wall else “white” -> post to wall

25 if – then – else What will this code do? var x := 3 if x ≤ 4 then else
“brown” -> post to wall else “red” -> post to wall

26 practice var x := 1 var x := 3 var y := 3 if x > 2 then
if x > y then “green” → post to wall else “black” → post to wall “white” → post to wall var x := 3 if x > 2 then “pink” → post to wall else “orange” → post to wall

27 if – then – else var x := 3 if x < 7 then else x → post to wall
x := x * 2 else x := x – 5 x → post to wall

28 practice var x := 0 var x := 6 var y := 3 if x = 2 then
x → post to wall else y → post to wall var x := 6 if x = 2 then x := 4 else x := x + 2 x → post to wall

29 quiz

30 guessing game Pick a number between 1 and 20
Get someone to guess your number Tell them whether they’re right, too high, or too low Guess until they get the right answer Think about how a computer would have to do this

31 user input The first step is to take user input
There’s an option on your phone called wall → ask number It returns the number given by the user (store it in a variable!)

32 guessing Have your phone pick a random number, then ask for a guess.
Tell the user whether they guessed right or wrong

33 guessing The next step is to tell the user whether they guessed too high or too low.

34 guessing One guess probably won’t be enough, so try to give the user two guesses There’s more than one way to do this

35 guessing Two probably still isn’t enough, so see if you can use a for loop to give the user even more guesses

36 challenge If you have everything else working, see if you can figure out how to give the user an unlimited number of guesses

37 if statements choice, part v

38 guessing game Pick a number between 1 and 20
Get someone to guess your number Tell them whether they’re right, too high, or too low

39 guessing game action guesser // pick a number between 0 and 20 var secret := math->random(21) // get someone to guess your number var guess := wall->ask number(“Guess”…) // Tell them too low if guess < secret then “too low”->post to wall else // Tell them whether too high if guess > secret then “too high”->post to wall // Tell them whether they’re right “you win!”->post to wall

40 math game write a game where the user has to calculate the sum of 2 numbers

41 fill the holes /iiyh action math game
// get a random number and store it in ‘x’ // get a random number and store it in ‘y’ // ask the user for sum of both numbers // and store his number in ‘answer’ // check whether the answer is correct if then // show the victory message | else // show the failure message |


Download ppt "Conditionals."

Similar presentations


Ads by Google