Um momento
0x30Lesson 4 of 7

Decide with conditions

Use `if`, comparisons, and booleans to branch.

15 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • 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 ||.

main.swift
1let score = 82
2if score >= 90 {
3    print("A")
4} else if score >= 80 {
5    print("B")
6} else {
7    print("Keep practicing")
8}
Output
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.

Exercise 1

Label a temperature

+25 XP

Read one integer and print cold if below 15, otherwise print warm.

  • Below threshold
  • At threshold
main.swift
Loading editor…

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…

Gostou da aula? 😆👍
Apoie nosso trabalho com uma doação: