I made a flowchart that will print counting numbers 1-10 automatically, and I'm supposed to print their sum and average at the end. Now, I have to convert the flowchart into three Java programs: For, While, DoWhile.

I'm having a bit of a problem on the flowchart and the programs. Can someone please help? Thanks in advance.

Btw, I'm kinda new to Java, so if ya'll can just dumb it down a bit. Lol. Not too dumb though.

I made a flowchart that will print counting numbers 1-10 automatically, and I'm supposed to print their sum and average at the end. Now, I have to convert the flowchart into three Java programs: For, While, DoWhile.

I'm having a bit of a problem on the flowchart and the programs. Can someone please help? Thanks in advance.

Btw, I'm kinda new to Java, so if ya'll can just dumb it down a bit. Lol. Not too dumb though.

First, let's go over the three kind of loops - for, while and do while

  1. The for statement
    The for loop enables you to iterate over a range of values in an easy way. Syntax:
    for (initialization; termination; increment) {
        //do something
    }

    The initialization expression initializes the loop and it is only executed when the loop begins. The termination expression is such that when it evaluates to false, the loop terminates. After each loop iteration the increment statement is invoked, incrementing the variable as we see fit. We can of course decrement it as well. Examples:

    for (int i =0; i < 10; ++i) {
        //do something
    }

    This code will initialize an integer variable named i to get the initial value of 0. The loop will iterate as long as i < 10, meaning for i-0,1,2,3,4,5,6,7,8,9 - 10 iterations. Each iteration, the value of i will be incremented by 1.

    for (int i =0; i < 10; i=i+2) {
        //do something
    }

    This time i will be incremented by 2 each iteration meaning that i's values will be 0,2,4,6,8 - 5 iterations overall.

    for (int i = 10; i > 0; --i) {
        //do something
    }

    This time i will be initialized to 10, decreased by 1 each iterations until i=0, meaning i will get the values i=10,9,8,7,6,5,4,3,2,1 - 10 iterations overall.

  2. The while statement
    Syntax:
    while (expression) {
         // do something
    }

    The while statement will continue to execute its code block until the expression is evaluated to false, and then the loop terminates. Unlike the for loop, this loop does not have a counting mechanism. Example for when you use the while loop is when you wait for an expression to become true, such when requesting the user to input a number between 0-9, and only when a valid number was entered to exit the loop.

  3. the do-while statement
    This loop is very similar to the while loop. Syntax:
    do {
         // do something
    } while (expression);

    The only difference between the while loop and the do-while loop is that since the expression in the do-while loop is evaluated at the bottom of the loop, the loop's code block will be executed at least once.

I will help you out with one of the loops, and hopefully you will be able to do the other two loops on your own. Let's do the for loop together step by step. We need to count 1-10, sum up all the numbers and print the sum and the average. In order to count from 1 to 10, let's start the for loop as follows:

for (int i = 1; i <= 10; ++i) {
    //do something
}

We are initializing the counting variable, i, to 1, counting until we have reached 10, and incrementing by one each time. We need to sum up the variables, so let's create a variable that will hold the sum:

int sum = 0;

Each time we iterate in the loop, we add the number to the total sum.

int sum = 0;
for (int i = 1; i <= 10; ++i) {
    sum += i;  //same as sum = sum + i;
}

When the loop ends, we will have our sum variable holding the sum of all the numbers that we iterated upon in the loop. To calculate the average, we need to do sum/(number of elements) which in our case means sum/10. Let's create another variable to hold the average, and print the sum and the average:

int sum = 0;
int average;
for (int i = 1; i <= 10; ++i) {
    sum += i;  //same as sum = sum + i;
}
average = sum / 10;
System.out.println("The sum is " + sum + ", and the average is " + average);

This was the for loop. Now try to think what you need to do with the while and do-while loops. Good luck!

Comments
good explanation

Thanks soo much for your help! I get it now! :) By the way, how to do I print the numbers 1-10? I ran the program and the output was the sum and average. I was wondering if there is a way to first print the numbers 1-10, then have the sum and average afterwards. Thanks again! :)

OH! And another thing, when I ran the For program, the average came out wrong. The average is supposed to be 5.5, since the sum is 55, so 55/10 = 5.5

public class Loop_For {
public static void main (String[] args) {
int sum = 0;
int average;
for (int i = 1; i <= 10; ++i) {
System.out.println(i);
sum += i;
}
average = sum / 10;
System.out.println("The sum is " + sum + ", and the average is " + average);
}
}

You have declared your average as an int - so it can only show int values. I see that I have done the same in my explanation. The average can hold real values and not only integers, so let's change it to double.

double average

Now, we indeed want to put inside average sum/10, but beware - in Java, if you divide to integers, the result will be integer, therefore the code

average = sum / 10;

will result in an integer (5 again in our case). The solution is simple - you need to cast one of the variable in the division to a double, and then the result of dividing an integer and a double will be a double. So let's try this:

average = ((double)sum) / 10; //casting sum to a double before dividing

This should give you the result you need. Your sum works fine as it is :)

@Eric Cute - the ++i and i++ will do the same in our case, and the ++i is more efficient since you don't need to hold a temp copy and return it after the incrementation.

I've been working on the While Loop, but I can't seem to get the average and the sum right. Here's the code:

public class Loop_While {
public static void main (String[] args) {
int sum = 0;
double average;
int ctr=1;
while (ctr<=10) {
System.out.println(ctr);
ctr++;
sum += ctr;
}
average = ((double)sum) / 10;
System.out.println("The sum is " + sum + ", and the average is " + average);
}
}


I keep getting sum=65 and average 6.5.

You need to swap sum+=ctr; line with ctr++;. Because you increase the counter before you add it, you are adding 2+3+...+11 instead of 1+2+...+10.

Edited 6 Years Ago by Taywin: n/a

Thanks guys for all your help! Now I get it. I really appreciate ya'll for spending some time to help me out. :)

This question has already been answered. Start a new discussion instead.