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
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 int intNumber = 1; System.out.println(intNumber); intNumber = intNumber + 1;
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 int intNumber ; for (intNumber = 1; intNumber <= 5; intNumber= intNumber+1) { System.out.println(“Inside loop “ + intNumber); } System.out.println(“After loop: “ + intNumber);
Flowchart of For…Next Loop intCount <= 10? Display "Hello" Yes No Set intCount to 1 Add 1 to intCount
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 int intNumber ; 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 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
Code that Totals int intSum = 0; int intNumber ; for (intNumber = 1; 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 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);
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!