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 9307747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:27:38+00:00 2026-06-19T00:27:38+00:00

I have a while loop and inside this while loop I have a foreach

  • 0

I have a while loop and inside this while loop I have a foreach loop.

Here I learned how to skip the currently interaction on a loop by using Continue; Return; Break. But I need to leave the while loop when i’m inside the foreach loop is that possible ?

I’m on a interaction inside of the foreach loop and I want to leave the foreach and go to the next interaction of the while. How May I do that ?
Like so:

while (!reader.EndOfStream)  //COMEÇO DO WHILE
{
  ///Some Codes

  foreach(string s in checkedlistbox1.items)
        {
          switch(s)
           {
             case "1":
                if( 1 > 0)
                 {
                    ///HERE I WANT TO GO TO THE NEXT INTERACTION OF THE WHILE
                    ///When I use CONTINUE; HERE, I GO TO THE NEXT INTERACTION OF THE FOREACH. BUT I NEED TO GO TO THE NEXT OF THE WHILE.
                 }
           }
        }
}

Here’s what I want do to:
I’m reading a file.txt line by line, and writing a new one with these values and some others things… Some of these values may be required (seted by user), if a required fieldis empty, so I do nothing, and go to the next while interaction…

  • 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-19T00:27:40+00:00Added an answer on June 19, 2026 at 12:27 am

    You need to break from the switch and then from the foreach, whilst having a variable set.

    You can then check that variable to see whether you should continue to the next while iteration.

    Do it like this:

    while (!reader.EndOfStream)
    {
        // Some Codes
    
        bool skipToNext = false;
    
        foreach (string s in checkedlistbox1.items)
        {
            switch (s)
            {
                case "1":
                    if (1 > 0)
                    {
                        skipToNext = true;
                        break;
                    }
            }
    
            if (skipToNext) break;
        }
    
        // in the case of there being more code, you can now use continue
        if (skipToNext) continue;
    
        // more code
    }
    

    Example of this flow working

    var list = new List<string> { "0", "1", "2" };
    int a = 0, b = 2;
    
    while (a++ < b)
    {
        // Some Codes
    
        bool skipToNext = false;
    
        foreach (string s in list)
        {
            Debug.WriteLine("{0} - {1}", a, s);
    
            switch (s)
            {
                case "1":
                    if (1 > 0)
                    {
                        Debug.WriteLine("Skipping switch...");
                        skipToNext = true;
                        break;
                    }
            }
    
            if (skipToNext)
            {
                Debug.WriteLine("Skipping foreach...");
                break;
            }
        }
    
        // in the case of there being more code, you can now use continue
        if (skipToNext)
        {
            Debug.WriteLine("Skipping to next while...");
            continue;
        }
    
        // more code
    }
    

    Outputs:

    1 - 0
    1 - 1
    Skipping switch...
    Skipping foreach...
    Skipping to next while...
    2 - 0
    2 - 1
    Skipping switch...
    Skipping foreach...
    Skipping to next while...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a while/for loop with a foreach loop inside. Reading the code will
I have a nested while loop inside a foreach loop where I would like
I have some problems with controlling a while loop inside an event structure. Say
The while loop I have while reading in from a file doesn't break. I'm
Currently, I have a while loop for listening the port. When a socket accepta
I have a function creating entries via grid() like this: (replace while-loop with function,
I'm stuck in my code! I have a Do -While Loop and inside I
I have an if statement inside a while loop to filter out filenames that
I'm running a WebSocketHandler with Tornado, and I have a while loop inside the
Really struggling with this one: I have an existing foreach, containing an if loop

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.