ifstudents learnthis computer will Get Smart Objectives 1
if(students. learn(this)) computer. will. Get. Smart(); Objectives: 1. Evaluate and write conditional structures. 2. Create Boolean expressions
Boolean • Boolean is something that is true or false For example: • If you do the dishes • If you look good • if (volume > 10) Springbrook High School page 2
Review • A conditional is a statement that says if something is true, then something will happen as a consequence. For example: • If you do the dishes, then I will give you allowance. • If you look good, you can go to the party. • if (volume > 10) radio. set. Volume(2); Springbrook High School page 3
Relational Expressions • A relational operator is used to compare two values, resulting in a relational expression. For example: • number > 16 The result of a • grade = 'F' relational expression is a boolean value of • passing >= 60 either true or false. Springbrook High School page 4
Logic review 1. 2. 3. 4. 5. 6. 7. true 5>2 7. 8 < 32. 9 true 89/23 < 0 false 452 <= 0 true 452 >= 0 not(452 = 0) true false 452 == 0 Springbrook High School page 5
Relational and Logical Operators • A relational operator is a binary operator that compares two values. < > = And Or less than greater than equal to asks if 2 things are both true asks if at least 1 thing is true Springbrook High School page 6
not operator NOT is also called an inversion • • false not(true) true not(false) Not(true) =false not(5 < 7) not(5 < 7 and 3 >= 3) Not(true and true) =false Springbrook High School page 7
Evaluate not(true) or not(false)) • not( false or true ) • Not ( true ) • false Springbrook High School page 9
Boolean variables • • Set answer to 5 > 6 Set x to y > z Set date to boy. Age > girl. Age + 2 Set reload to ammo. In. Clip = 0 • These variables can be used as conditions Springbrook High School page 10
if-else statement if (expression) Write an { Check the first statement 1; condition, and if it is true then do statement 1. } else If the first condition is false… do statement 2. { statement 2; } Springbrook High School if-else: page 11
if - flowchart condition evaluated true statement 1 Springbrook High School false statement 2 page 12
Boolean conditions Boolean statements go here if (expression) statement 1; else statement 2; Springbrook High School page 13
Boolean conditions Set time 4 more to ammo. In. Clip = 0 and ammo. Available > 0 if (time 4 more) reload(); else shoot(); Springbrook High School page 14
Boolean conditions Set expression to cool > 7 or funny > 5 if (expression) befriend(person); else ignore(person); Springbrook High School page 15
When to use an if-else Ask Do you have room for more ice cream? Set full. Stomach to (answer=no) if (full. Stomach) Say(“No thank you”) if ( not full. Stomach) Say(“Yes, thanks, I’d love some more…”) if (full. Stomach) Say(“No thank you”) else Say(“Yes, thanks, I’d love some more…”) Springbrook High School page 16
Boolean worksheet questions • You have reached the new high score in Donkey Kong if your score is over 1, 050, 200 points. • The variable score has been set to your score set high. Score to ( score > 1050200 ) if (high. Score) Say(“You got the high score!”); else Say(“Try again. ”); Springbrook High School page 17
- Slides: 16