Why Swift loops are safe by default
📖 Scenario: Imagine you have a list of daily temperatures and you want to check which days were warm. You will use a loop to go through the temperatures safely without errors.
🎯 Goal: Build a Swift program that uses a safe loop to check temperatures and print a message for warm days.
📋 What You'll Learn
Create an array called
temperatures with exact values: 15, 22, 18, 30, 25Create a variable called
warmThreshold and set it to 20Use a
for loop with variable temp to iterate over temperaturesInside the loop, use an
if statement to check if temp is greater than warmThresholdPrint
"Day with temperature X°C is warm." where X is the temperature💡 Why This Matters
🌍 Real World
Checking temperature data safely is important in weather apps to avoid crashes and errors.
💼 Career
Understanding safe loops helps you write reliable Swift code for apps that handle lists and collections.
Progress0 / 4 steps