Conditional Statements & Operators
Master Swift Programming Logic and Conditional Flow Control
Core Programming Concepts
Conditional Logic
The foundation of programming decision-making. Test conditions and execute code based on true or false outcomes.
Operators
Symbols that check, change, or combine values including comparison, arithmetic, and logical operations.
Control Flow
Direct program execution through if statements, switch cases, and complex conditional structures.
Setup Your Development Environment
Launch Xcode
Open Xcode on your Mac. If you completed the previous Variables.playground exercise, it should still be available.
Open Playground File
Navigate to File > Open, then go to Desktop > Class Files > yourname-iOS App Dev 1 Class and open Swift Basics Ready for Conditionals.playground
Verify Prerequisites
Ensure you have completed exercise 1C on Variables before proceeding with conditional statements and operators.
Programming is based largely on conditional logic, where you test a condition, then execute something based on the outcome of that condition. This is exemplified in apps like Netflix that test login credentials using if statements.
Conditional Statement Types
If Statement
Tests a single condition and executes code only when true. Uses syntax: if condition { code }
If-Else Statement
Provides alternative action when condition is false. Ensures one of two code blocks always executes.
Else-If Statement
Tests multiple conditions sequentially. Allows testing several possibilities without nested statements.
Swift Comparison Operators Reference
| Feature | Operator | Meaning | Example Usage |
|---|---|---|---|
| Equal | == | Tests equality | numberOfVerses == 4 |
| Not Equal | != | Tests inequality | numberOfVerses != 5 |
| Greater Than | > | Tests greater value | numberOfVerses > 3 |
| Less Than | < | Tests lesser value | numberOfVerses < 10 |
| Greater Equal | >= | Greater or equal | songRating >= 4 |
| Less Equal | <= | Less or equal | songRating <= 5 |
With boolean values, it's redundant to write 'if variable == true'. Simply use 'if variable' for true conditions and 'if !variable' for false conditions.
Band Performance Calculations Example
Arithmetic Operations in Swift
Addition (+)
Combines values like calculating total songs played including encore performances.
Subtraction (-)
Reduces values such as songs cut from a shortened set list performance.
Multiplication (*)
Scales values like calculating total revenue from ticket price times attendance.
Division (/)
Distributes values such as splitting band earnings equally among members.
Logical Operators Comparison
| Feature | Operator | Symbol | Usage |
|---|---|---|---|
| Not (Inverse) | ! | Reverses boolean value | |
| And (Conjunction) | && | Both conditions must be true | |
| Or (Disjunction) | || | At least one condition must be true |
The conjunction operator (&&) takes precedence over the disjunction operator (||). Use parentheses to group expressions like (A || B) && C to ensure proper evaluation order.
Switch vs If-Else Statements
For more than three possible values, using an else-if statement is strongly discouraged. Switch statements provide cleaner, more maintainable code for complex conditional logic with multiple discrete cases.
Switch Statement Best Practices
Required unless all possible cases are explicitly listed
Multiple values can be assigned to the same case using commas
Makes switch cases more readable and maintainable
For fixed sets of values, enums eliminate need for default clause
Key Takeaways