1 / 17

Counting Loops

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.

nevaeh
Download Presentation

Counting Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Counting Loops

  2. 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

  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.

  4. 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);

  5. Repeat More Often • Change count to 10 times. • Annoying??? • See the repetition? • Loop the same commands over and over again in a pattern

  6. 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

  7. 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);

  8. Set intCount to 1 intCount <= 10? Display "Hello" Add 1 to intCount Yes No Flowchart of For…Next Loop

  9. 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 ___

  10. 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);

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

  12. 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)

  13. 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);

  14. 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

  15. 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.

  16. 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);

  17. 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!

More Related