Conditionals

Overview

Conditionals should cover all the possibilities, else the output will be an error.

if

let a = true
let b = true

if a && b {
    print("Hello, Swift!")
}
// compile: true

//  Both a and b are true

if not: ”if !”

Guard

guard => Guard this rule

else => if result not as we needed then do this

return => finish the process

throw => ???

Sources

if vs Guard

Do-catch, Do-try

Sources

Switch

the difference between if-else

If-else allows duplicate criteria whereas switch does not. This is the main difference between them

enum Weather {
    case sun, rain, wind, snow, unknown
}

let forecast = Weather.rain

if forecast == .sun {
    print("It should be a nice day.")
} else if forecast == .rain {
    print("Pack an umbrella.")
} else if forecast == .wind {
    print("Wear something warm")
} else if forecast == .rain { // dublicated criteria, if-else allows but switch does not
    print("School is cancelled.")
} else {
    print("Our forecast generator is broken!")
}


switch forecast {
case .sun:
    print("It should be a nice day.")
case .rain:
    print("Pack an umbrella.")
case .wind:
    print("Wear something warm")
case .snow:
    print("School is cancelled.")
case .unknown:
    print("Our forecast generator is broken!")
}

default:

In some cases, you will have dozens if not hundreds of criteria, in those cases normally you will need to describe all of them in switch formula. But swift does not require it, and with the help of "default:" case, it allows you to describe a result for all other cases at once.

let place = "Metropolis"

switch place {
case "Gotham":
    print("You're Batman!")
case "Mega-City One":
    print("You're Judge Dredd!")
case "Wakanda":
    print("You're Black Panther!")
default:
    print("Who are you?")
}

/*
print:
Who are you?
*/

The "default:" criteria must be at the end, otherwise swift will throw an error message, "Additional 'case' blocks cannot appear after the 'default' block of a 'switch'"

switch place {
default: // this is the default value for all other cases
    print("Who are you?") // compile: Additional 'case' blocks cannot appear after the 'default' block of a 'switch'
case "Gotham":
    print("You're Batman!")
case "Mega-City One":
    print("You're Judge Dredd!")
case "Wakanda":
    print("You're Black Panther!")
}

fallthrough

In some cases, you may need to merge multiple cases in order.

"fallthrough" Allows you to merge multiple (ordered) criteria. You can use it as many times as you want.

let day = 5
print("My true love gave to me…")

switch day {
case 5:
    print("5 golden rings")
    fallthrough
case 4:
    print("4 calling birds")
    fallthrough
case 3:
    print("3 French hens")
    fallthrough
case 2:
    print("2 turtle doves")
    fallthrough
default:
    print("A partridge in a pear tree")
}

/*
print:
5 golden rings
4 calling birds
3 French hens
2 turtle doves
A partridge in a pear tree
*/

// --

switch day {
case 5:
    print("5 golden rings")
    fallthrough
case 4:
    print("4 calling birds")
    fallthrough
case 3:
    print("3 French hens")
//    fallthrough
case 2:
    print("2 turtle doves")
//    fallthrough
default:
    print("A partridge in a pear tree")
}

/*
print:
5 golden rings
4 calling birds
*/
// it did not print the "default:"

Ternary Conditional Operator

Criteria ? :

  • "criteria": > || < || = || ! || !=

  • "?": True

  • ":" False

This squence also called as WTF

  • W: What to check

  • T: if True

  • F: if False

let age = 18
let canVote = age >= 18 ? "Yes" : "No"
print(canVote)

// print: "Yes"

// More simple version
print(age >= 18 ? "Yes" : "No")
// print: "Yes"

You cannot insert an "if-else" statement in a "print", you can only insert "print" in an "if-else".

The other solution which is more readable is to use a ternary in a "print". #control

Ternary conditions also shorten your code.

Sources

Videos

Articles / Documents

Last updated