4

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?

7
  • 1
    It IS appropriate to ask homework questions on SO - please don't misunderstand my suggestion: you should tag this question as 'homework' if that is what it is.
    – payo
    Oct 15, 2012 at 18:35
  • 2
    There is no homework tag. Do not try to tag as homework. Oct 15, 2012 at 18:37
  • 1
    for one, you need to specify the 'case' for the code following the break. Right now, nothing is instructed to run that code (and in fact won't compile unless you do).
    – payo
    Oct 15, 2012 at 18:38
  • @JoelRondeau There IS a homework tag, and meta.stackexchange.com/questions/10811/…
    – payo
    Oct 15, 2012 at 18:40
  • 4
    @payo The homework tag is obsolete. See stackoverflow.com/questions/tagged/homework Oct 15, 2012 at 18:41

5 Answers 5

4

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)
3
  • You code will never hit drinks[0, 0]. Arrays are zero indexed "drinks[menuChoice, 0]" could be "drinks[menuChoice - 1, 0]" then correct the if else condition "menuChoice < drinksCount" to "menuChoice <= drinksCount" Oct 15, 2012 at 19:07
  • This leads me onto the right way. However, my buyDrink looks like this: static void buyDrink(String drink, double drinkPrice) and it won't accept the array stuff being put into it. How to fix?
    – Frederik
    Oct 15, 2012 at 19:18
  • @LastCoder, yes you are absolutely right. I have edited it now. Oct 16, 2012 at 3:48
3

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.

2

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

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])); 
}
2
  • drinks[0, 0], arrays are zero indexed. You need menuChoice - 1 Oct 15, 2012 at 19:09
  • oh yes right, corrected. Hey, also the best upvoted answer has the same issue.... :-)
    – Steve
    Oct 15, 2012 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.