Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to...

11
Programming Concept #2 Iteration

Transcript of Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to...

Page 1: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Programming Concept #2

Iteration

Page 2: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Iteration• Is just a fancy way of saying that you would

like something to repeat more than one time.• It is used in any modern programming

language to make life easier on the programmer.

• If any of you have ever used code.org you have already used iteration in your programs without even knowing it.

Page 3: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Specifications• Are the instructions that are handed off to a

computer programmer telling them what they need to do.

• They should be CLEARLY written with little to no room for interpretation.

• If the programmer has any questions about the specifications they should get with whoever wrote them BEFORE they start writing their code.

• The abbreviation for specifications is ‘specs’.

Page 4: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Your Specs• So lets assume that you are a programmer and you

receive the following specs.– Create a program with a city as a background and

create a sprite that patrols the city making sure it is safe.

– Your sprite will patrol the city by moving back and forth across the screen. When they reach one side of the screen they will turn around and head to the other.

– If they stop patrolling the city it means the city is unsafe and open to mayhem.

Page 5: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Picture of your Staring off Point

Page 6: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Pseudocode• Please write down in ENGLISH how you would

go about writing this code.

Page 7: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Example Code WITHOUT Iteration

Notice that the first four lines would need to be repeated OVER, and

OVER, AND OVER again.

This makes it almost impossible to write this code because the cat will

stop moving once it reaches the end of this code and the cat

shouldn’t stop until the user ends the program.

So how do you solve this problem?

Page 8: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Iteration

You can replace all of this code with 5 lines of code.

Go to the next page to see how it would look.

Page 9: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Iteration

- Notice that you are simply repeating the first four lines FOREVER.

- That is called a forever ‘loop’ and any block inside of it will simply repeat over and over until the program ends.

- That is a very simple form of iteration.

Page 10: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Iteration

- Things to note:- When you use iteration it

should make your code easier to read.

- When you use iteration you need fewer lines of code.

- This makes it easier to code the program.

- It also makes it easier to read the program.

Page 11: Programming Concept #2 Iteration. Is just a fancy way of saying that you would like something to repeat more than one time. It is used in any modern programming.

Iteration• There are other blocks in Scratch that can be

used for iteration.• Take a look at the block• And the block for other examples of

iteration.• Remember iteration is just a fancy way of

saying repeat. • It’s your job as the programmer to figure out

what steps you want repeated and how long they should repeat.