Follow control through a method call

The method below adds the whole numbers from 1 to n with a while loop, and main calls it once and prints the result.


Java
public static int sumTo(int n) {
    int total = 0;
    int k = 1;
    while (k <= n) {
        total += k;
        k++;
    }
    return total;
}

// inside main
int result = sumTo(2 * 3);
System.out.println(result);


Put the events in the order in which they happen when main reaches the line int result = sumTo(2 * 3);.

Loading ordering steps…

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: