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 Collections and Control Flow Control Flow With Conditional Statements Working With Logical Operators

Not sure what I am doing wrong...

It's saying I should append the correct value but I believe I am ?

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

for n in 1...100 {
    // Enter your code below
if n % 2 != 0 && n % 7 == 0
results.append(n)
}

    // End code 

2 Answers

Emin Grbo
Emin Grbo
13,092 Points

It seems you are only lacking the curly braces for the IF statement :) Before, the append method was only executed 1 i think.

for n in 1...100 {
    // Enter your code below
if n % 2 != 0 && n % 7 == 0{
results.append(n)
}
}

Emin Grbo Why do I need a second set of curly braces inside?

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing well and your logic is solid, but your syntax is a bit off. In some languages, you may put what happens if an expression evaluates to true on the same line as an if statement and have it work without curly braces. This is not true of Swift. The opening and closing curly braces for an if statement are non-optional. Just as your for loop has opening and closing curly braces, your if statement also needs them:

for n in 1...100 {  // begin for loop
    if expression {  // begin if
        // do something here
    }  // end if
}  // end for loop

Hope this helps! :sparkles: