Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , , , ,

Sponsored Links



How to break outer loop from switch case?

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-03-2010
Member
 
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.
Reply With Quote
  #2  
Old 12-03-2010
absolute55's Avatar
Member
 
Join Date: Nov 2005
Posts: 1,238
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;
    }
}
Reply With Quote
  #3  
Old 12-03-2010
Reegan's Avatar
Member
 
Join Date: Oct 2005
Posts: 2,299
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;
Reply With Quote
  #4  
Old 12-03-2010
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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
__________________
The FIFA Manager 2009 PC Game
Reply With Quote
  #5  
Old 12-03-2010
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
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;
    }
Reply With Quote
  #6  
Old 12-03-2010
Modifier's Avatar
Member
 
Join Date: Jan 2008
Posts: 1,502
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;
    } 
}
Reply With Quote
  #7  
Old 09-10-2010
Member
 
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..
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to break outer loop from switch case?"
Thread Thread Starter Forum Replies Last Post
Stuck in Case failed loop in L.A. Noire Game Cherilyn Video Games 9 03-10-2011 07:25 PM
Problem in switch case Jensen Ackles Software Development 5 12-03-2010 12:12 PM
Need Explanation for C# Case Break hatred Software Development 4 15-01-2010 06:19 PM
How to Break Out of a Continuous Reboot Loop in XP ? TAKODA Operating Systems 1 24-02-2009 10:44 PM
SWITCH....CASE statement in C# Aadarsh Software Development 1 10-11-2008 05:39 PM


All times are GMT +5.5. The time now is 05:24 AM.