Help: While loops


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Help: While loops

  1. #1
    genevieve Guest

    Help: While loops


    Below is code I have written to work out the grades of an array of marks.
    It worked fine, but i tried to change it to use a while loop and i don't
    understand why it doesn't work. Any help appreciated!

    G

    Code:
    import java.awt.Graphics.*;
    import java.awt.*;
    import java.awt.event.*;

    public class DetermineGradeWhileLoop {
    public static void main(String[]args) {

    final char[] reference_grade = {'A','B','C','D','F'};
    char [] grade = new char[5];
    int [] raw_score ={35,95,65,75};
    //as condition for while loop needs to be boolean, needs new variable
    boolean finish = false;

    while (finish==false) {
    if (raw_score[i] >= 90) {
    grade[i]= reference_grade[0] ;
    i++;
    if (i>3) {
    finish=true;
    }
    }
    else if (raw_score[i] >=80) {
    grade[i] = reference_grade[1] ;
    i++;
    if (i>3) {
    finish=true;
    }
    }
    else if (raw_score[i] >=70) {
    grade[i] = reference_grade[2] ;
    i++;
    if (i>3) {
    finish=true;
    }
    }
    else if (raw_score[i] >=60) {
    grade[i] = reference_grade[3];
    i++;
    if (i>3) {
    finish=true;
    }
    }
    else grade[i] = reference_grade[4];
    i++;
    if (i>3) {
    finish=true;
    }
    }
    System.out.println("Grade = " + grade[i] + ", from score
    of "+ raw_score[i]);
    }
    }

  2. #2
    Colin McGuigan Guest

    Re: While loops

    genevieve wrote:
    > Below is code I have written to work out the grades of an array of
    > marks. It worked fine, but i tried to change it to use a while loop
    > and i don't understand why it doesn't work. Any help appreciated!

    <snip>
    > else grade[i] = reference_grade[4];
    > i++;
    > if (i>3) {
    > finish=true;
    > }
    > }
    > System.out.println("Grade = " + grade[i] + ", from
    > score of "+ raw_score[i]);
    > }
    > }


    Your problem is with that final else -- since you don't put brackets
    starting at "grade[i] =" and ending right before "System.out", only the
    line:

    grade[i] = reference_grade[4];

    is considered part of the else. The rest of the statements:

    i++;
    if (i > 3) {
    finish = true;
    }

    Are always going to be executed every run through the while() loop
    (since they're not part of the if/else structure), and the
    System.out.println is only going to be executed once the while() loop is
    finished (since it's actually outside of it).

    If you indent the section appropriately, you'll see what I mean:


    } else grade[i] = reference_grade[4];
    // Note that this is outside of the if/else
    i++;
    if (i>3) {
    finish=true;
    } // End of 'if (i>3)'
    } // End while
    // Note this is outside the while loop
    System.out.println("Grade = " + grade[i] + ", from score of "+
    raw_score[i]);

    So i could potentially get incremented twice during the while() loop,
    and you won't see your System.out.println on each iteration; just at the
    end (which will generate an array out of bounds exception because i will
    be equal to 4, which is too high for raw_score).

    Also, you don't seem to declare the variable 'i', but you may have just
    not put that in the post.

    Some pointers: This would be better done with a for() loop:

    for (i = 0; i <= 3; i++) {
    }

    But if you're intent on using a while(), I would use:

    while (i <= 3)

    and not bother with the finish variable, and put i++ outside of the
    if/else structure; there's no reason to repeat it four times for each
    'if' condition if you can just have it once outside.

    --
    Colin McGuigan



  3. #3
    Kyle Guest

    Re: While loops


    Hi genevieve.
    Here is an alternate way to do what you are doing still using a while loop.

    import java.awt.Graphics.*;
    import java.awt.*;
    import java.awt.event.*;

    public class DetermineGradeWhileLoop {
    public static void main(String[]args) {

    final char[] reference_grade = {'A','B','C','D','F'};
    char [] grade;
    int [] raw_score ={35,95,65,75};
    int i = 0;

    // create the grade array
    grade = new char[raw_score.length];

    // Mark grades in this loop
    while (i < raw_score.length) {
    if (raw_score[i] >= 90) {
    grade[i]= reference_grade[0] ;
    } else if (raw_score[i] >=80) {
    grade[i] = reference_grade[1] ;
    } else if (raw_score[i] >=70) {
    grade[i] = reference_grade[2] ;
    } else if (raw_score[i] >=60) {
    grade[i] = reference_grade[3];
    } else {
    grade[i] = reference_grade[4];
    }
    i++;
    }

    // reset loop counter
    i = 0;

    // print out grades in this loop
    while (i < grade.length) {
    System.out.println("Grade = " + grade[i] + ", from score of " + raw_score[i]);
    i++;
    }
    }
    }
    with the way I have done the while test you never need to hard code the value
    at which the while loop should finish, thereby removing the need for that
    boolean variable.

    Cheers!
    Kyle


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center