Labs ICT
Pro Login

Booleans and Conditionals

Making decisions with if, guard, and switch.

Booleans and Conditionals

Booleans are Swift's way of representing true or false values. They're essential for making decisions in your code.

The if/else if/else structure lets you execute different code based on conditions:

let temperature = 75

if temperature > 80 {
    print("It's hot outside!")
} else if temperature > 60 {
    print("It's nice weather.")
} else {
    print("It's chilly.")

Swift's switch statement is incredibly powerful with pattern matching. It can handle ranges, tuples, and complex patterns:

let grade = 85

switch grade {
case 90...100:
    print("Excellent")
case 80..<90:
    print("Good")
case 70..<80:
    print("Average")
default:
    print("Needs improvement")

Use guard for early exits when you need to check conditions before proceeding. The ternary operator condition ? valueIfTrue : valueIfFalse provides a concise way to choose between two values.

Logical operators combine conditions: && for AND, || for OR, and ! for NOT.

Try it Yourself ->