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 Basics (retired) Control Flow Exercise: FizzBuzz Generator

Parentheses

Hey I succeeded at making the FizzBuzz generator heres my code:

for number in 1...20 {

if number % 3 == 0 && number % 5 == 0 {

        println ("FizzBuzz")

    } else if number % 5 == 0 {

        println("Buzz")

    } else if number % 3 == 0 {

        println("Fizz")

} else {

    println(number)
}
}

I saw that Amit put parentheses around his ( i % 3 == 0) however it worked for me without. Is it a good habit to put parentheses around it?

And sorry im really bad at formatting

1 Answer

William Li
PLUS
William Li
Courses Plus Student 26,868 Points

Hi, Anthony Paulino I've edited the code format for you.

Is it a good habit to put parentheses around it?

The parentheses are optional in this case, so whether to use them is largely a matter of personal preference. But IMO, I'd say that wrap the boolean condition around with parentheses makes the code a bit more readable; also, if you're unsure about operator precedence, it's good idea to use parentheses to be on the safe side.

Thanks William!