CHAPTER 05
Beginner
Operators, Conditions, and Loops
Updated: May 16, 2026
5 min read
# CHAPTER 5
Operators, Conditions, and Loops
1. Introduction
A program that just stores data is useless. An app must react and make decisions. If a user types the correct password, let them in; otherwise, show an error. If a user buys 5 items, calculate the total price. To implement this logic, we use Control Flow. In this chapter, we will master Operators, Conditions, and Loops. We will learn how to perform math, compare values, execute branching logic usingif and switch statements, and automate repetitive tasks using loops.
2. Learning Objectives
By the end of this chapter, you will be able to:- Perform mathematical calculations using Arithmetic Operators.
-
Compare data using Comparison and Logical Operators (
==,!=,&&,||).
-
Write conditional branching logic using
if,else if, andelse.
-
Write clean multi-condition logic using the
switchstatement.
-
Execute repetitive code using
for-inandwhileloops.
3. Arithmetic and Assignment Operators
Swift provides standard math operators:-
Addition:
+
-
Subtraction:
-
-
Multiplication:
*
-
Division:
/
-
Remainder (Modulo):
%(Returns the remainder of a division)
Compound Assignment: A shortcut for updating a variable!
swift
4. Comparison and Logical Operators
To make a decision, you must compare data. Comparisons always result in a Boolean (true or false).
-
Equal to:
==
-
Not equal to:
!=
-
Greater / Less than:
>,<,>=,<=
Logical Operators (Combining conditions):
-
AND (
&&): BOTH sides must be true.
-
OR (
||): AT LEAST ONE side must be true.
-
NOT (
!): Flips the boolean (True becomes False).
swift
5. Conditional Statements (if / else)
The if statement executes code *only* if the condition is true.
swift
6. The switch Statement (Cleaner Conditions)
If you have many else if statements checking the exact same variable, use a switch! It is significantly cleaner and faster.
*Swift Rule: Switch statements MUST be exhaustive. They must cover every possible scenario, which is why a default case is almost always required!*
swift
7. The for-in Loop
Loops allow you to execute a block of code multiple times.
The for-in loop is perfect for iterating over a sequence of numbers (a Range).
-
1...5(Closed Range): 1, 2, 3, 4, 5
-
1..<5(Half-Open Range): 1, 2, 3, 4
swift
8. The while Loop
A while loop continues running code repeatedly *until* a condition becomes false. It is excellent for game loops or waiting for data.
*Warning: If the condition never becomes false, you create an "Infinite Loop" and your app will crash!*
swift
9. Mini Project: Grade Calculator
Let's combine Variables, Operators, and Conditions into a logical script.
swift
10. Common Mistakes
-
Using
=instead of==in if statements: A single=is an assignment operator (giving a box a value). A double==is a comparison operator (checking if two boxes are identical).if name = "Alice"will crash the compiler! It must beif name == "Alice".
-
Forgetting the
defaultcase in a switch: Swift will rigidly refuse to compile aswitchstatement if it thinks there is even a 0.01% chance a value might slip through unhandled.
11. Best Practices
-
Switch over If-Else: If you are checking the same variable against 3 or more specific values, always prefer a
switchstatement over anif / else ifchain. It compiles faster and is much easier to read.
12. Exercises
-
1.
Write an
ifstatement that checks if a variableisBatteryLowistrue. If it is, print "Please plug in device".
-
2.
Write a
forloop that prints the numbers 10 down to 1. (Hint: Look upreversed()ranges in Swift!).
13. MCQ Quiz with Answers
Question 1
In an if statement, which Logical Operator requires BOTH conditions to evaluate to true for the code block to execute?
Question 2
Why is the default case typically mandatory when writing a switch statement evaluating a String in Swift?
14. Interview Questions
-
Q: Explain the mechanical difference between the
1...5closed range operator and the1..<5half-open range operator. In what specific coding scenario is the half-open range critical?
-
Q: Contrast a
for-inloop with awhileloop. Provide a specific architectural scenario where awhileloop is the superior choice.
- Q: Describe the concept of an "Infinite Loop". How is it created, and what happens to an iOS application when it occurs on the main thread?
15. Summary
In Chapter 5, we injected intelligence into our code. We utilized Operators to perform complex mathematics and compare data states. We established branching architectures usingif / else statements, and organized complex multi-state logic efficiently utilizing the exhaustive switch statement. Finally, we conquered automation, executing repetitive tasks seamlessly using for-in ranges and condition-dependent while loops.