Coding Exercise: Types of Loops
Let's test your skills with loops.
We'll cover the following
Problem
In this exercise, a target number is given to count up or down to. Write a piece of code that prints this counting in the following manner:
- If the
number
is5
and the value ofoptionSelected
isUp
, the output should be from0
–5
as follows:
Counting Up: 0
Counting Up: 1
Counting Up: 2
Counting Up: 3
Counting Up: 4
Counting Up: 5
- If the
number
is3
and the value ofoptionSelected
isDown
, the output should be from3
–0
as follows:
Counting Down: 3
Counting Down: 2
Counting Down: 1
Counting Down: 0
Sample input
The program accepts accept one string
and one int
parameter, for example:
"Up", 2
Expected output
The finished output of the program should look like the example below:
Counting Up: 0
Counting Up: 1
Counting Up: 2
Get hands-on with 1400+ tech skills courses.