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

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!