Solution: Types of Loops

Let's discuss the solution of the exercise.

We'll cover the following

Problem

In this exercise, a target number is given for counting up or down to. Write a piece of code that prints this counting in the following manner:

  • If the number is 5 and the value of optionSelected is Up, the output should be from 05 as follows:
Counting Up: 0
Counting Up: 1
Counting Up: 2
Counting Up: 3
Counting Up: 4
Counting Up: 5
  • If the number is 3 and the value of optionSelected is Down, the output should be from 30 as follows:
Counting Down: 3
Counting Down: 2
Counting Down: 1
Counting Down: 0

Get hands-on with 1400+ tech skills courses.