Cycles and conditions

Cycles and conditions are key tools in programming. Therefore it is worth considering a few more examples.

Example 1: Run cycle provided

The task is to write a shouter function that takes a string and, if there are more than 5 characters in the string, displays it 100 times. If the string has 5 characters or less, the function does nothing.

Let’s start writing a function:

static void shouter(String str) {

}

As you probably guessed, in order to display the line 100 times on the screen, a cycle is needed. But this is only necessary if the string is more than 5 letters.

So you need to start by creating the condition:

static void shouter(String str) {
  var length = str.length();

  if (length > 5) {
  }
}

Inside curly { } brackets after if there will be a code that will be executed only if length > 5. There will be a cycle for repeating 100 times.

Recall the strategy for creating such cycles:

  1. Create a counter and give it an initial value.
  2. Make a cycle that will be repeated until the counter has reached a finite value.
  3. In the body of the loop, perform the desired action (in our case, output to the screen) and update the counter.

We need 100 repetitions, so we will make a counter with a value of 0 and we will increase it by 1 each step, and so on until the counter becomes 100.

static void shouter(String str) {
  var length = str.length();

  if (length > 5) {
    var counter = 0;
    while (counter != 100) {
      System.out.print(str);
      counter += 1;
    }
  }
}

counter += 1 is an increase in the counter by 1.

Run:

shouter("HELLO!");
HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!HELLO!

Now let’s modify the function so that it returns the result, rather than displaying it on the screen. Let’s use the same technique that was in the function for flip the line: create an empty string result and fill it in a loop, and then return it from the function.

static String shouter(String str) {
  var length = str.length();
  var result = "";
  if (length > 5) {
    var counter = 0;
    while (counter != 100) {
      result += str;
      counter += 1;
    }
  }
}

instructions

Modify the latest version of the shouter function by adding the following conditions:

  1. If the string length is greater than 0 and less than 5, then return the original string without repetition.
  2. If the string length is exactly 5 characters, then return the string repeated 10 times.

The condition “if length is more than 5, then return the line repeated 100 times” should be preserved.


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.