Cycles and conditions 2

Example 2: Condition inside a loop

Task: write the function getEvenNumbersUpTo, which takes a number and returns all even numbers from 1 to this number inclusively as a string.

Let’s start writing a function:

static String getEvenNumbersUpTo(int num) {

}

We need a cycle to collect numbers from 1 to num, but not all numbers, but only even ones.

It may seem that this requires a special cycle, because we do not need ALL the numbers, but only some. And here different approaches are possible, but we will consider the most obvious and simple, not requiring any new skills and knowledge.

First, let’s try to solve a simpler problem: let the function actually return all numbers from 1 to num.

We do everything as usual - the counter, cycle, update the counter:

static String getEvenNumbersUpTo(int num) {
  var counter = 1;
  var result = "";

  while (counter != num) {
    result += counter;
    counter += 1;
  }

  return result;
}

The counter counter performs two tasks at once: it tracks the number of repetitions and is the number that needs to be displayed.

Run:

System.out.print(getEvenNumbersUpTo(5));
1234

It works, but not to the end: the last number was not displayed. Apparently, the cycle is working, but it stops one step earlier than we need. Completion of the loop depends on the predicate in while, let’s take a closer look at it:

while (counter != num)

“While the counter is not equal to num. It turns out, when the counter is equal to num, the cycle is no longer executed. But we need it to be executed one more time, when the counter is num, and then it has already stopped.

The predicate should be changed to “while the counter is less than or equal to num:

static String getEvenNumbersUpTo(int num) {
  var counter = 1;
  var result = "";

  while (counter <= num) {
    result += counter;
    counter += 1;
  }

  return result;
}

Run:

System.out.print(getEvenNumbersUpTo(5));
12345

Hooray!

Now back to the initial problem: you need to return only even numbers. In other words, if the number is even, then add it to the result; otherwise, do nothing.

So you need a condition with if directly in the body of the loop. In the place where we added the number to the result string, we need to add a condition:

static String getEvenNumbersUpTo(int num) {
  var counter = 1;
  var result = "";

  while (counter <= num) {
    if (counter % 2 == 0) {
      result += counter;
    }
    counter += 1;
  }

  return result;
}

Запустим:

System.out.print(getEvenNumbersUpTo(5));
24
System.out.print(getEvenNumbersUpTo(9));
2468

Works!

Despite the fact that we cycle through all numbers from 1 to num, due to the condition, adding to the string is the result only for even numbers. The counter is increasing in any case at every step.

instructions

As you can see, the getEvenNumbersUpTo function returns numbers by blinding them together. Modify the function so that the numbers in the result are separated by commas:

2,4,6,8,

If you got stuck and don't know what to do, you can ask a question in our huge and friendly community
Exercise available only for signed users.

Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on GitHub.