counting loops

17
Counting Loops

Upload: nevaeh

Post on 23-Feb-2016

28 views

Category:

Documents


0 download

DESCRIPTION

Counting Loops. A loop is a repeating structure that contains a block of program statements. Needs a way to indicate which statements repeat Needs a way to start. Introduction to Loops: The Counting Loop. Chap 2 section 2.3. Repeat Something. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Counting Loops

Counting Loops

Page 2: Counting Loops

Introduction to Loops: The Counting Loop

• Chap 2 section 2.3

• A loop is a repeating structure that contains a block of program statements.

• Needs a way to indicate which statements repeat

• Needs a way to start

Page 3: Counting Loops

Repeat Something

• The a parameter sets a number on and the program needs to count up to that number by displaying each number on the console until it reaches that number– Assume the parm is going to hold 5. The program should

show: 12345

– Code that now.

Page 4: Counting Loops

Repetition Code

int intNumber = 1;System.out.println(intNumber);intNumber = intNumber + 1;System.out.println(intNumber);intNumber = intNumber + 1;System.out.println(intNumber);intNumber = intNumber + 1;System.out.println(intNumber);intNumber = intNumber + 1;System.out.println(intNumber);

Page 5: Counting Loops

Repeat More Often

• Change count to 10 times. • Annoying???• See the repetition?• Loop the same commands over and over

again in a pattern

Page 6: Counting Loops

Loop Syntax

• Tell the computer to loop• To do it 5 times:– Setup: Create an integer to count with – Start where? Start at 1– Increment? Keep adding 1 your integer– Stop where? Stop at 5

• Watch your counter by printing it– During– After

Page 7: Counting Loops

Your Code with a Loop

int intNumber ; for (intNumber = 1;

intNumber <= 5;intNumber= intNumber+1) {

System.out.println(“Inside loop “+ intNumber);

} System.out.println(“After loop: “ + intNumber);

Page 8: Counting Loops

Flowchart of For…Next Loop

Chapter 5 – Slide 8

intCount <= 10? Display "Hello"

Yes

No

Set intCount to 1

Add 1 to intCount

Page 9: Counting Loops

You try it - Counting

• Count from 5 to 50 by 5’s– Setup: Create an integer to count with – Start where? Start at ___– Increment? Keep adding ___ your integer– Stop where? Stop at ___

Page 10: Counting Loops

Your Code with a Loop

int intNumber ; for (intNumber = 5;

intNumber <= 50;intNumber= intNumber+5) {

System.out.println(“Inside loop “+ intNumber);

} System.out.println(“After loop: “ + intNumber);

starttest increase

Page 11: Counting Loops

Add Up

• Need a bucket to hold the total• Start at 0• Keep accumulating each pass• Make your numbers sum up

Page 12: Counting Loops

Logic for Keeping a Running Total

Chapter 5 – Slide 12

Read the next number

Is there another number to read?

Set accumulator to 0

Add the number to the accumulator

Yes(True)

No(False)

Setting the accumulator variable to zero before entering the loop is a critical step

Page 13: Counting Loops

Code that Totals

int intSum = 0; int intNumber ; for (intNumber = 1;

intNumber <= 5;intNumber= intNumber+1) {intSum = intSum + intNumber;System.out.println(“Inside loop “ + intNumber );

System.out.println(“running count: “ + intSum ); } System.out.println(“After loop total: “ + intSum);

Page 14: Counting Loops

Accumulating a total

• Variables don't remember what they were• You can create a variable to increase and run it each time the

loop runs.double total = 0;for (int count = 1; count <= 3; count++){ total = total + count;} // add what you are counting to what you haveSystem.out.println(total);

at end of first loop, count = 1 and total = 1 +0 = 1at end of second loop, count = 2 and total = 2 + 1 = 3at end of third loop, count = 3 and total = 3 + 3 = 6at end of entire loop, count is gone and total = 3

Page 15: Counting Loops

You try – totalling

• What is the total of the series from 5 to 50 by 5’s?

• Create a variable to hold the total • Set that variable to 0 – before you start

repeating• Add to the variable repeatedly (inside loop)• After the loop – you have your total.

Page 16: Counting Loops

Your Code Totalling with a Loop

int intNumber ; int intSum= 0; for (intNumber = 5;

intNumber <= 50;intNumber= intNumber+5) {

intSum = intSum + intNumber;System.out.println(“Inside loop “ + intNumber ); System.out.println(“running count: “ + intSum );

} System.out.println(“After loop total: “ + intSum);

Page 17: Counting Loops

Summary of Loops• Repetition• Starts with For () { • Ends with : matching }• Starting value: 1rst statement inside ()• Go Condition – 2nd statement inside ()• Increment – last statement inside ()• Total – – Create bucket starting at zero– Accumulate inside loop– Have total after loop

• Next: Loops inside loops!