Chapter 4: Swift Statements

1. What is a “statement” in Swift?

A statement is any complete instruction that Swift can execute. It usually ends with a semicolon ; — but in Swift you almost never need to write ; at the end (only when you put multiple statements on one line).

Examples of statements:

Swift

2. Most Common Category: Simple Expression Statements

These are the statements you write most often — they evaluate an expression and (usually) do something with the result.

Swift

Important note: Even a function call that returns something is a valid statement — you can ignore the return value.

Swift

3. Declaration Statements (let, var, func, struct, enum, class…)

These introduce new names into your code.

Swift

4. Control Flow Statements – if, guard, switch

These decide which code runs.

4.1 if – else if – else

Swift

One-line if (sometimes used):

Swift

4.2 guard – very popular in Swift

guard is used for early exit — it makes code much easier to read.

Swift

Key difference:

  • if let = keep going deeper (nested)
  • guard let = exit early → flatter, cleaner code

4.3 switch – very powerful in Swift

Swift

Advanced switch examples you see often:

Swift

5. Loop Statements

5.1 for-in loop (most common)

Swift

With index:

Swift

5.2 for with range

Swift

5.3 while & repeat-while

Swift

6. Transfer Statements – control flow jumps

These change the normal flow.

Statement What it does Where you use it most
break Exit the nearest loop / switch Loops, switch
continue Skip to next iteration of loop Loops
fallthrough Continue to next case in switch Rare, intentional
return Exit function / closure Functions
throw Throw an error Throwing functions

Examples:

Swift

7. do – catch (Error Handling Statements)

Swift

Modern short version:

Swift

Quick Reference Table – Most Important Statements

Kind Statement Example Most Common Use Case
Expression print(“Hi”) Doing work, calling functions
Declaration let name = “Amit” Creating variables, functions, types
if if age >= 18 { … } Simple conditions
guard guard let name else { return } Early exit / input validation
switch switch status { case 200..<300: … } Multiple conditions, enums, ranges
for-in for item in array { … } Iterating collections
while / repeat-while while condition { … } Unknown number of iterations
return return result Exit function with value
break / continue if condition { break } Early exit from loop
do-catch do { try … } catch { … } Handling errors

Small Practice – Combine several statements

Try writing this in a playground:

Swift

Next topic you want to explore deeply?

  • More about switch (pattern matching, associated values)
  • Closures (very important statement style)
  • defer statement (cleanup code)
  • throwing functions & do-try-catch in more detail
  • async/await statements (modern concurrency)

Just tell me what you want next — we’ll keep going in the same detailed way 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *