A chain versus separate ifs when a branch changes x

Two students write Java code that awards points and adjusts an integer x. Version 1 uses an else-if chain:


Java
int x = 75;
int points = 0;
if (x > 50) {
    x = x - 40;
    points = 3;
} else if (x > 20) {
    x = x - 10;
    points = 2;
} else if (x > 5) {
    points = 1;
} else {
    points = 0;
}


Version 2 is identical except that both else if keywords are replaced by separate if statements, so the final else belongs only to the if (x > 5) test:


Java
int x = 75;
int points = 0;
if (x > 50) {
    x = x - 40;
    points = 3;
}
if (x > 20) {
    x = x - 10;
    points = 2;
}
if (x > 5) {
    points = 1;
} else {
    points = 0;
}


Each version runs on its own, starting from x = 75 and points = 0. Give the final values of x and points after each version, as exact integers.

Your answers

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

More Chained Decisions Questions: