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 Comparison and Logical Operators

John Scully
John Scully
1,018 Points

What is the point of using string interpolation in the buzzfizz example?

Hi,

I got this to work with the following:

for numbers in 1...50{
if numbers % 3 == 0 && numbers % 5 == 0 {
    println("buzzfizz")
}   else if numbers % 3 == 0 {
    println("buzz")
}   else if numbers % 5 == 0 {
        println("fizz")
}   else {
println (numbers)
}
}

My question is what the value is what the value of using string interpolation might be here?

1 Answer

You aren't using string interpolation. You are just printing a string. An example of string interpolation is when you declare a variable, say a = "world". And then you print "Hello /(a)". This will output "Hello World".

John Scully
John Scully
1,018 Points

Thanks jonroby,

Sorry meant to say I got it to work without interpolation. I guess I'm just struggling to connect the dots as to why you might use interpolation for this example.

John Scully
John Scully
1,018 Points

Actually I thought about it a bit harder and realised how it can be useful to concatenate the buzz and fizz variables.

var b = "buzz"
var f = "fizz"

for numbers in 1...15{
if numbers % 3 == 0 && numbers % 5 == 0 {
    println("\(b)\(f)")
}   else if numbers % 3 == 0 {
    println("\(b)")
}   else if numbers % 5 == 0 {
        println("\(f)")
}   else {
println (numbers)
}
}

Thank you - your answer led me to realising why I might use it here.