Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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

Working with Logical Operators

Could someone explain this challenge?

logicalOperators.swift
var results: [Int] = []
var even = 0
var odd = 1
for n in 1...100 {
    // Enter your code below
    if 100 % 1 = 0 {
    print("Even")
    } else { 
        print("Odd")
    // End code 
}

1 Answer

Hi Fahiye,

I think you'll actually want to use

n % 2 = 0

The reason is that the modulo operator is being used here to determine if any given number is even, which means it's divisible by 2. (hence, n % 2 = 0). Since all numbers are divisible by 1, 100 % 1 (or n % 1) will always result in true statement - your loop will never reach the else clause.

Hope that helps! Cena