# Conditionals

## Overview

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

## if

```swift
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

* <https://www.codingem.com/swift-guard/>

## if vs Guard

## Do-catch, Do-try

### Sources

* <https://codewithchris.com/swift-try-catch/>

## Switch

### the difference between if-else

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

```swift
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.

```swift
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'**"

```swift
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.&#x20;

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

```swift
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: **W**hat to check
* T: if **T**rue
* F: if **F**alse

```swift
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".&#x20;

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

Ternary conditions also shorten your code.

## Sources

### Videos

{% embed url="<https://youtu.be/laVLtkWjybM>" %}

### Articles / Documents

* <https://www.hackingwithswift.com/100/swiftui/5>
* <https://www.hackingwithswift.com/quick-start/beginners/how-to-use-switch-statements-to-check-multiple-conditions>
* <https://www.hackingwithswift.com/quick-start/understanding-swift/when-should-you-use-switch-statements-rather-than-if>
* <https://www.hackingwithswift.com/quick-start/beginners/how-to-use-the-ternary-conditional-operator-for-quick-tests>
* <https://www.hackingwithswift.com/quick-start/understanding-swift/when-should-you-use-the-ternary-operator-in-swift>
