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

  • Home
  • SEARCH
  • 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 6549279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:04:26+00:00 2026-05-25T12:04:26+00:00

I have a code snippet : int n = 0; for (int i =

  • 0

I have a code snippet :

int n = 0;
for (int i = 0; i < 50;i++)
{
    n = checkStatus();
    switch (n)
    {
       case 1:
          break;
          break;//This is unreachable and so i cannot Terminate the For Loop within the SWITCH
    }
}

As described in a comment I cannot terminate the For Loop directly from the Switch, only if I declare a boolean and at the End of Switch test

if(LoopShouldTerminate)
    break;

PS : or maybe I’m very confused!

[POST]
I got the message ,and the problem is Solved ,but i would like to asume that using Switch within a for loop isn’t a god idea ,because i heard from lot of developer’s i should break from loop in the moment when i get the desired result ,so using switch need’s extra boolean or push the Int i value to 50 direclty , but what would happen if we’re using while loop ?

  • 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-05-25T12:04:27+00:00Added an answer on May 25, 2026 at 12:04 pm

    Solution 1: Move the loop and the switch to different methods:

    for(int i = 0; i < 50; ++i)
    {
        if (DoCheckStatus(i)) break;
    }
    

    …

    bool DoCheckStatus(int i)
    {
        switch(CheckStatus(i))
        {
            case 1 : return true;
            default: return false;
        }
    }
    

    Solution 2: Adapt the above to eliminate the loop with an eager extension method:

    static void DoWhile<T>(this IEnumerable<T> sequence, Func<T, bool> predicate)
    {
        foreach(T item in sequence) 
            if (!predicate(item)) return;
    }
    
    ...
    
    Enumerable.Range(0, 50).DoWhile(DoCheckStatus)
    

    Solution 3: Adapt the above to eliminate the loop and the helper method:

    Enumerable.Range(0, 50).DoWhile(i=> 
    { 
        switch(CheckStatus(i))
        {
            case 1 : return true;
            default: return false;
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code snippet that I want to simplify: switch (typeString) { case
I have this much Code snippet: int *filePointer; float *valPtr; *valPtr = 5.6; filePointer
Have a look at this code snippet struct S{ int i; int j;}; int
Have a look at this code snippet: int i = 10; int *pi =
I have following code snippet: class ABC{ public: int a; void print(){cout<<hello<<endl;} }; int
I have this code snippet: <div id=div1> </div> <div id=div2> <h3>This is the content</h3>
I have this code snippet inside a function that checks if an object exists
VS 2008 I have this code snippet I found on a VB website. But
Say, I have a code snippet like this: public static void main(String[] args) {
I have this code: protected void onListItemClick(ListView l, View v, int position, long id)

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.