tags:

views:

121

answers:

5

Had to write the following program for an on line pre java class using, while, do-while and for loops. Looking for a little explanation. Thanks in advance!
PS While looking for reference books is this Java or Javascript? Any suggestions for a good reference book? I get the concept, mostly, the devil is certainly in the details.

public class ExamsFor {

    public static void main(String[] arguments) {


    int inputNumber; // One of the exams input by the user.
    int sum;     // The sum of the exams.
    int count;   // Number of exams.
    Double Avg;    // The average of the exams.


    /* Initialize the summation and counting variables. */

    sum = 0;
    count = 0;

    /* Read and process the user's input. */

    TextIO.put("Please enter the first exam: "); // get the first exam.


        inputNumber = TextIO.getlnInt();

    for (inputNumber!=0; sum += inputNubmer; count++ ) {  // had the while loop below enter here, worked

    TextIO.put("Please enter the next exam, or 0 to end: "); // get the next exam.  
    inputNumber = TextIO.getlnInt();
    } 

    /* Display the result. */

    if (count == 0) {
    TextIO.putln("You didn't enter any data!");
    }
    else {
    Avg = ((double)sum) / count;
    TextIO.putln();
    TextIO.putln("You entered " + count + " exams.");
    TextIO.putf("The average for the exams entered is %1.2f.\n", Avg);
    } 

    } // end main ()
}  // end class ExamsFor

/*  Had the following 'while loop' in place of the 'for loop'


while (inputNumber != 0) {
    sum += inputNumber;  // Add inputNumber to running sum.
    count ++;        // Count the input by adding 1 to the count.

*/
A: 

Read this and compare your for loop to the expected syntax to see where you're going wrong.

Will A
+5  A: 

You can compare both for and while statement observing that you mainly need 4 things in an iterative construct:

  • the initial value of the value used in the condition (A)
  • a condition that is used to check if keeping up iterations (B)
  • a statement that modifies the value that is checked every time (C)
  • the body of the statement itself (BODY)

for for you have

for (A; B; C)
  BODY

while for while (full of word jokes here :) you have something like

A;
while (B)
{
  BODY;
  C;
}

That's quite simple, isn't it?

Jack
To be more precise, BODY should be in a `try` block and C should be in a `finally` block in case body has some `continue` statements.
Malcolm
A: 

There is a problem with your for loop which is basically incorrect. From The Java Tutorials:

The for Statement

[...] The general form of the for statement can be expressed as follows:

for (initialization; termination; increment) {
    statement(s)
}

When using this version of the for statement, keep in mind that:

  • The initialization expression initializes the loop; it's executed once, as the loop begins.
  • When the termination expression evaluates to false, the loop terminates.
  • The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

The following program, ForDemo, uses the general form of the for statement to print the numbers 1 through 10 to standard output:

class ForDemo {
     public static void main(String[] args) {
          for(int i=1; i<11; i++){
               System.out.println("Count is: " + i);
          }
     }
}

I suggest to start with the Tutorials but if you're interested, you'll find complete and more formal details in the Java Language Specification, section 14.14 The for Statement.

Pascal Thivent
A: 

You're just using the for loop wrong. The first part is for initializing indexes, the second is for testing to see if we need to continue or quit the loop, and the third is anything you want to happen after every pass through the loop, such as incrementing the index. In your case you want an empty for loop so you can break out of the loop when the input is '0'.

for(;;)
{
  TextIO.put("Please enter the next exam, or 0 to end: "); // get the next exam.   
  inputNumber = TextIO.getlnInt();
  if(inputNumber == '0') break;
  //put your processing here.

}



Initialize, test, do something.

kirk.burleson
+1  A: 

Shouldn't your for loop be: (note the preceding ; and a typo in sum+=inputNumber)

for (;inputNumber!=0; sum += inputNumber, count++ )

The while loop would be

while(inputNumber!=0) {
// rest of the things
 sum+=inputNumber;
 count++;
}
neal aise
That did it! I know there are many ways to accomplish any given tasks but I don't know that I would have ever figured that out. I'm a network guy just trying to survive this class. Thanks!!!
jjason89
glad it worked for ya!
neal aise