Presentation is loading. Please wait.

Presentation is loading. Please wait.

FLUENCY WITH INFORMATION TECNOLOGY

Similar presentations


Presentation on theme: "FLUENCY WITH INFORMATION TECNOLOGY"— Presentation transcript:

1 FLUENCY WITH INFORMATION TECNOLOGY
Skills, Concepts, and Capabilities

2 PART 4 Problem Solving

3 chapter21 ONCE IS NOT ENOUGH Iteration Principles

4 ITERATION: PLAY IT AGAIN, SAM
What is a loop? A loop is a programming structure that provides a means of repeating the same code multiple times. The programmer must control the loop. Copyright © 2003 Pearson Education, Inc.

5 Classic Loop Problem Instructions on a bottle of shampoo. Lather Rinse
Repeat But when does it end? Repeat once, twice, each time we hit repeat? Copyright © 2003 Pearson Education, Inc.

6 Loop components There are four parts to every loop initialization
decision body update Copyright © 2003 Pearson Education, Inc.

7 Loop control In most loops, there will be one variable that will control the loop. That variable is called the loop control variable. The loop control variable must be initialized and updated. The loop continues until the loop control variable reaches a predefined value. Copyright © 2003 Pearson Education, Inc.

8 Initialization We set our loop control variable to some beginning value that makes sense for the task. Counting tasks, set the variable to 0 or 1. Copyright © 2003 Pearson Education, Inc.

9 Update Each time through the loop, the loop control variable may be changed. This change is usually called an increment, but there are many ways to control loops that do not involve an increment. This update must “help” the loop control variable to reach the control value. Copyright © 2003 Pearson Education, Inc.

10 Decision A decision is like the condition of an if statement.
If the condition is true, the loop continues to run. If the condition is false, the loop stops and the statement just beneath the loop executes. Copyright © 2003 Pearson Education, Inc.

11 Body The action or set of actions that you want to repeat.
The body of a loop may include update or it may simply carry out an action. Copyright © 2003 Pearson Education, Inc.

12 The World Famous Iteration
for loop for loops are found in most programming languages a for loop is a counting loop also called a definite loop a for loop contains all four parts of a loop Copyright © 2003 Pearson Education, Inc.

13 The for Statement – aka counting loop
A for statement has the following syntax: The initialization is executed once before the loop begins The statement is executed until the condition becomes false for ( initialization ; condition ; increment ) { statement(s); } The increment portion is executed at the end of each iteration Copyright © 2003 Pearson Education, Inc.

14 Let’s look at Counter.html
Simple counting loop. Copyright © 2003 Pearson Education, Inc.

15 Let’s look at two loops Counter2.html
Copyright © 2003 Pearson Education, Inc.

16 Loop problems Infinite loop. The loop control variable fails to reach the limit value. Caused by failing to update the loop control variable or failing to have the loop control variable reach the limit. May also occur if you have a value that “skips” over the limit. Demo Copyright © 2003 Pearson Education, Inc.

17 Off by one error Have your program increment one too many or one too few times. Demo Copyright © 2003 Pearson Education, Inc.


Download ppt "FLUENCY WITH INFORMATION TECNOLOGY"

Similar presentations


Ads by Google