0
0
Kotlinprogramming~15 mins

For loop with step and downTo in Kotlin - Mini Project: Build & Apply

Choose your learning style9 modes available
For loop with step and downTo in Kotlin
📖 Scenario: You are organizing a countdown event and want to display numbers counting down with a specific step.
🎯 Goal: Build a Kotlin program that counts down from 10 to 1, stepping by 2, and prints each number.
📋 What You'll Learn
Create a variable called start with the value 10
Create a variable called end with the value 1
Use a for loop with downTo and step to count down from start to end by 2
Print each number inside the loop
💡 Why This Matters
🌍 Real World
Counting down with steps is useful in timers, animations, and game loops where you want to skip certain numbers.
💼 Career
Understanding loops with steps and counting down is important for writing efficient code in many programming jobs, including app development and automation.
Progress0 / 4 steps
1
Create start and end variables
Create a variable called start and set it to 10. Create another variable called end and set it to 1.
Kotlin
Need a hint?

Use val to create variables and assign the numbers 10 and 1.

2
Write a for loop with downTo and step
Write a for loop using i as the loop variable that counts down from start to end using downTo and steps by 2 using step.
Kotlin
Need a hint?

Use for (i in start downTo end step 2) to count down by 2.

3
Print each number inside the loop
Inside the for loop, write println(i) to print each number.
Kotlin
Need a hint?

Use println(i) inside the loop to show each number.

4
Run the program to see the countdown
Run the program to print the countdown numbers from 10 to 1 stepping by 2.
Kotlin
Need a hint?

The program should print numbers 10, 8, 6, 4, and 2 each on a new line.