Decide with conditions
Use `if`, comparisons, and booleans to branch.
- Write clear if/else logic from input values.
Swift conditions evaluate to Bool. Use if, else if, and else to choose behavior. Comparison operators include ==, !=, <, <=, >, and >=; combine conditions with && and ||.
1let score = 82
2if score >= 90 {
3 print("A")
4} else if score >= 80 {
5 print("B")
6} else {
7 print("Keep practicing")
8}B
Swift also includes guard for early exits, which can simplify control flow in functions by handling invalid cases first.
Key takeaways
Write clear if/else logic from input values.
Swift favors clear intent and safe defaults.
Compiler feedback is part of the learning loop.
Lesson quiz
5 questions · pass with 4 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Practice: write Swift
These run for real. Write the program, press Run tests, and read what the compiler tells you - learning to read compiler messages is half of learning C.
Label a temperature
Read one integer and print cold if below 15, otherwise print warm.
- Below threshold
- At threshold
Swift lessons are authored for Swift 6.4; exercises run on Swift 6.0.1 via Wandbox, a free public service. Your source code and test input are sent to that service.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…