Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language VBScript

(correct lyrics version)

Date:07/19/05
Author:exec
URL:n/a
Comments:8
Info:n/a
Score: (2.97 in 67 votes)
'99 Bottles of Beer in VBS
'coded by exec

For i = 99 To 0 Step -1
    Select Case i
        Case 1
            msg i & " Bottle of beer on the wall, " & i & " bottle of beer."
            msg "Take one down and pass it around, no more bottles of beer on the wall."
        Case 0
            msg "No more bottle of beer on the wall. No more bottles of beer..."
            msg "Go to the store and buy some more... 99 bottles of beer."
        Case Else
            msg i & " Bottles of beer on the wall, " & i & " bottles of beer."
            msg "Take one down and pass it around, " & i - 1 & " bottles of beer on the wall."
    End Select
Next

Sub msg(message)
    MsgBox message, , "99 Bottles of Beer"
End Sub

Download Source | Write Comment

Alternative Versions

VersionAuthorDateCommentsRate
WSH using recursion and Microsoft AgentBob Stammers03/10/061
Demonstrates use of "class"Bruce M. Axtens09/29/050
WSF, Microsoft Agent, EN/FR/DEBob Stammers06/02/061
long versionJonathan Harrison05/17/053
short versionPhilipp Winterberg04/20/050

Comments

>>  nullplan said on 02/20/06 18:51:52

nullplan That's really bad. You get a very very big MsgBox by using that code. You know, what would be worse? One MsgBox for every 4 lines. Good luck in shutting that down. (100 MsgBoxes!! Well, it's your time, which is wasted.)

>>  exec said on 03/10/06 12:35:04

exec lol? you know this is vbs?
what else then msgboxes would you use?

>>  Bob Stammers said on 03/12/06 13:04:19

Bob Stammers If you replace the MsgBox statement with "WScript.Echo message", you'll still get a series of Message boxes if run using wscript.exe but you'll get a smooth list of lyrics if run with cscript.exe.

You could also stream the lyrics into a textfile then display using notepad.

>>  exec said on 03/27/06 09:51:08

exec Okay... You are right.
But at that time I don't knew another Method^^"

>>  Sue Harris said on 09/14/06 16:50:11

Sue Harris You need another Case for 2
When you get to i=2 your i - 1 & " bottles of beer on the wall." is still plural: 1 bottles of beer on the wall."

>>  sefo said on 07/05/09 05:54:05

sefo dim oBeer, numBeer, verse

set args = wscript.arguments
if args.count < 1 then
wscript.echo "Usage: cscript 99bob.vbs [number_of_beers]" & vbCrLf
wscript.quit(1)
end if

Set oBeer = New Beer

numBeer = args(0)

oBeer.Sing(numBeer)

class Beer

Private Sub Class_Initialize()
'constructor
End Sub

Public Function Sing(byval value)
For i = value To 0 Step -1
Select Case i
Case 2
verse = "2 bottles of beer on the wall, 2 bottles of beer." & vbCrLf &_
"Take one down and pass it around, 1 bottle of beer on the wall." & vbCrLf
Case 1
verse = "1 bottle of beer on the wall, 1 bottle of beer." & vbCrLf &_
"Take one down and pass it around, no more bottle of beer on the wall." & vbCrLf
Case 0
verse = "No more bottle of beer on the wall. No more bottle of beer..." & vbCrLf &_
"Go to the store and buy some more... 99 bottles of beer." & vbCrLf
Case Else
verse = i & " Bottles of beer on the wall, " & i & " bottles of beer." & vbCrLf &_
"Take one down and pass it around, " & i - 1 & " bottles of beer on the wall." & vbCrLf
End Select
wscript.echo verse
Next
End Function

Private Sub Class_Terminate()
'destructor
End Sub

End Class

>>  etfan said on 09/10/09 00:33:04

etfan Exec -- I took your code and gradually edited it for brevity and clarity. It's not something I would write, but I thought it was interesting to see how much shorter and clearer it came out, while still handling the i = 2 case correctly that somebody mentioned. The Case structure dropped out completely. Your subroutine really had no purpose.

'99 Bottles of Beer in VBS
'coded by exec
'improved by moi, but still not great ...

For i = 5 To 3 Step -1 'substitute 99 to 3 for the whole deal
MsgBox i & " Bottles of beer on the wall, " & i & " bottles of beer."
MsgBox "Take one down and pass it around, " & i - 1 & " bottles of beer on the wall."
Next

MsgBox "2 Bottles of beer on the wall, 2 bottles of beer."
MsgBox "Take one down and pass it around, 1 bottle of beer on the wall."
MsgBox "1 Bottle of beer on the wall, 1 bottle of beer."
MsgBox "Take one down and pass it around, no more bottles of beer on the wall."
MsgBox "No more bottles of beer on the wall. No more bottles of beer..."
MsgBox "Go to the store and buy some more... 99 bottles of beer."

>>  exec said on 02/03/10 09:27:12

exec Of course not, etfan. I intended demonstrate a function. Just as I tried to demonstrate a Switch.
"This is not a "shortest code" contest"
"Your example should demonstrate the main advantages and features of the language"
Of course there were flaws in my code, but that was 4,5 years ago^^
Thanks to you all anyway.

Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!

Name:

eMail:

URL:

Security Code:
  
Comment: