170 likes | 248 Views
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.
E N D
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 • 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: 1 2 3 4 5 • Code that now.
Repetition Code intintNumber = 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);
Repeat More Often • Change count to 10 times. • Annoying??? • See the repetition? • Loop the same commands over and over again in a pattern
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
Your Code with a Loop intintNumber ; for (intNumber = 1; intNumber<= 5; intNumber= intNumber+1) { System.out.println(“Inside loop “ + intNumber); } System.out.println(“After loop: “ + intNumber);
Set intCount to 1 intCount <= 10? Display "Hello" Add 1 to intCount Yes No Flowchart of For…Next Loop
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 ___
Your Code with a Loop start test increase intintNumber ; for (intNumber = 5; intNumber<= 50; intNumber= intNumber+5) { System.out.println(“Inside loop “ + intNumber); } System.out.println(“After loop: “ + intNumber);
Add Up • Need a bucket to hold the total • Start at 0 • Keep accumulating each pass • Make your numbers sum up
Logic for Keeping a Running Total Set accumulator to 0 Setting the accumulator variable to zero before entering the loop is a critical step Is there another number to read? Yes (True) Read the next number Add the number to the accumulator No (False)
Code that Totals int intSum = 0; intintNumber ; 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);
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 have System.out.println(total); at end of first loop, count = 1 and total = 1 +0 = 1 at end of second loop, count = 2 and total = 2 + 1 = 3 at end of third loop, count = 3 and total = 3 + 3 = 6 at end of entire loop, count is gone and total = 3
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.
Your Code Totalling with a Loop intintNumber; intintSum= 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);
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!