![Page 1: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/1.jpg)
Computer Science and Software Engineering © 2014 Project Lead The Way, Inc.
Algorithms:Branching and Iteration
(in Scratch)
![Page 2: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/2.jpg)
http://scratch.mit.edu/projects/11516969/
Learn from Scratch!
Edit your picture in paint editor
![Page 3: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/3.jpg)
Algorithms are step-by-step procedures
![Page 4: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/4.jpg)
If-else structure: conditional
• Always evaluates condition at top of the if-else structure
• Boolean expression: true or false
![Page 5: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/5.jpg)
If-else structure• Executes one
chunk or the other
• If-else always executes one chunk: not zero, not two
• If (no else) might execute one or zero chunks
![Page 6: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/6.jpg)
If-else structure
• Continues with next block
![Page 7: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/7.jpg)
Stacked if-else-structures• These are separate structures,
one after another
-240
240
-240
2400 x
y
snapsing
snapsqueak
clapsqueak
clapsing
![Page 8: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/8.jpg)
Nested if-else-structures• One inside the
other
-240
240
-240
2400 x
y
snapHello
clapHello
squeakFella
singFella
![Page 9: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/9.jpg)
Iteration• Iteration: an algorithm that repeats,
one cycle after another
![Page 10: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/10.jpg)
Your turn!
![Page 11: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/11.jpg)
Don’t miss the “else” opportunity• These stacks run slightly differently.
Why?Usually wasteful, risky
Better
Pair of opposite conditions
![Page 12: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/12.jpg)
How to get exactly-one-of-several• If-else selects 1-of-2• If-elseif-else selects 1-of-3
10 20 30
![Page 13: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/13.jpg)
How to get exactly-one-of-several• How about 1-of-4?
1-of-3
10 20 30
1-of-4 ?if
else-if
else-if
else
if
else-if
else
![Page 14: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)](https://reader030.vdocuments.net/reader030/viewer/2022032722/56649f515503460f94c73c48/html5/thumbnails/14.jpg)
How to get exactly-one-of-several1-of-5
10 20 30
if
else-if
else-if
else
40
else-if