/
Chapter 9.1: Lists Chapter 9.1: Lists

Chapter 9.1: Lists - PowerPoint Presentation

celsa-spraggs
celsa-spraggs . @celsa-spraggs
Follow
380 views
Uploaded On 2015-11-25

Chapter 9.1: Lists - PPT Presentation

Lists are often called by another name in other programming languages arrays Suppose there is a list of Customers people waiting in line You want to serve each customer one at a time so each one should walk into the store one at a time ID: 204944

list loop order frogs loop list frogs order forallinorder code frog left talk option times counted let

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Chapter 9.1: Lists" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Chapter 9.1: Lists

Lists are often called by another name in other programming languages – arrays.Slide2

Suppose there is a list of Customers (people waiting in line). You want to serve each customer one at a time, so each one should walk into the store one at a time.

Use a

DoTogether

tileUse a DoInOrder tileUse a ForAllTogether tileUse a ForAllInOrder tileSlide3

Which of the following types can be used in a list?

Rockette

Rockette’s

ThighsRockette’s HipsAll of the AboveSlide4

If I declare a list thusly, and use a

ForAllInOrder

tile, what order do the

rockettes kick?OrderArockette

rockette

2

rockette3

B

rockette

rockette3rockette2CYou can’t be sureDNone of the aboveSlide5

Old

SuperBowl

Commercial

has three frogs talkBut what they are advertising we isn’t “ahem” a school topic So let’s modify it.Slide6

Three Frogs

Three frogs, together they say

Frog 1 (on left) “Hear No Evil”

Frog 2 (in middle) “See No Evil”Frog 3 (on right) “Speak No Evil”That’s hard – let’s start withThree frogs talk in order left to rightAll of them say the same thing: HelloLike this:Slide7

What does this code do?Slide8

What does this code do?

The frogs talk in order left to right

The frogs talk in backwards order (right to left)

Each frog talks, but the order dependsSlide9

It ALWAYS goes in the order you make your list inSlide10

Let’s assume I

remake my list like

this (L to R):Slide11

What if I want the frogs to say Hello (left to right) 5 times?

Make a counted loop run five times INSIDE

(as the inner loop)

the ForAllInOrder loopMake a counted loop run five times with the ForAllInOrder loop INSIDE (as the inner loop)Slide12

Predict: What would this code do?

???Slide13

By the way… you can make a list out of different kinds of objects

Do the wave with different “People” objects

Make different types of flowers grow

And out of other things…Slide14

Problem: Each frog should say something different…

Hear no evil

See no evil

Speak no evilSlide15

What programming construct would you use?

An if statement inside the For all in order

A counted loop inside the For all in order

An if statement before the For all in orderA counted loop outside the For all in orderI don’ t know…Slide16

There are (at least) two possible solutions… What is the BEST explanation of why is one better than the other?

Option 1 is better because it is shorter

Option 1 is better because it does the least number of “checks” (or

boolean condition evaluations)Option 2 is better because it makes clear exactly what the “checks” (or boolean condition evaluations) areOption 2 is better because it has a regular structure with empty “else” portionsSlide17

Shorter

Least

Boolean Checks

C) Clear Boolean

Checks

D) Regular

Structure with

Empty “

else”esSlide18

Remember this…

“Good” programs aren’t (always) the ones that

Make it easiest on the programmer

Are easiest for humans to readRequire least amount of effort from programmerInstead they are (usually) the ones thatRequire least computing work (by computer)Are easiest to adapt to new situationsMethods and parameters come in hereSlide19

There’s an EVEN BETTER way to write this program!

T

hink about this…

What if we had…A list of froggiesSome number of frogs that want to talk A list of wordsExactly the same “number” of Strings to be “said” by each of those frogs…How can we make a program that will make each frog say a specific wordIn ONLY 2 LINES OF CODE?!?!?Slide20

Let’s make the beetles do a more complicated dance…

Like this:Slide21

Which structure would you want?

One

ForAllInOrder

loopOne ForAllInOrder loop with an if statement insideOne if statement with a ForAllTogether insideNone of the above Slide22

Does this code correctly implement our intended behavior?

Yes

No

I don’t know

And WHY!Slide23

What is new about this code?

Nested “for all” loops

A loop that “iterates” over all items in a list

Inside that is another loop that goes over all the items in a listIn this case, happen to be the same listWow, you can do complicated things now… Slide24
Slide25

There are 4 beetles, how many times is this instruction executed?

4

12

16

48

I don’t knowSlide26

Parameters to methods:

We can do better now!Slide27

How would we do our “new” solo (with backup)