DEV Community

Gamya
Gamya

Posted on Edited on

Swift—Checking Multiple Conditions with else, else if, && and || 🔀

In our last article we learned how if lets Swift make a single decision. But real apps rarely deal with just one condition — you need to handle multiple outcomes, combine checks, and deal with complex logic.

That's what this article is all about. 🧠


🔁 The Problem with Multiple if Statements

Let's say you're checking a shinobi's rank based on their power level:

let powerLevel = 9001

if powerLevel > 9000 {
    print("It's over 9000! 🔥")
}

if powerLevel <= 9000 {
    print("Not quite there yet...")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

This works — but it's wasteful. Swift has to check powerLevel twice, even though the two conditions can never both be true at the same time. For a simple integer that's fine, but imagine if the check involved something complex like fetching data or running a calculation — you'd be doing that work twice for no reason.

There's a better way. ✅


🔀 Using else

else means "if the condition above was false, run this instead":

let powerLevel = 9001

if powerLevel > 9000 {
    print("It's over 9000! 🔥")
} else {
    print("Not quite there yet...")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Now Swift checks powerLevel once. If it's over 9000, the first block runs. Otherwise, the else block runs. Cleaner, faster, easier to read. 🌸


🪜 Using else if — Handling More Outcomes

What if you need more than two outcomes? That's where else if comes in — it lets you chain additional checks:

let powerLevel = 9000

if powerLevel > 9000 {
    print("It's over 9000! 🔥")
} else if powerLevel == 9000 {
    print("It's exactly 9000!")
} else {
    print("Still training...")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Output:

It's exactly 9000!
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Swift works through the conditions top to bottom and stops as soon as one is true. The rules are:

  • You must have exactly one if to start
  • You can have as many else if blocks as you need
  • You can have zero or one else at the end — it catches everything that didn't match above

⚠️ Without else if, you'd end up with nested if statements inside else blocks — which works but gets messy fast. else if keeps everything flat and readable.


🔗 Combining Conditions with && (AND)

Sometimes one condition isn't enough — you want both things to be true at the same time.

Use && (read as "and") to combine two conditions. The whole check only passes if both sides are true:

let temperature = 25

if temperature > 20 && temperature < 30 {
    print("Perfect weather for training outside! ☀️")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Output:

Perfect weather for training outside! ☀️
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

If either side is false, the whole condition is false. Both must be true for the block to run.


🔗 Combining Conditions with || (OR)

Use || (read as "or") when you want the condition to pass if at least one side is true:

let userAge = 14
let hasParentalConsent = true

if userAge >= 18 || hasParentalConsent {
    print("You can access this content.")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Output:

You can access this content.
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

The user is only 14 — so the first part fails. But they have parental consent — so the second part passes. With ||, one passing side is enough. ✅


⚠️ Mixing && and || — Be Careful!

Here's where things can get tricky. Look at this condition:

if isOwner && isEditingEnabled || isAdmin {
    print("You can delete this post.")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Does that mean:

  • (isOwner AND editingEnabled) OR isAdmin — admins can always delete
  • isOwner AND (editingEnabled OR isAdmin) — only owners can delete

These are two completely different things! Swift will always pick the first interpretation (it reads && before ||), but relying on that is a bad habit.

Always use parentheses to make your intent clear:

// Clear version — admins can always delete, owners can delete if editing is on
if (isOwner && isEditingEnabled) || isAdmin {
    print("You can delete this post.")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Any time you mix && and || in the same condition, add parentheses. Your future self will thank you. 🙏


🎮 Enums + Conditions — A Perfect Combo

Enums and conditions work beautifully together. Here's a battle system that shows off if, else if, else, and || all at once:

enum BattleStyle {
    case ninjutsu, taijutsu, genjutsu, swordplay, summon
}

let sasukesStyle = BattleStyle.ninjutsu

if sasukesStyle == .ninjutsu || sasukesStyle == .genjutsu {
    print("Sasuke is using chakra-based techniques! ⚡")
} else if sasukesStyle == .taijutsu {
    print("Sasuke is fighting hand to hand! 👊")
} else if sasukesStyle == .swordplay {
    print("Sasuke drew the Kusanagi blade! ⚔️")
} else {
    print("Sasuke called upon a summoning! 🐍")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Output:

Sasuke is using chakra-based techniques! ⚡
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

A few things worth noticing:

  • We need to write BattleStyle.ninjutsu the first time to tell Swift which enum we mean
  • After that, Swift knows sasukesStyle is a BattleStyle, so we can just write .genjutsu, .taijutsu, etc.
  • || lets us check for two enum cases in a single condition — if either matches, the block runs
  • Swift checks top to bottom and stops at the first match — only one block ever runs

🧩 Putting It All Together

Here's a more complete example — a shinobi ranking system:

let chakra = 85
let missionsCompleted = 47
let isAnbu = false

if chakra >= 90 && missionsCompleted >= 50 {
    print("Rank: Jonin 🌟")
} else if (chakra >= 70 && missionsCompleted >= 20) || isAnbu {
    print("Rank: Chunin ⚔️")
} else if chakra >= 50 {
    print("Rank: Genin 🍃")
} else {
    print("Rank: Academy Student 📚")
}
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

Output:

Rank: Chunin ⚔️
🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด 🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด

The first condition fails — chakra is 85 but missions is only 47. The second condition passes — chakra is over 70 and missions is over 20. Swift stops there and runs that block.


🌟 Wrap Up

Here's everything we covered:

Tool What it does
else Runs if the if condition was false
else if Checks a new condition if the previous one was false
&& Both conditions must be true
OR operator (two pipe symbols) At least one condition must be true
() parentheses Makes the order of checks explicit and clear

A few golden rules to remember:

  • else catches everything that didn't match — you can only have one
  • else if lets you chain as many checks as you need
  • Always add parentheses when mixing && and || — don't leave it to Swift to guess
  • Swift stops at the first true condition — only one block ever runs

Conditions are the foundation of all app logic — master these and you'll be able to build almost anything. 💪

Next up, we'll look at switch statements — a cleaner way to handle many conditions at once. See you there! 👋

Top comments (2)

🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด
 
junhao profile image
Ahmed bahar •

My Dear Girl❤️❤️❤️
Thank you for your message👍👍👍
I really enjoyed reading this article today. Your explanations are clear, friendly, and full of useful insights. The progression from basic conditions to more advanced logic felt natural and easy to follow. It's obvious that you care about helping others learn Swift effectively. Keep up the amazing work! 🌸🌸🌸

🔴 Hot News: gamya m/swift checking multiple conditions with else else if and lbc - สรุปดราม่าล่าสุด
 
gamya_m profile image
Gamya •

This honestly made my day, thank you! 🌸 I put a lot of thought into how each concept flows into the next, so knowing it lands that way is the best feedback I could get. More articles coming soon — hope you enjoy them just as much! 😊✨