0% found this document useful (0 votes)
59 views2 pages

Loops Puzzle

The document contains Swift code that prints the numbers from 1 to 100 with some exceptions. It uses different for loops and if/else statements to print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "Fizz Buzz" for numbers that are multiples of both 3 and 5. The final code sample provides a condensed way to perform this by using a switch statement on the modulo values of i % 3 and i % 5.

Uploaded by

casper russell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views2 pages

Loops Puzzle

The document contains Swift code that prints the numbers from 1 to 100 with some exceptions. It uses different for loops and if/else statements to print "Fizz" for multiples of 3, "Buzz" for multiples of 5, and "Fizz Buzz" for numbers that are multiples of both 3 and 5. The final code sample provides a condensed way to perform this by using a switch statement on the modulo values of i % 3 and i % 5.

Uploaded by

casper russell
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

import Cocoa

/*var myFirstInt: Int = 0


for i in 1...5 {
myFirstInt += 1
myFirstInt
print("myFirstInt equals \(myFirstInt) at iteration \(i)")
}

myFirstInt = 0

var i = 1
while i < 6 {
myFirstInt += 1
print(myFirstInt)
i += 1
}

for i in 1 ... 100 where i % 3 == 0 {


print(i)
}
*/

/*for i in 1 ... 100 {


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

/*for i in 1...100 {
if (i % 3 == 0) {
print("Fizz")
}
if (i % 5 == 0) {
print("Buzz")
}
else if (i % 3 != 0 && i % 5 != 0) {
print(i)
}
}

/ar myFirstInt: Int = 0

for _ in 1...5 {
myFirstInt += 1
myFirstInt
print(myFirstInt)
}*/

for i in 1 ... 100 {


let values = (one: i % 3, two: i % 5)
switch values {
case _ where (values.one == 0) && (values.two == 0):
print("Fizz Buzz")
case _ where (values.one == 0) && (values.two != 0):
print("Fizz")
case _ where (values.one != 0) && (values.two == 0):
print("Buzz")
default:
print(i)
}
}

You might also like