Hello, I would like to break out of the loop i am in so i would put break;. My problem is i want to be able to do it inside of a switch so the break breaks out of the switch not the loop. Is there a way i can break out of both of them.
Thanks.
Hello, I would like to break out of the loop i am in so i would put break;. My problem is i want to be able to do it inside of a switch so the break breaks out of the switch not the loop. Is there a way i can break out of both of them.
Thanks.
>Is there a way i can break out of both of them.
Not an elegant way. The two most common ways are a goto and placing the entire construct inside of a function so that you can use return.
My best code is written with the delete key.
Well depending on how your loop is set up (ie what your sentinal value is) you can just set a variable to the sentinal for example when using the for loop:
just set i to 10Code:for (int i = 0; i < 10; i++)
if your using a while loop just create a state that doesnt evaluate to true in the while clause for example:
Hope this helps!Code:bool done = false; while(!done) { switch(my_var) { case 1: func1(); break; case 2: func2(); done = true; break; defualt: cerr << "\a Illegal State!\n"; break; } }
"Computer Science is no more about computers than astronomy is about telescopes"
- E.W. Dijkstra
Thanks ZAK. That worked really well. Not sure why I didn't think of that:P. Thanks.
There is no bool type in C. In C99 there is _Bool (stdbool.h) however. But the concept is the same, or similar:Code:bool done = false;
Quzah.Code:for( foo = 0; foo < bar && condition == keepgoing; foo++ ) { for( baz = 0; baz < bongo; baz++ ) { if( somecondition ) { condition = !keepgoing; break; } } }
Hope is the first step on the road to disappointment.
good call quzah! I actually posted a C++ solution to a C question...you also can not declare a variable in side of a loop in C. I would however not put a flag in the loop when you can just use the sentinal of the loop in your case bar, but you would trade off some readability. Another good way to define bool is a common way that was done in C++ before bool was part of alot of compilers (remember VC++ 1.0) which is equivalent in C.
Thanks again for correcting my errant code!Code:typdef char bool #define TRUE 1; #define FALSE 0;
"Computer Science is no more about computers than astronomy is about telescopes"
- E.W. Dijkstra
prelude told me to use this
Code:typedef enum { false, true } bool;
Prelude's bool is lot more elegant IMHO.
Help populate a c/c++ help irc channel
server: irc://irc.efnet.net
channel: #c
Elegant it may be, but it can also be a trap.
bool matched = strcmp(word1,word2) == 0;
Where types are more strictly enforced, this generates warnings.
The left is an enumeration, the right is an int.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
This is true Salem, but strcmp never was designed to return a boolean it returns an integer... it really isn't a boolean function. The enumeration code is a better elegant solution to the problem.
"Computer Science is no more about computers than astronomy is about telescopes"
- E.W. Dijkstra
>but strcmp never was designed to return a boolean it returns an integer... it really isn't a boolean function.
No, strcmp doesn't return a boolean value, but an expression that uses it with a relational operator does. However, the result is still the same type, int. The problem is the mismatch between integral and enumerated types. The standard actually allows it AFAIK, but mentions potential traps in the Common Warnings section.
>prelude told me to use this
I did? Maybe it was mentioned as an alternative or shown in example code, I typically don't recommend one option over the other for something like this, and I certainly don't recommend redefining standard names without very good cause. My example probably looked more like this:
Code:typedef enum {BFALSE, BTRUE} bool_t;
My best code is written with the delete key.