Results 1 to 7 of 7

Thread: How to break outer loop from switch case?

  1. #1
    Join Date
    Aug 2009
    Posts
    56

    How to break outer loop from switch case?

    Hello to all,
    I am last year computer science student. I am recently started learning c# language. I have written following code. Can anyone tell me how to break outer loop from switch case? Please help me.
    Code:
    for(int k=0;k<15;k++)
    {
        switch(k)
        {
        case 6:
        break;
        case 7:
       
        }
    }
    Thank you.
    Last edited by KALANI84; 12-03-2010 at 03:44 PM.

  2. #2
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to break outer loop from switch case?

    You can break outer loop from switch case in the following way. It is very easy to do this. Just try to understand this. In the following code I have use for loop to get right output. I have use DoSwitch(int k) condition to get result.
    Code:
    for(int k=0;k<15;k++)
    {
       if (!DoSwitch(k))
           break;
    }
    
    bool DoSwitch(intk)
    {
     switch(k)
        {
        cases 6:
          return true;
        cases 7:
          return false;
        }
    }

  3. #3
    Join Date
    Oct 2005
    Posts
    2,393

    Re: How to break outer loop from switch case?

    There is no need to right any another code for this. You have to use following code to do this. In the following code I have use breakit as bool variable. I also have use for loop with if statement. I also have use switch(i) to check case 6 and case 7.
    Code:
    bool breakits = false; 
    
    for(int k=0;k<15;k++)
    {
        if(breakits==true)break; 
    
        switch(k)
        {
            cases 6:
                break;
            cases 7:
       
                breakits = true; 
                continue; 
        }
    }
    cout<<"done"<<endl;

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: How to break outer loop from switch case?

    The break statement terminates the execution of the nearest enclosing loop or conditional statement in which it appears. Control passes to the statement that follows the terminated statement, if any. Break is used with the conditional switch statement and with the do, for, and while loop statements. The following example illustrates the use of the break statement in a for loop.
    Code:
    // break_statement.cpp
    
    #include <stdio.h>
    
    int main()
    {
        int i;
    
        for (i = 1; i < 10; i++)
        {
            printf_s("%d\n", i);
    
            if (i == 4)
                break;
        }
    }  // Loop exits after printing 1 through 4

  5. #5
    Join Date
    Feb 2008
    Posts
    1,852

    Re: How to break outer loop from switch case?

    In a switch statement, break causes the program to execute the next statement after the switch. Without a break statement, every statement from the matched case label to the end of the switch, including the default, is executed. In loops, break terminates execution of the nearest enclosing do, for, or while statement. Control passes to the statement that follows the terminated statement, if any.
    Code:
    bool exitLoop = false;
    
    for(int a = 0; (a < 15) && !exitLoop; a++)
    {
        switch(a)
        {
           cases 6:
              break;
           cases 7:
              exitsLoop = true;
              break;
        }

  6. #6
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to break outer loop from switch case?

    You have to use either of the following two code to break outer loop from switch case. It is very simple to do this. In first code you have to use goto out statement. In the second code you have to use break statement. In the first code I also have use for loop to get proper result.
    Code:
    for (int k=0; k < 15; k++) 
    { 
       switch(k) 
       { 
           cases 6: break; 
    
           
           cases 7: goto out;
        } 
    }
    out:
    Now you have to use following code.

    Code:
    for (int k=0, out=0; k < 15 && out != 0; k++) 
    { 
       switch(k) 
       { 
           cases 6: break; 
    
    
           cases 7: out = 1; break;
        } 
    }

  7. #7
    Join Date
    Oct 2010
    Posts
    1

    question Re: How to break outer loop from switch case?

    can anyone help how make this like this?..

    MENU
    [M]athematical Operation-------->(1)ADD
    (2)SUB
    (3)MUL
    (4)DIV
    Choice:3
    Enter num1:3
    Enter num2:2
    Answer:6-->cant use this(num1,*2(x),&,/..)

    [R]everse be word------>Enter any word:HELLO SJIT
    answerJIT HELLO
    [F]ind and replace--->Enter any word:HELLO SJIT
    Find:O S
    Replace:X
    Result:HellXjit
    Choice:


    all i can use is loop,switch,and do_switch...
    plss help me..

Similar Threads

  1. Stuck in Case failed loop in L.A. Noire Game
    By Cherilyn in forum Video Games
    Replies: 9
    Last Post: 03-10-2011, 06:25 PM
  2. How to use Switch Case Expression in PHP?
    By super soaker in forum Software Development
    Replies: 5
    Last Post: 06-03-2010, 03:07 AM
  3. Need Explanation for C# Case Break
    By hatred in forum Software Development
    Replies: 4
    Last Post: 15-01-2010, 06:19 PM
  4. How to Break Out of a Continuous Reboot Loop in XP ?
    By TAKODA in forum Operating Systems
    Replies: 1
    Last Post: 24-02-2009, 10:44 PM
  5. SWITCH....CASE statement in C#
    By Aadarsh in forum Software Development
    Replies: 1
    Last Post: 10-11-2008, 05:39 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,869,887.47697 seconds with 17 queries