AP Computer Science Java: Lesson 3.3 Repetitive Statements - Counts and Sums Karel Unit 1 Unit 2 Unit 3 Unit 4 Unit 5 Unit 6 Unit 7 Progs Tests Back to Lesson 3.2 - Incrementation On to Lesson 3.4 - Random Integers Lesson 3.3 - Counts and Sums Purpose: To learn how to declare and use counter and summation variables What's the count?   There are many instances in programs involving loops where we need to count how many times something happens or find the sum of a collection of numbers. "How," you ask? Let's look at a counter variable first. Let's take an example. Suppose we wish to know how many integer factors a number has. Recall that a factor of a number is an integer that divides evenly into that number. For example, the number 10 has 4 factors: 1, 2, 5, and 10. Let's look at a loop that can accomplish this task. Begin by declaring a counter variable, which we will call factors. A counter variable must be declared as an int since a count must be a whole number. Also, we initialize the counter as 0 since that is where we begin, with a count of nothing.    int factors=0, number=10, loop; Here is the loop:    for(loop=1; loop<=number; loop++)    {       if(number%loop==0) factors++;    } Let's examine this code. First note that the value of loop begins at 1 and ends at the number. Here this means we will check all numbers from 1 to 10 to see if they are factors of 10. When value of loop is 1, the condition number%loop==0 is true since the remainder of 12 divided by 1is 0. So the statement factors++ will enact. This statement means add one to factors. So factors is now 1. Now loop takes the value 2, which also divides into 10 evenly. So factors is now 2. The next value of loop is 3 which is NOT a factor of 10, so factors does not change. In the end, only four numbers are found to be factors of 10, so the variable factors is 4. Summer Time  There are other times where a sum of a group of numbers is needed. For example, suppose we want to find the sum of the numbers from 1 to 100. We need an integer summation variable for the sum because we are adding integers together. This variable must be initialized to 0 just as a counter variable is.    int sum=0, loop; Here is the loop:    for(loop=1; loop<=100; loop++)    {       sum+=loop; //recall this means add loop to sum    } Another example: Suppose we wish to find the total cost of a purchase of 5 items in which decimal values are involved. First, declare the variables:    int loop;    double price, total = 0.0; and now the loop...    for(loop=1; loop<=5; loop++)    {       System.out.print("Enter price: ");       price=keybd.readInt();            total+=price; //add price to total    }    In closing, Counters are always integers. Sums can be ints or doubles depending on what is being summed. Lastly, remember to initialize counts and sums to 0. If this is not done, you will get an error because you are trying to add to a variable that doesn't yet have a value. I guess that means that you can't add something to nothing! Back to Lesson 3.2 - Incrementation On to Lesson 3.4 - Random Integers AP Home Karel Unit 1 Unit 2 Unit 3 Unit 4 Unit 5 Unit 6 Unit 7 Progs Tests