0
0
Swiftprogramming~15 mins

While loop in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
Counting Down with a While Loop
📖 Scenario: Imagine you have a rocket that needs to count down before launch. You want to create a simple program that counts down from a number to zero.
🎯 Goal: Build a Swift program that uses a while loop to count down from 5 to 0 and print each number.
📋 What You'll Learn
Create a variable called count with the value 5
Create a variable called zero with the value 0
Use a while loop that runs as long as count is greater than or equal to zero
Inside the loop, print the current value of count
Inside the loop, decrease count by 1 each time
💡 Why This Matters
🌍 Real World
Counting down is used in many real-world situations like rocket launches, game timers, or cooking timers.
💼 Career
Understanding loops is essential for programming tasks that require repetition, such as processing data, animations, or user interactions.
Progress0 / 4 steps
1
Create the starting number
Create a variable called count and set it to 5.
Swift
Need a hint?

Use var to create a variable and assign the number 5 to count.

2
Create the zero value
Create a variable called zero and set it to 0.
Swift
Need a hint?

This variable will be used to compare in the loop condition.

3
Write the while loop
Write a while loop that runs as long as count is greater than or equal to zero. Inside the loop, print count and then subtract 1 from count.
Swift
Need a hint?

Use while count >= zero { ... } and inside the braces print and decrease count.

4
Run and see the countdown
Run the program to print the countdown from 5 to 0.
Swift
Need a hint?

The program should print numbers from 5 down to 0, each on its own line.