Consider a grading system where numbers are turned into letters. Fill in the blanks in the following code to complete the Boolean expression.
float grade = random(0,100);

if (_________________) {
  println("Assign letter grade A.");
} else if (_________________) {
  println(_____________________);
} else if (_________________) {
  println(_____________________);
} else if (_________________) {
  println(_____________________);
} else { 
  println(_____________________);
}
Answer:
float grade = random(0,100);

if (grade > 90) {
  println("Assign letter grade A.");
} else if (grade > 80) {
  println("Assign letter grade B.");
} else if (grade > 70) {
  println("Assign letter grade C.");
} else if (grade > 60) {
  println("Assign letter grade D.");
} else { 
  println("Assign letter grade F.");
}

5 Comments

»

  1. Shouldn’t we write it as
    if (grade>=90){
    println(“Assign Letter grade A.”);
    }else if (grade>=80){
    println(“Assign letter grade B.”);

    and so forth.
    or does the code already set that limit so in which the second conditional “If grade is greater than 80 assign grade B” will only go from 80-89. I feel like since 91 is greater than 80 it will still assign a B letter grade.

    Comment by Zaira — October 13, 2009 @ 6:19 pm

  2. Yes, technically speaking this would be more precise, in terms of grading!

    Comment by admin — October 14, 2009 @ 7:35 pm

  3. Is there another way of defining ranges of numbers in the conditionals block?

    else if(grade = 70-79){
    printIn(“You just earned a C, genius”);
    }

    I return an error in this way. I know that I should use <=79. Is it that Processing is reading "70-79" as 70 minus 79 versus 70 through 79? I tried a comma, like in the random() function, but failed. Just wondering.

    Comment by Gabe — October 18, 2009 @ 3:33 pm

  4. You have to use a logical “AND” represented by ‘&&’, i.e.

    if (grade >= 70 && grade < 80) {
      println("Grade is greater than or equal to 70 and less than 80.");
    }
    

    Comment by admin — October 18, 2009 @ 6:09 pm

  5. that is why you’re the teacher…*bows

    Comment by Gabe — October 18, 2009 @ 7:54 pm

Leave a comment