Swift if-statement: несколько методов условного выполнения

Вот несколько методов, которые вы можете использовать с оператором if в Swift:

  1. Основной оператор if:

    if condition {
       // Code to be executed if condition is true
    }
  2. оператор if-else:

    if condition {
       // Code to be executed if condition is true
    } else {
       // Code to be executed if condition is false
    }
  3. if-else оператор if-else:

    if condition1 {
       // Code to be executed if condition1 is true
    } else if condition2 {
       // Code to be executed if condition2 is true
    } else {
       // Code to be executed if all conditions are false
    }
  4. Трнарный оператор:

    let result = condition ? valueIfTrue : valueIfFalse
  5. Несколько условий с логическими операторами:

    if condition1 && condition2 {
       // Code to be executed if both condition1 and condition2 are true
    }
    if condition1 || condition2 {
       // Code to be executed if either condition1 or condition2 is true
    }
  6. Вложенных операторов if:

    if condition1 {
       if condition2 {
           // Code to be executed if both condition1 and condition2 are true
       } else {
           // Code to be executed if condition1 is true and condition2 is false
       }
    } else {
       // Code to be executed if condition1 is false
    }
  7. Использование операторов if с дополнительными опциями:

    if let unwrappedValue = optionalValue {
       // Code to be executed if optionalValue is not nil
    } else {
       // Code to be executed if optionalValue is nil
    }