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

becky hayes
becky hayes
2,226 Points

I dont understand why the && statement checking if number divisible by 3&5 needs to go at the top

My initial code read like this... (i.e the (i % 5 == 0) && (i % 3 == 0) was the last statement instead of being the first statement. The code only worked when i switched it...Why does this matter?

for i in 1...20 {

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

3 Answers

Michael Reining
Michael Reining
10,101 Points

Hi Becky,

The computer program evaluates the code from top to bottom.

If you place the code at the top, then the condition can be met.

if (i % 5 == 0) && (i % 3 == 0) {
        print ("FizzBuzz") }

If you place the code after the other if clauses, the code will never run. Why? The else if clause only gets evaluated IF the initial if clause evaluates to false.

Also, if you enter / paste the code into the Playground, you will instantly see the answer and difference.

for i in 1...20 {

    if (i % 3 == 0){
        print("Fizz")
    }
    else if (i % 5 == 0){
        print ("Buzz")
    }
    else if (i % 5 == 0) && (i % 3 == 0) { // never runs
        print ("FizzBuzz") }
    else {
        print(i)
    }
}

for i in 1...20 {
    if (i % 5 == 0) && (i % 3 == 0) { // now it runs
        print ("FizzBuzz") }

    else if (i % 3 == 0){
        print("Fizz")
    }
    else if (i % 5 == 0){
        print ("Buzz")
    }

    else {
        print(i)
    }
}

I hope that helps,

Mike

PS: Thanks to the awesome resources on Team Treehouse, I launched my first app.

Now you can practice writing Swift code directly on your iPhone :-)

Code! Learn how to program with Swift

Jhoan Arango
Jhoan Arango
14,575 Points

Hello :

Well, it matters because execution starts from top to bottom. So if the first condition is true, then it will execute the print method and exit, then the loop will start again the next round, and if the first condition is false this time around, it will allow the other else if statements to consider their condition, and this will happen several times until the loop no longer has numbers in the range.

So with that said here is the output of both codes..

This is with the && condition on top ( correct one )

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
17
Fizz
19
Buzz

And this is the one with the && condition at the bottom ( Incorrect one )

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
Fizz // This should be FizzBuzz
16
17
Fizz
19
Buzz

// Since the "if i % 3 == 0" evaluated to true, it did not reach to the bottom where
// both 3 and 5 were being considered.

Hope this makes sense.

Good luck

becky hayes
becky hayes
2,226 Points

Thanks so much! does make sense now ;)