Skip to main content
Swift for iOS Development
CHAPTER 03 Beginner

Swift Programming Basics

Updated: May 16, 2026
6 min read

# CHAPTER 3

Swift Programming Basics

1. Introduction

Before you can build complex user interfaces or fetch data from the internet, you must learn the vocabulary and grammar of the Swift language. Swift was designed by Apple to be incredibly clean, safe, and modern. Unlike older languages, it eliminates unnecessary semicolons and complex syntax, making it read almost like plain English. In this chapter, we will master Swift Programming Basics. We will explore how to store data in memory, write clean code using comments, and output information to the console using print statements.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Understand the core syntax rules of Swift.
  • Create single-line and multi-line comments.
  • Utilize the print() function to debug code.
  • Understand the concept of Variables and Constants at a high level.
  • Work with basic data forms: Strings, Numbers, and Booleans.

3. Swift Syntax and Semicolons

If you have used JavaScript, Java, or C++, you are used to putting a semicolon ; at the end of every line. In Swift, semicolons are not required. You simply press "Enter" to start a new line.
swift
12345
// This is JavaScript:
// let name = "Alice";

// This is Swift:
let name = "Alice" 

4. Comments in Swift

Comments are notes you leave in your code for yourself or other humans. The compiler completely ignores them.
swift
1234567
// This is a single-line comment. I can write anything here!

/*
   This is a multi-line comment.
   It is great for writing long explanations
   or temporarily disabling chunks of code.
*/

5. The Print Statement

When writing code, you frequently need to check what the computer is thinking. You do this by asking the computer to print data to the "Console" (the debug area at the bottom of Xcode).
swift
12345678
// Printing a simple string
print("Hello, world!")

// Printing a number
print(42)

// Printing the result of math
print(10 + 5) // Will print 15

6. Variables and Constants (A High-Level Look)

A program needs to remember things (like a user's score, or their name). It remembers things using "boxes" in the computer's memory.
  • If the box's contents *can change* later, we use var (Variable).
  • If the box's contents *must never change*, we use let (Constant).

*(We will cover this deeply in the next chapter, but here is a quick overview!)*

swift
123456789
// 1. Variable (var): The score changes as the game progresses!
var score = 0
score = 10
score = 25
print(score) // Prints 25

// 2. Constant (let): A user's birth year does not change.
let birthYear = 1990
// birthYear = 1995 -> ERROR! The compiler will block this.

7. Basic Data Forms

Swift organizes data into different categories so it knows how to handle them.

Strings (Text): Always wrapped in double quotes "".

swift
12
var playerName = "Arthur Morgan"
let greeting = "Welcome to the app!"

Numbers (Integers & Decimals): Written as raw numbers. No quotes!

swift
12
var playerAge = 35         // Whole number (Integer)
let exactPi = 3.14159      // Decimal number

Booleans (True/False): The simplest form of logic. Used for on/off switches.

swift
12
var isGameOver = false
let isProMember = true

8. Full Swift Snippet: A Mini Text Game Logic

Let's put all the basics together into a single script.
swift
12345678910111213141516171819202122
// 1. Setting up the game constants (Things that won't change)
let gameTitle = "Dragon Quest"
let maxHealth = 100

// 2. Setting up the variables (Things that will change)
var currentHealth = 100
var hasSword = false
var playerName = "Hero"

// 3. Simulating game events
print("Welcome to " + gameTitle)
print("Player spawned: " + playerName)

// The player finds a sword!
hasSword = true
print("Did the player find a sword?")
print(hasSword)

// The player takes damage!
currentHealth = currentHealth - 20
print("Player was attacked! Current health is:")
print(currentHealth)

9. Common Mistakes

  • Putting Quotes Around Variables: If you type print("currentHealth"), the computer will literally print the word "currentHealth". If you want the *value* stored inside the box (e.g., 80), you must drop the quotes: print(currentHealth).
  • Misusing Case Sensitivity: Swift is strictly case-sensitive. var Score = 10 and var score = 10 are two completely different variables.

10. Best Practices

  • camelCase Naming: Always name your variables starting with a lowercase letter, and capitalize every subsequent word. (e.g., userEmailAddress, highestPlayerScore).
  • Prefer let over var: Whenever possible, use let. It makes your code faster and prevents accidental bugs where you change data that shouldn't be changed. Only use var if you explicitly know the value will update.

11. Exercises

  1. 1. Write a single-line comment stating your name and today's date.
  1. 2. Use the print() statement to output the result of 100 * 5.

12. Coding Challenges

Challenge: Create a constant named appVersion and assign it the decimal number 1.5. Create a variable named isLoggedIn and assign it the boolean false. Finally, print both of them to the console.

13. MCQ Quiz with Answers

Question 1

In Swift, how do you declare a piece of data in memory that is strictly forbidden from ever changing its value?

Question 2

Which of the following correctly defines a Boolean value in Swift?

14. Interview Questions

  • Q: Explain the mechanical difference between a var and a let in Swift. Why does Apple heavily encourage the default use of let in iOS architecture?
  • Q: Describe how Swift handles statement termination compared to legacy C-based languages. Are semicolons ever required in Swift?
  • Q: Contrast a String literal with a variable reference inside a print() statement.

15. FAQs

Q: Where do I actually type this code in Xcode to test it? A: For pure Swift logic testing without building a full app, Xcode has a brilliant feature called a Playground. Go to File -> New -> Playground, select "Blank", and you can type Swift code and see the results instantly on the right side of the screen!

16. Summary

In Chapter 3, we explored the foundational grammar of the Swift programming language. We discovered that Swift utilizes a clean, semicolon-free syntax. We practiced annotating our code with single and multi-line comments, and learned to output data via the print() statement. Finally, we took a high-level look at memory allocation, distinguishing between mutable Variables (var) and immutable Constants (let), while identifying core data forms like Strings, Numbers, and Booleans.

17. Next Chapter Recommendation

We know what variables and strings are, but Swift is a "Type-Safe" language, meaning it is incredibly strict about how data is categorized. Proceed to Chapter 4: Variables, Constants, and Data Types in Swift to master memory architecture.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·