programming with logical structures - mr. harris 3/project 3 ppt...project 3: programming with...
TRANSCRIPT
Alice 2.0Introductory
Concepts and Techniques
Project 3
Programming with Logical Structures
Project 3: Programming with Logical StructuresProject 3: Programming with Logical Structures 2
Project Objectives
• List and describe the three major elements of
logical structure found in algorithms
• List several criteria that should be met by each
linear sequence in an algorithm
• Describe the difference between binary bypass
and binary choice branching routines
• Describe the difference between a pretest loop
and a posttest loop
• List and describe the four major parts of a count-
controlled loop
Project 3: Programming with Logical StructuresProject 3: Programming with Logical Structures 3
Project Objectives
• List and describe the six logical comparison operations
used in computer programming
• List and describe the three major operations in Boolean
logic
• Create methods in Alice that implement each of the
following
– a binary bypass
– a binary choice
– a count-controlled loop
– a sentinel loop that is not count controlled
• Create a compound Boolean expression in an Alice
looping or branching instruction
Project 3: Programming with Logical Structures
Project Overview
• Project 1 (skaterHello.a2w)
• Project 2 (skaterRoutine.a2w)
• Project 3 (skaterLogic.a2w)
– Modify the skaterRoutine.a2w
– Use logical structures to control the behavior of the skater
– Change the documentation
– Add a flag pole to the world
Project 3: Programming with Logical StructuresProject 3: Programming with Logical Structures 5
Creating a New World
from an Existing World – pp. 134-137
• Start Alice
• Open skaterRoutine.a2w
• Update documentation
– Program name
– Date
– Program description
• Save as skaterLogic.a2w
Project 3: Programming with Logical Structures
The Logical Structure of Algorithms
• Algorithm – a step-by-step process
• OOP – each method is an algorithm
• Corrado Bohm and Giuseppe Jacopini (Italian
mathematicians)
– 1960s
– Showed that all algorithms are composed of three
elements of logical structure
• Selection sequences – branching routines
• Repetition sequences – loops
• Linear sequences – in order
Project 3: Programming with Logical Structures
Flowcharts
• Graphical depiction of an
algorithm using symbols
• Flowcharting template
Project 3: Programming with Logical Structures
Linear Sequences
• All the previous Alice methods written have been linear sequences –executed one after the other
• Steps executed one after another with no looping or decisions
• Criteria to meet
– Clear starting and ending points
– Entry and exit conditions clearly stated
– Sequence of instructions are complete
– Sequence of instructions are in the proper order
– Each instruction in sequence needs to be correct
Project 3: Programming with Logical Structures
Selection Sequences – Branching
• Occurs whenever the flow of instructions splits into separate paths
• Must have a condition to determine the path to follow
• Binary branching
– Flow of logic splits into two possible paths
– True-or-false condition
• Multiple branching
– Flow of logic divides into more than two paths
– Single condition with many possible values
BinaryMultiple
Project 3: Programming with Logical Structures
Binary Branching
• Binary bypass
– Instruction is either executed or bypassed
– It is possible that nothing will happen
• Binary choice
– One of two instructions, but not both, is executed
– One of the two instructions will definitely be executed
• If/Else instruction tile (bottom of Editor area)
Bypass Choice
Project 3: Programming with Logical Structures
Binary Bypass – pp. 141-144
• Built-in functions (World object)
– Day of year – used to return the day of the year from the computer’s system clock
– a==b – function used to see if the day of the year is equal to 1
Project 3: Programming with Logical Structures
Binary Choice – pp. 144-146
• AM built-in function (World object)
– Returns a value of true if the time on the system clock is before 12:00 PM
– Returns a value of false if the time on the system clock is 12:00 PM or after
Project 3: Programming with Logical Structures
Multiple Branching
• Questions with many
possible answers
• Alice does not have
an instruction for
multiple branching
Visual Basic Statement
Project 3: Programming with Logical Structures
Multiple Branching
• Same results
can be achieved
by asking a
series of binary
questions
(If/Else
instructions)
Series of Binary
Branching Routines
Series of Nested
If/Else Instructions
Project 3: Programming with Logical Structures
Repetition Sequences - Looping
• Formed whenever a branch in an algorithm leads
back to a previous instruction
• While
– True – block of code is executed
– False – block of code is ignored
Project 3: Programming with Logical StructuresProject 3: Programming with Logical Structures 16
Adding a Flagpole to the skaterLogic
World – pp. 150-152
• ADD OBJECTS button – Objects/Flagpole
• Drag flagpole to back of world
• Resize flagpole – (right-click and methods)
Project 3: Programming with Logical Structures
Capturing the iceSkater in a New
Pose – pp. 153-156
• Object tree – iceSkater tile
– Select left lowerLeg
– Methods (right-click) – change
position of left lowerLeg
• Properties tab
– Capture pose
– Name it – leftFootUp
Project 3: Programming with Logical Structures
Creating the glideStep method – pp.
156-160
• iceSkater will push and glide until she is within
three meters of the flagpole – loop
• iceSkater will move one meter while pushing
with her left foot
• iceSkater will glide two meters on her right foot
with her left foot raised behind her (left FootUp
position)
Project 3: Programming with Logical Structures
Creating the GlideStep Method – pp.
156-160
Project 3: Programming with Logical Structures
Adding a While Loop to the Closing
Method – pp. 160-163
• iceSkater
– Turns to face flagpole
– Continues to execute glide steps while she is more than three meters away from the flagpole
– Loop will stop when she is <= three meters from the flagpole
• While instruction tile (bottom of Editor area)
• a > b function (World object)
Project 3: Programming with Logical Structures
Pretest and Posttest Loops
• Pretest loop– Test occurs before the body of the loop
– Body of loop does not have to execute
– While
• Posttest loop– Test occurs after the body of the loop
– Body of loop will execute at least once
– Alice does not have an instruction for a posttest loop
PretestPosttest
Project 3: Programming with Logical Structures
Count-Controlled and Sentinel Loops
• Sentinel loop
– Marker or condition that tells the computer when to
stop executing a loop
– Every loop is a sentinel loop (no infinite loops)
• Count-controlled loop
– Sentinel loop in which the sentinel is a counter that
is not allowed to go beyond a certain value
Project 3: Programming with Logical Structures
Count-Controlled Loops
• Counter
– Variable that is updated each time the program runs through the loop
– Must have an initial value, a final value, an increment
• Four parts
– Initialization – sets the first value of the control variable
– Test – checks the control variable to see if the loop should be executed
– Processing – repeats the instructions in the body of the loop
– Update – changes the value of the control variable each time the loop is executed
Project 3: Programming with Logical Structures
Loop Instruction
• Simple version – shows only the number of times the loop
is to be repeated
• Complicated version – shows the control variable, named
index, initial value, final value, increment value
• Loop instruction tile (bottom of Editor area)
SimpleComplicated
Project 3: Programming with Logical Structures
Adding a Count-Controlled Loop to
the Routine Method – pp. 166-167
• ice-Skater will repeat the figure eight three times
Project 3: Programming with Logical Structures
Comparison Operators
• Operators that allow for a comparison of two
values
Project 3: Programming with Logical Structures
Boolean Logic
• George Boole (mathematician)
– 1854
– Boolean logic – only values used are true and false
• Three basic operations
– AND
– OR
– NOT
Project 3: Programming with Logical Structures
Adding a Compound Boolean Expression
to the Opening Method – pp. 169-173
• December 22 – shortest day of the year
Project 3: Programming with Logical Structures
Testing the skaterLogic Alice 2.0
World – p. 173
• Play button
• Methods should be called in order
Project 3: Programming with Logical StructuresProject 3: Programming with Logical Structures 30
Project Summary
• List and describe the three major elements of
logical structure found in algorithms
• List several criteria that should be met by each
linear sequence in an algorithm
• Describe the difference between binary bypass
and binary choice branching routines
• Describe the difference between a pretest loop
and a posttest loop
• List and describe the four major parts of a count-
controlled loop
Project 3: Programming with Logical StructuresProject 3: Programming with Logical Structures 31
Project Summary
• List and describe the six logical comparison operations
used in computer programming
• List and describe the three major operations in Boolean
logic
• Create methods in Alice that implement each of the
following:
– a binary bypass
– a binary choice
– a count-controlled loop
– a sentinel loop that is not count controlled
• Create a compound Boolean expression in an Alice
looping or branching instruction