Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean variable –what sequence structures are and when used –what decision structures are and when used –the difference between dual-alternative and single-alternative decision structures –what nested instructions are –what relational operators are and how they are used –what an infinite loop is –what conditional loops are

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-2 Today’s Agenda 1.Boolean Values 2.The If / Else Decision Structure 3.Relational Comparisons and Logical Operators 4.The Loop Instruction 5.The While Instruction

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-3 Boolean Variables Boolean Variables hold only two values –True –False –Initial Value is usually set to be “TRUE” 4.1

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-4 Boolean Functions Boolean Functions return one value from the choice of –True –False Also primitive functions that are Boolean –User prompted to answer Yes (true) or No (false) 4.1

5 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-5 The If/Else Decision Structure If/Else decision executes one set of instructions if a Boolean condition is true and a different set if the condition is false All previous instructions have been consecutive (sequence structure), that is: –One –After –The –Other 4.2

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-6 If/Else If/Else is a decision structure Instruction tests a condition (which gives a Boolean value) and then based on a true value does task “A” or task “B” if the value was false BOTH actions will NOT be performed…only ONE can be! If the hole is wider than the penguin penguin falls down the hole Else penguin says “Drats!” End If TRUE FALSE 4.2 The penguin falling or speaking are instructions that are conditionally executed –They do NOT always execute –Executed only under certain conditions

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-7 Decision Structure 4.2 Penguin falls down into the hole Flowchart If/Else Tile

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-8 If/Else Note to empty “slots” in instruction. –IF part –ELSE part The TRUE placeholder is replaced with the Boolean variable or a Boolean function Instructions tiles added to both the IF part and the ELSE part More than one instruction can be added to both the IF and ELSE part! Empty If/Else instruction 4.2

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-9 Single-Alternative Decision Structures If/Else is a dual-alternative decision structure –Two paths of execution: one following TRUE and the other following FALSE Single-alternative decision structures are similar to the dual-alternative –The ELSE part is empty! 4.2

10 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-10 Single-Alternative Decision Structures Start Penguin turns to face hole Hole wider than penguin? End Penguin walks to center of hole Penguin falls into hole 4.2 T F

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-11 Nested If/Else Instructions An If/Else instruction is placed inside another If/Else instruction or nested. The inner If/Else executes only if the outer If/Else is true. 4.2

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-12 Nested If/Else Instructions 4.2 Start Penguin faces hole Within 2 meters “Too Far!” True Hole wider? “Drats!”Penguin walks Penguin falls in End True False

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-13 Relational Comparisons Relational Operators used to compare values and determine whether relationships such as greater than, less than, or equal to exist Compare two values and determine how they relate to each other 4.3 OperatorMeaning = Equal to ! =Not equal to >Greater than > =Greater than or equal to <Less than < =Less than or equal to

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-14 Relational Operators Binary…operate on two pieces of data Can use to create conditions in an If/Else instruction Alice uses “a” and “b” for placeholders –Drag the desired tile and replace either the “a” or “b” with a value…such as an object 4.3

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-15 Logical Operators Tests more than true/false…can do complex testing! –Test two conditions to see if they BOTH are true! –Or ONLY one condition is true! –Or NEITHER condition is true! 4.3

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-16 The Loop Instruction Loop instructions cause one or more other instructions to repeat or loop a certain number of times. 4.4

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-17 The Loop Instruction The Loop instruction has an empty slot where instructions can be inserted. Look at the complicated version… 4.4

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-18 Computing the Number of Times to Repeat What if you want the something to loop and NOT have to specify a particular number of repetitions? –For example…getting a ball to roll across the screen, regardless of where the ball is initially placed –Use the soccerBall’s distance function to calculate the distance –Loop uses integers (whole numbers) and decimal portions are dicarded –Objects loop 4 times, not 4.8; 3 times, not 3.4 4.4

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-19 Infinite Loops Infinite Loops…the loop NEVER stops! –Use for objects that shouldn’t stop! If placed in consecutive order…the next instructions will NEVER occur, since the loop NEVER ends! Place an infinite loop into a Do Together structure with other items. 4.4

20 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-20 Flowcharting a Loop Instruction Set index to 0 Index < 12? Clock, minute roll left 1 revolution Clock, hour roll left 1/12th revolution Add 1 to index T 4.4

21 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Your turn – try these exercises Try exercises 3, 4, and 6 on pages 66-67 Plane crash –Add a biplane object to the top of the world and have it fall from the sky –Use a conditional expression to check when the plane hits the ground –Have the plane say “Oops” when it hits the ground 4-21

22 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-22 The While Instruction While instruction is a loop that repeats as long as a Boolean condition is true Second repetition structure Called conditional loop…since the loop is controlled by a condition Let’s look at a tennis racket example 4.5

23 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-23 Flowcharting a While Instruction Loop’s condition is tested before each repetition of the loop First, it tests condition –If true…performs a repetition and starts over –If false…the loop terminates 4.5 Start Distance > 0? Racket turns forward Ball moves forward End

24 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Your turn – try these exercises Try the RescueSea tutorial (handout) Try the Roman Numeral Exercise –Create an interactive world that asks the user to enter a number that is at least 1, but not more than 10. Select an Alice character who will say the Roman Numeral equivalent of the number entered by the user. If the user enters a number outside the range of 1-10, the character should ask the user to enter another number Goal keeper –Create a world with a soccer net and a ball –Have the ball go into the goal as a goalkeeper dives to try to stop it 4-24


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-1 Lecture Objectives To understand: –what values can be stored in a Boolean."

Similar presentations


Ads by Google