HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to SwiftVariablesData TypesOperatorsStringsArraysConditions (if / else)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output
📦 Variables & Types
🔢 Numbers & Math
✍️ String
📚 Array & Dictionary
🔀 Condition & Loop
🧩 Function
Swift›Conditions (if / else)

Conditions (if / else)

Decision making with if-else if-else; conditions without parentheses, blocks with curly braces.

Simple enough for a 5-year-old to understand

decision with if

Conditions in Swift do not require parentheses, but the { } block is mandatory.

Example code
1
let age = 20
2
if age >= 18 {
Condition without parentheses.
ConditionCondition without parentheses.
3
print("Adult")
Runs if true.
Output / InputRuns if true.
4
} else {
5
print("Child")
6
}
ConditionOutput / Input
💡 Hint: No parentheses in the condition; curly braces { } are required.

About this topic

Conditions are structures that change the flow of a program: if a condition is true, do this; otherwise, do that. In Swift, if, else if, else, and switch handle this job. The result of a conditional expression must strictly be a Bool.

Swift does not allow you to use numbers or other values in place of conditions. In many languages, zero is considered "false" and anything non-zero is "true"; Swift rejects this and requires you to write an explicit comparison. This also makes accidental assignments impossible.

The switch statement in Swift is more powerful than you might expect: it can match against ranges, tuples, and conditional patterns. It also must cover every single case; if you leave any out, the compiler will warn you.

What you will learn

  • the if, else if, and else structure
  • Why conditions must be a Bool
  • Combining conditions with logical operators
  • Using switch and the exhaustiveness rule
  • Writing conditions with Optional binding

Who is this for?

  • Beginners learning flow control
  • Those coming from C-like languages
  • Those starting to work with Optionals

When to use?

An if chain is enough for decisions with two or three outcomes; if there are more than four outcomes, a switch is usually much more readable.

If you are branching based on whether a value exists or not, if let or guard let is much more correct than a plain if check.

Common mistakes

Wrong

let count = 5
if count {
  print("full")
}

Swift requires the condition to be a Bool; it does not treat numbers as truth values and gives a "type 'Int' cannot be used as a boolean" error. You are expected to clearly write your intention: is it greater than zero, is it not equal to zero? This strictness prevents the reader from having to guess.

Correct

let count = 5
if count > 0 {
  print("full")
}

Wrong

let score = 90
if score > 80
  print("successful")

In Swift, curly braces are mandatory even if the if body is just a single line; this code will not compile. In many C-like languages, braces are optional, which leads to bugs when a line is added later without noticing. Swift eliminates that bug entirely by enforcing the rule from the start.

Correct

let score = 90
if score > 80 {
  print("successful")
}

Wrong

let name: String? = "Ayşe"
if name != nil {
  print("Merhaba " + name)
}

Doing a nil check does not convince the compiler; name is still of type Optional and cannot be used without being unwrapped. The correct approach is Optional binding, which does the check and the unwrap in a single step, making the value directly usable inside the block.

Correct

let name: String? = "Ayşe"
if let name = name {
  print("Merhaba " + name)
}

Frequently asked questions

Do I need to write break in a switch?

No. In Swift, every case ends automatically, it does not fall through to the next one like in C. If you intentionally want it to fall through, you must write fallthrough. This default behavior eliminates the classic bug caused by a forgotten break.

Why does switch require a default?

Because Swift requires a switch to cover all possible values. If you have covered all states in a finite type like an enum, a default is not needed; but for an infinite type like Int, a default is unavoidable. This rule ensures the compiler warns you when a new case is added.

When is guard preferred?

For preconditions at the beginning of a function. guard forces you to exit early if the condition is not met and frees the main logic from indentation. If you have several validations in a row, a guard chain is much more readable than nested if blocks.

Summary

The conditional expression must be a Bool.

Curly braces are mandatory even for a single line.

A nil check does not unwrap an Optional; if let should be used.

switch cases do not fall through automatically.

A switch must cover all possibilities.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code