0
0
Swiftprogramming~15 mins

If and if-else statements in Swift - Mini Project: Build & Apply

Choose your learning style9 modes available
If and if-else statements
📖 Scenario: You are creating a simple app that checks the temperature outside and gives advice on what to wear.
🎯 Goal: Build a program that uses if and if-else statements to decide what message to show based on the temperature.
📋 What You'll Learn
Create a variable to store the temperature as an integer
Create a variable to store a temperature limit
Use an if statement to check if the temperature is below the limit
Use an if-else statement to print different messages based on the temperature
💡 Why This Matters
🌍 Real World
Apps often need to make decisions based on data, like weather apps telling you what to wear.
💼 Career
Understanding <code>if</code> and <code>if-else</code> statements is essential for controlling program flow in any software development job.
Progress0 / 4 steps
1
DATA SETUP: Create a temperature variable
Create a variable called temperature and set it to 15.
Swift
Need a hint?

Use var to create a variable and assign the number 15 to temperature.

2
CONFIGURATION: Create a temperature limit variable
Create a variable called limit and set it to 20.
Swift
Need a hint?

Use var to create a variable named limit and assign it the value 20.

3
CORE LOGIC: Use an if statement to check temperature
Write an if statement that checks if temperature is less than limit. Inside the if, write a comment // It's cold.
Swift
Need a hint?

Use if temperature < limit { } and put the comment inside the braces.

4
OUTPUT: Use if-else to print messages
Write an if-else statement that checks if temperature is less than limit. If true, print "Wear a jacket". Otherwise, print "No jacket needed".
Swift
Need a hint?

Use if temperature < limit { print("Wear a jacket") } else { print("No jacket needed") }.