|
| |||||||||
| Tags: break, c sharp language, class, file handle, function, main, object, oops |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| 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:
}
} Last edited by KALANI84 : 12-03-2010 at 03:44 PM. |
|
#2
| ||||
| ||||
| 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
| ||||
| ||||
| 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;
__________________ Grand Theft Auto 4 PC Video Game |
|
#4
| ||||
| ||||
| 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 |
|
#5
| ||||
| ||||
| 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
| ||||
| ||||
| 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: Code: for (int k=0, out=0; k < 15 && out != 0; k++)
{
switch(k)
{
cases 6: break;
cases 7: out = 1; break;
}
} |
|
#7
| |||
| |||
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 answer JIT 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.. |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |