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

Krishna Das
Krishna Das
1,186 Points

Condition inside brackets

Hi, I wrote the code below for the FizzBuzz exercise, and it outputs the same as Amit's code does. I have one question: In the video Amit has put the condition(s) of the if else statements inside () brackets. I didn't do that, but it seems to work the same. Just so I can understand I would like to ask, what is the advantage of using the brackets or disadvantage not doing it?

let numbers = 1...99

for number in numbers {

    if number % 3 == 0 && number % 5 == 0 {
        println("FizzBuzz")
    } else if number % 3 == 0 {
        println("Fizz")
    } else if number % 5 == 0 {
        println("Buzz")
    } else {
        println(number)
    }
}

Thanks

3 Answers

Eduardo Rojas
Eduardo Rojas
4,084 Points

Basically is for you to understand the code, in swift there is no need of using () while in objective-c they are fundamental and without them your code won't work

I'm learning C# currently in one of my college classes as I am going through these courses (these courses are way better than my professor ha) and so I use the () but that's just because in the older languages, like C# you have to use them. Plus it helps you when you are reading the code but it's not necessary.

Krishna Das
Krishna Das
1,186 Points

Ah ok great! Good to know. Thanks!