Frederik
Frederik

Reputation: 652

Using a for loop inside switch/case

Can I use a for loop inside a switch/case?

Example code:

String[,] drinks = new String[,] { { "Cola", "20" }, { "Fanta", "20" }, { "Sprite", "20" }, { "Tuborg", "25" }, { "Carlsberg", "25" } };


switch (menuChoice)
{
  case 0:
    Console.WriteLine("Goodbye!");
    Thread.Sleep(500);
    Environment.Exit(0);
    break;
    for (int i = 0; i < drinksCount; i++)
    {
      case i+1:
      buyDrink(drinks[i,0];
      break;
    }

(More code and methods is in between these)

Basically, I create an array with the drinks this machine sells, and then I want to create a menu where to chose these drinks, but also the ability to add more drinks within the GUI.

Is this even possible?

Upvotes: 4

Views: 18439

Answers (5)

Furqan Safdar
Furqan Safdar

Reputation: 16698

You can use loops inside switch statement but not the way you are using it currently. Try to replace your code with below code:

if (menuChoice == 0)
{
    Console.WriteLine("Goodbye!");
    Thread.Sleep(500);
    Environment.Exit(0); 
}
else if (menuChoice > 0 && menuChoice < drinksCount)
{         
    buyDrink(drinks[menuChoice, 0]);
}

ADDED:

As per valuable comments, why don't you just use -1 as menuChoice for Exit, this way:

if (menuChoice == -1)
{
    Console.WriteLine("Goodbye!");
    Thread.Sleep(500);
    Environment.Exit(0);
}
else if (menuChoice > 0 && menuChoice <= drinksCount)
{
    buyDrink(drinks[menuChoice - 1, 0], Convert.ToDouble(drinks[menuChoice - 1, 1]));
}

This way you can call your static method as shown:

static void buyDrink(String drink, double drinkPrice)

Upvotes: 4

Steve
Steve

Reputation: 216273

That code could not work at all.
A break is the final (and required) statement of a case.
Looking at your example you could simply write

if(menuChoice == 0)
{
    Console.WriteLine("Goodbye!"); 
    Thread.Sleep(500); 
    Environment.Exit(0); 
}
else
{
    buyDrink(drinks[menuChoice-1,0]); 
}

EDIT: Seeing your comment about buyDrink as static method and the parameters required then you should change the call to buyDrink prepending the class name where the buyDrink method is defined and add the parameter for the price (as double value)

.......
else
{
    BeverageClass.buyDrink(drinks[menuChoice-1,0], Convert.ToDouble(drinks[menuChoice-1,1])); 
}

Upvotes: 1

Joel Rondeau
Joel Rondeau

Reputation: 7586

A better title for this question might be: Is it possible to have variable case statements?

  case i+1:

This is an invalid case statement. A the value of a case statement must be a constant. If i were a constant in this case, it would be allowed. However, i isn't, and therefore the case statement will not compile.

Having said that, although the answer to the title question is yes, as shown by other answers, the real answer here is: Not the way you're trying to do it.

Upvotes: 3

Louis Ricci
Louis Ricci

Reputation: 21086

if(menuChoice == 0) {
    Console.WriteLine("Goodbye!");
    Thread.Sleep(500);
    Environment.Exit(0);
} else if(menuChoice > 0 && menuChoice <= drinksCount) {
    buyDrink(drinks[menuChoice - 1, 0]);
}

Upvotes: 2

Lews Therin
Lews Therin

Reputation: 10995

It doesn't make sense to have code after break.

You break from the case how do you expect it to execute?

case 0:
  {   
    break;//If you break here, it exits the block.. your loop never executes
    for (int i = 0; i < drinksCount; i++)
    {
      case i+1:
      buyDrink(drinks[i,0];
      break;
    }
   }

And even if it is possible I fail to see the point. You will execute the case as long as it is greater than 0 right? So why not just use an if statement, help yourself and others?

if(menuChoice ==0)
{ 
   //dosomething
}
else if(menuChoice >0)
{
  //buy drink
}

Upvotes: 2

Related Questions