computer science and software engineering© 2014 project lead the way, inc. algorithms: branching...

14
Computer Science and Software Engineering © 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)

Upload: augustus-harvey

Post on 13-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)

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)

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)

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)

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)

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)

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)

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)

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)

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)

Your turn!

Page 11: Computer Science and Software Engineering© 2014 Project Lead The Way, Inc. Algorithms: Branching and Iteration (in Scratch)

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)

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)

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)

How to get exactly-one-of-several1-of-5

10 20 30

if

else-if

else-if

else

40

else-if