iOS Swift Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

J. Tigre
PRO
J. Tigre
Pro Student 2,845 Points

Logical Operators

I need an if statement that tests for odd number and is a multiple of 7.

operators.swift
var results: [Int] = []

for n in 1...100 {
    // Enter your code below
    if (n)  {
      statement
    } else if {
      statement
    } else {
      statement 
    }
    // End code 
}

1 Answer

Rogier Nitschelm
Rogier Nitschelm
iOS Development Techdegree Student 5,460 Points

In order to check if a number is odd you could use the following expression, using the modulo-operator:

n % 2 != 0 // it is odd

To check if its a multiple of 7 you can use the following one:

n % 7 == 0 // it is a multiple of 7

Combining them:

if n % 2 != 0 && n % 7 == 0 {
   // do something
}