Skip to content

Instantly share code, notes, and snippets.

@pravj
Last active July 30, 2019 00:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pravj/e4af092f494bd46cddc9 to your computer and use it in GitHub Desktop.
Save pravj/e4af092f494bd46cddc9 to your computer and use it in GitHub Desktop.
99 Bottles of Beer in Swift Language
var i = 99
while i > 0
println(i + " bottles of beer on the wall, " + i + "bottles of beer.")
var num = i - 1
if i == 1 {
var num = "no more"
}
println("Take one down and pass it around, " + num + "bottles of beer on the wall.")
println("No more bottles of beer on the wall, no more bottles of beer.")
println("Go to the store and buy some more, 99 bottles of beer on the wall.")
@symonty
Copy link

symonty commented Feb 21, 2015

Try this instead.

for i in reverse(1...99) {
println("(i) bottles of beer on the wall, (i) bottles of beer.")
var num = toString(i - 1)
if ( i == 1 ) { num = "no" }
println("Take one down and pass it around, (num) bottles of beer on the wall.")
}
println("No more bottles of beer on the wall, no more bottles of beer.")
println("Go to the store and buy some more, 99 bottles of beer on the wall.")

@IOOI-SqAR
Copy link

Swift 5:

for i in (1...99).reversed() {
    print("\(i) bottles of beer on the wall, \(i) bottles of beer.")
    var num = String(i - 1)
    if ( i == 1 ) { num = "no" }
    print("Take one down and pass it around, \(num) bottles of beer on the wall.")
}
print("No more bottles of beer on the wall, no more bottles of beer.")
print("Go to the store and buy some more, 99 bottles of beer on the wall.")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment