Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8671461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:57:23+00:00 2026-06-12T18:57:23+00:00

Can I use a for loop inside a switch/case? Example code: String[,] drinks =

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:57:24+00:00Added an answer on June 12, 2026 at 6:57 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can polymorphism replace an if-else statement or Switch inside of a loop? In
How can I use Progress Dialog box with for Loop. Means either inside the
In PHP I can use a foreach loop such that I have access to
How can I use a for loop to find the first 3 perfect numbers
In Oracle, we can use FOR employee_rec in (select id from mytbl) LOOP DBMS_OUTPUT.PUT_LINE(employee_rec.id);
How can I use a batch file to loop through non-integer values? I'm trying
So I do iteration with for loop and I can't use result of result_array()
I found the below code here: How to use double buffering inside a thread
I am trying to use makeObjectsPerformSelector instead of using a loop and I can't
The following code segment enters an infinite loop inside the BOOST_FOREACH statement, and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.