Model values with clear types
Use `let`, `var`, and Swift’s common value types.
- 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.
1let language = "Swift"
2var year = 2014
3year += 10
4print("\(language) is \(year) years old")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.
Compute doubled age
Read one integer and print twice its value.
- Input 4
- Input -3
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…