if-else Examples

Following are flow charts for each set of code. The flow charts show all possible flows, with the particular flow when score = 85 highlighted in red.

Incorrect code and flow chart:


char grade;
int score = 85;

if (score >= 90)
  grade = 'A';

if (score >= 80)
  grade = 'B';

if (score >= 70)
  grade = 'C';

if (score >= 60)
  grade = 'D';

else
  grade = 'F';

System.out.println("Score: " + score + " Grade: " + grade)
Solution to Problem 2

Correct code and flow chart:


char grade;
int score = 85;
    
if (score >= 90)
  grade = 'A';

else if (score >= 80)
  grade = 'B';

else if (score >= 70)
  grade = 'C';
    
else if (score >= 60)
  grade = 'D';

else
  grade = 'F';
    
System.out.println("Score: " + score + " Grade: " + grade);
Solution to Problem 1