Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,865 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!