Measure the effect of an early return

A reading challenge should award one coupon per day, except that every fourth day earns three coupons instead. The method below is called as coupons(15).


Java
static int coupons(int days) {
    int total = 0;
    for (int day = 1; day <= days; day++) {
        if (day % 4 == 0) {
            total = total + 3;
        } else {
            total = total + 1;
        }
        return total;
    }
    return total;
}


A programmer corrects the method by removing only the return total; inside the loop, leaving the final return after the loop. By how many coupons does this correction increase the returned result? Answer in coupons, to zero decimal places.

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 Methods, Conditionals, and Loops in Java Questions: