c# 99 bottles of beer

C# 99 bottles of beer on the wall is an easy programming challenge. The challenge can be solved in at least dozens of ways. Here is one quite compact way of doing it using string interpolation. If you are not used to string interpolation it might look a bit confusing, but it is not that hard in this simple example.

Here is the complete code for my C# 99 bottles of beer on the wall. You see the naming of the application is not good. Don’t start the name with a number. Dotnet adds an underscore. I totally forgot.

using System;
using System.Threading;

namespace _99bottles
{
    class Program
    {
        static void Main(string[] args)
        {
            int bottlesOfBeer = 99;
            int delayInMiliseconds = 200;

            for(int i = bottlesOfBeer; i > 0; i--) {
                Console.WriteLine($"{i} bottle{(i==1?"":"s")} of beer on the wall, {i} bottle{(i==1?"":"s")} of beer.");
                Console.WriteLine($"Take one down and pass it around, {i-1} bottle{(i==2?"":"s")} of beer on the wall.\n");
                Thread.Sleep(delayInMiliseconds);
            }
        }
    }
}

Let us look at the code. The For loop is pretty standard. Sets the i variable to 99 and counts downwards as long as i is above 0.

for(int i = bottlesOfBeer; i > 0; i--) {
    // Code goes here
}

Then it is string interpolation. To use string interpolation expressions in a string you need to put a dollar sign in front of the string. The {i} just take the i variable from the for loop and display the number (of beers). The next expression is a bit more interesting. Because if there is more than 1 beer we use bottles, and if there is only 1 beer we want to use bottle. To fix that we can use this expression: bottle{(i==1?””:”s”)}

This display bottle, and then we have a ternary expression to decide if we want to add an s or not. So if i equal 1 we add nothing to the word bottle. But if it is not 1, we add an s to the word bottle.

Console.WriteLine($"{i} bottle{(i==1?"":"s")} of beer on the wall, {i} bottle{(i==1?"":"s")} of beer.");

Ternary expression needs to be in parantheses. A ternary expression goes like this: (Expression ? True statement : False statement). In our example (i == 1 ? “” : “s”). If i is 1 add nothing “”, if the expression is false add an “s”.

In the next line we print out there we actually subtract 1 from i directly in the string. Here is how the first loop will look like:

99 bottles of beer on the wall, 99 bottles of beer.
Take one down and pass it around, 98 bottles of beer on the wall.

So in the second line the i is still 99 but we subtract 1 in the value we display. Therefore we will need to check if i is equal to 2 to determine if we want bottle or bottles.

Console.WriteLine($"Take one down and pass it around, {i-1} bottle{(i==2?"":"s")} of beer on the wall.\n");

If you want a pause between the verses we can do this.

Thread.Sleep(delayInMiliseconds);

So that’s how how to make a C# 99 bottles of beer on the wall text writer.

Check out how to install DotNet on Debian 10

Leave a Reply