Presentation is loading. Please wait.

Presentation is loading. Please wait.

Counted Loops.

Similar presentations


Presentation on theme: "Counted Loops."— Presentation transcript:

1 Counted Loops

2 What is a Loop? In programming, a loop is a sequence of instructions that is continually repeated until some kind of end condition is met. We call one full cycle of a loop an iteration “How many times does that loop iterate?” “What iteration did the error occur during?” Useful Applications of Loops: A password program that limits attempts before denying access Analyze a String, character by character for encryption (loop based on the String length) Repeating a process, such as drawing 10 of one thing on the screen Game Loops repeat the same two actions 60 times per second, Update & Draw Animation continually loops through all the images in a single animation over a period of time Working with arrays, we can scan through each element one at a time

3 Applied Usefulness Add the numbers from 1 to 1000 and output the sum
sum = sum + 1; Code Description: sum = sum + 2; FOR each number from 1 to 1000 sum = sum + 3; COMPUTE sum as sum plus number ... ENDFOR sum = sum ; console.log(“sum: “ + sum);

4 For loops A counted loop is a loop that iterates a specific number of times. In most languages, a counted loop is also called a for loop because it uses the keyword, for For loops are made up of 4 parts: (3 to define the for loop, 1 to be executed) Define a counter variable (used to count iterations) Compare counter against an end value to continue looping Change counter in a manner that gets it closer to the end value Code to execute each iteration of the loop

5 For loops - 3 Parts in the for statement
1 ) Define a counter variable int count = startValue This variable can be defined before the for loop if you want to keep the data after the loop completes. It can also be defined when defining the for loop itself. However, this strategy will delete the data when the loop completes. startValue can be any integer or even another variable The name of the counter variable (count in the example) can be simple to save time. The most common count variable names in for loops are i and j. As shown here, the counter variable should be an int type

6 For loops - 3 Parts in the for statement
2) Compare the counter against an endValue. count < 10 or count >= 0 endValue must also be an int type If your loop is counting up, endValue must be greater than startValue Otherwise if your loop is counting down, endValue must be less than startValue The Loop continues to repeat as long as the test evaluates to true This test occurs before ever entering the loop code, which means the code may never execute

7 For loops - 3 Parts in the for statement
3) Change the counter count = count + 1 or count = count - 1 The amount to increase/decrease the loop counter by at the end of iteration This is also referred to as incrementing the counter You can change the counter in any way you wish as long as it is progressing towards the endValue E.g. count = count + 5 or count = count / 2 or count = Math.Pow(count,2) Failure to progress towards endValue will result in an endless(infinite) loop Remember your shortcuts: count = count + 1  count += 1  count++ count = count - 1  count -= 1  count--

8 For loops – Example Counting Up
Create a for loop that counts from 1 to 1000, and keeps a running total of the count int total = 0; for (int count = 1; count <= 1000; count = count + 1) { total = total + count; } 1) Define 2) Compare 3) Change 4) Code

9 Example Program for (int i = 10; i > 0; i = i – 1) {
System.out.println(“Iteration number:” + i); } What will the output look like? Iteration number:10 Iteration number:9 Iteration number:8 Iteration number:7 Iteration number:6 Iteration number:5 Iteration number:4 Iteration number:3 Iteration number:2 Iteration number:1

10 Example Program 2 for (int i = 10; i > 0; i -= 2) {
System.out.println(“Iteration number:” + i); } What will the output look like? Iteration number:10 Iteration number:8 Iteration number:6 Iteration number:4 Iteration number:2

11 Example Program 3 for (int i = 3; i <= 12; i += 3) {
System.out.println(“Iteration number:” + i); } What will the output look like? Iteration number:3 Iteration number:6 Iteration number:9 Iteration number:12

12 Arrays and Counted Loops
Assume you have an array of 10 numbers. How would you write a subprogram that iterated through the array one element at a time and calculate the average number? private static void Main(String [] args) { double numbers = new double[]{9, 3.2, 4, 7, 6, 6, 3, 9, 10, 1}; double average = FindAverage(numbers); System.out.println(“Average number: “ + average); } private double FindAverage(double[] nums) int sum = 0; int avg = 0; for (int i = 0; i < nums.length; i++) sum = sum + nums[i]; avg = sum / nums.length; return avg; Pass the array as a parameter Q: What is nums.length? A: 10  for (int i = 0; i < 10; i++) Meaning, through the life of the for loop, i will have the values 0 through 9…all the possible index values in our Array!! This allows us to access each sequential element of the array by using the counter variable as our index…AMAZING!

13 Nesting As mentioned in the Selection lessons, any control structure inside of another is called nesting. if inside an if switch inside an if (or vise versa) loop inside a loop loop inside an if (or vise versa) You Can nest as many loops as you want, but it can get confusing with more than 3 loops inside of loops

14 Nesting Example What output would this produce?
Starting Row: 1 Starting Column: 1 for Row: 1 Starting Column: 2 for Row: 1 Starting Column: 3 for Row: 1 Ending Row 1 Starting Row: 2 Starting Column: 1 for Row: 2 Starting Column: 2 for Row: 2 Starting Column: 3 for Row: 2 Ending Row 2 Starting Row: 3 Starting Column: 1 for Row: 3 Starting Column: 2 for Row: 3 Starting Column: 3 for Row: 3 Ending Row 3 Nesting Example for (int row = 1; row < 4; row++) { System.out.println(“Starting Row: “ + row); for (int column = 1; column < 4; column++) System.out.println(“Starting Column: “ + column + “ for Row: “ + row); } System.out.println(“Ending Row: “ + row); What output would this produce? Think of it like a spreadsheet, you look at one row at a time and work your way from left to right looking at each column. When the row is done you go to the next row and repeat the process until there are no more rows remaining.

15 Nesting Example What output would this produce? Starting Row: 1
Starting Column: A for Row: 1 Starting Column: B for Row: 1 Starting Column: C for Row: 1 Ending Row 1 Starting Row: 2 Starting Column: A for Row: 2 Starting Column: B for Row: 2 Starting Column: C for Row: 2 Ending Row 2 Starting Row: 3 Starting Column: A for Row: 3 Starting Column: B for Row: 3 Starting Column: C for Row: 3 Ending Row 3 Nesting Example for (int row = 1; row < 4; row++) { System.out.println(“Starting Row: “ & row); for (int column = 1; column < 4; column++) if (column == 1) System.out.println(“Starting Column: “ + “A” + “ for Row: “ & row); } else if (column == 2) System.out.println(“Starting Column: “ + “B” + “ for Row: “ & row); else if (column == 3) System.out.println(“Starting Column: “ + “C” + “ for Row: “ + row); System.out.println(“Ending Row: “ + row); What output would this produce?

16 Nesting Example Conditionals Inside Loops
for (int number = -3; number < 33; number++) { //if the remainder of dividing number by 5 is 0, then it is evenly divisible if (number % 5 == 0 ) System.out.println(number + “ is evenly divisible by 5”); } 0 is evenly divisible by 5 5 is evenly divisible by 5 10 is evenly divisible by 5 15 is evenly divisible by 5 20 is evenly divisible by 5 25 is evenly divisible by 5 30 is evenly divisible by 5


Download ppt "Counted Loops."

Similar presentations


Ads by Google