Um momento
0x10Lesson 2 of 7

Model values with clear types

Use `let`, `var`, and Swift’s common value types.

15 min 5-question quiz 1 code exercise
By the end of this lesson you can
  • Declare and use constants and variables with inferred types.

Use let for constants and var for values that change. Swift infers types from initial values and includes common types like Int, Double, String, and Bool. Favor let by default to reduce accidental mutation and make code easier to reason about.

main.swift
1let language = "Swift"
2var year = 2014
3year += 10
4print("\(language) is \(year) years old")
Output
Swift is 2024 years old

String interpolation uses \(expression) inside a string literal. Swift type inference keeps code concise while still preserving compile-time safety.

Key takeaways

  • Declare and use constants and variables with inferred types.

  • 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

Compute doubled age

+25 XP

Read one integer and print twice its value.

  • Input 4
  • Input -3
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: