Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition."— Presentation transcript:

1 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition by Tony Gaddis Chapter 8: Decision and Repetition Structures

2 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-2 Chapter 8 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

3 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-3 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

4 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-4 Boolean Values True or False? –Boolean values are abstract concepts used in computations? TRUE! 4.1

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

6 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-6 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

7 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-7 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

8 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-8 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 If the hole is wider than the penguin penguin falls down the hole Else penguin says “Drats!” End If TRUE FALSE 4.2

9 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-9 If/Else BOTH actions will NOT be performed…only ONE can be! The penguin falling or speaking are instructions that are conditionally executed –They do NOT always execute –Executed only under certain conditions If the hole is wider than the penguin penguin falls down the hole Else penguin says “Drats!” End If TRUE FALSE 4.2

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

11 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-11 Decision Structure 4.2 If/Else Tile

12 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-12 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

13 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-13 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

14 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-14 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

15 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-15 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

16 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-16 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

17 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-17 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

18 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-18 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

19 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-19 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

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

21 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-21 The Loop Instruction The Loop instruction has an empty slot where instructions can be inserted. 4.4

22 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-22 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 to 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

23 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-23 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

24 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-24 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

25 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-25 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

26 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-26 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 4.5

27 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4-27 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


Download ppt "Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Alice: A Visual Introduction to Programming First Edition."

Similar presentations


Ads by Google