473,594 Members | 2,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

break outside a loop and a switch

Could someone tell me a beautiful way to exit from a switch and a loop in one statement ...
without using a goto... and if possible without using an auxiliary variable as i did...

int res;
while (1)
{
res = 0;
if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1)
{
aff_erreurs ("msgrcv", "Error when recieving message : %d", errno);
continue;
}
if (strcasecmp (rq_resa.mess," admin"))
printf ("Admin d'ont manage bad formatted messages...\n") ;
else
switch (rq_resa.rep)
{
case 'q':
res = working_q();
manage_error (res);
break;
case 'f':
res = working_f();
manage_error (res);
break;
default:
printf ("This function is not yet implemented...\ n");
}
if (res) break;
}

all ideas welcome,

Xavier
Jan 29 '06 #1
14 6243
serrand wrote:
Could someone tell me a beautiful way to exit from a switch and a loop
in one statement ...
without using a goto... and if possible without using an auxiliary
variable as i did...

int res;
while (1)
{
res = 0;
if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1)
{
aff_erreurs ("msgrcv", "Error when recieving message : %d",
errno);
continue;
}
if (strcasecmp (rq_resa.mess," admin"))
printf ("Admin d'ont manage bad formatted messages...\n") ;
else
switch (rq_resa.rep)
{
case 'q':
res = working_q();
manage_error (res);
break;
case 'f':
res = working_f();
manage_error (res);
break;
default:
printf ("This function is not yet implemented...\ n");
}
if (res) break;
}

all ideas welcome,


Here's a possibility:

while (1) {
...
switch (rq_resa.rep) {
case 'q':
res = working_q();
if (res == 0)
continue;
break;
case 'f':
res = working_f();
if (res == 0)
continue;
break;
...
}
manage_error (res);
break;
}

However, I would not recommend using this pattern
indiscriminatel y. Other programmers -- perhaps yourself
in six months' time -- are likely to find the control flow
confusing and contrary to the usual expectations of the
way `switch' behaves. When you confuse the programmer
(perhaps yourself), you increase the chance of introducing
errors during "routine" maintenance. There's nothing wrong
with the pattern in your original code, and the "auxiliary"
variable seems to be necessary anyhow.

Another way to rearrange your original might go something
like this:

do {
...
switch (rq_resa.rep) {
case 'q':
res = working_q();
break;
case 'f':
res = working_f();
break;
...
}
} while (res == 0);
manage_error (res);

.... and I think this is clearer than my abuse of `continue',
but perhaps a little less clear than your original.

--
Eric Sosman
es*****@acm-dot-org.invalid
Jan 29 '06 #2

serrand wrote:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ...

[snip]
beauty is in the eye of the beholder...

Jan 29 '06 #3
Eric Sosman wrote:
serrand wrote:
<snip OP and first suggection by Eric>
Another way to rearrange your original might go something
like this:

do {
...
switch (rq_resa.rep) {
case 'q':
res = working_q();
break;
case 'f':
res = working_f();
break;
...
}
} while (res == 0);
manage_error (res);

... and I think this is clearer than my abuse of `continue',
but perhaps a little less clear than your original.


I'd say that this is the "correct" way of representing what OP wanted,
and one of the cases where do { } while() is a natural choice.

Cheers

Vladimir

--
(NULL sig; hope that's OK)

Jan 29 '06 #4
Haroon Shafiq wrote:

serrand wrote:
Could someone tell me a beautiful way to exit from a switch and a
loop in one statement ...

[snip]
beauty is in the eye of the beholder...


No:

Beauty is the eye of the beer-holder... ;-)

Cheers

Vladimir

--
Bubble Memory, n.:
A derogatory term, usually referring to a person's
intelligence. See also "vacuum tube".

Jan 29 '06 #5
Vladimir S. Oka wrote:
Haroon Shafiq wrote:

serrand wrote:
Could someone tell me a beautiful way to exit from a switch and a
loop in one statement ...

[snip]
beauty is in the eye of the beholder...


No:

Beauty is the eye of the beer-holder... ;-)


Obviously, "in the eye", although the above could have some merit, too.

Cheers

Vladimir

--
Famous last words:

Jan 29 '06 #6
On Sun, 29 Jan 2006 14:10:21 UTC, serrand <xa************ @free.fr>
wrote:
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ...
without using a goto... and if possible without using an auxiliary variable as i did...
Some very little changes:

/* int res; */
int res = 0; /* initialise variables during definition makes
things easier */
/* while (1) */

while (!res)
{ /* superflous res = 0; */ if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1)
{
aff_erreurs ("msgrcv", "Error when recieving message : %d", errno);
continue;
}
if (strcasecmp (rq_resa.mess," admin")) { printf ("Admin d'ont manage bad formatted messages...\n") ; res = 4712; /* some value reprenting this error if it is
one */
break; /* or when this is not really an error then
continue; */
/* and no change to res */
} else /* will be superflous now */
switch (rq_resa.rep)
{
case 'q':
res = working_q(); /* manage_error (res); */ break;
case 'f':
res = working_f(); /* manage_error (res); /* set error code like above instead */
*/ break;
default:
printf ("This function is not yet implemented...\ n"); res = 4711; /* some value representing this error
(if it is one */ } /* unneeded as the while makes the right thing. if (res) break; */ } manage_error(re s); /* will do nothing when res is 0 (NO_ERROR) */
all ideas welcome,


There is a flaw anyway above: You would set an errorcode and break the
while whenever something gets fault. manage_error() should know that
it has only to print a single error message and do exactly that.

That is whenever one case is finished good: continue;
bad: break; (the switch)

Another possiblity is to move the whole switch (maybe inclusive the
while when it is really needed) into an own function and use "return
errorcode" whenever you have to break out and return 0 otherwise,

I prefere this because
- the new function will break its run whenever an error is dedected
so we know inside the function that anything goes well for now.
You would not even think on goto.
- anything that is to do when the switch is finished can be done there
without breaking the flow as we know that the calle gets informed
immediately about the error and only when there is no error the flow
gets on.
- I love short functions holding only organizational things where
the real work gets hidden. I learned that in the time an I8088 was
quick like a rocket.
--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2 Deutsch ist da!
Jan 29 '06 #7
Vladimir S. Oka wrote:
Vladimir S. Oka wrote:

Haroon Shafiq wrote:

serrand wrote:

Could someone tell me a beautiful way to exit from a switch and a
loop in one statement ...

[snip]
beauty is in the eye of the beholder...


No:

Beauty is the eye of the beer-holder... ;-)

Obviously, "in the eye", although the above could have some merit, too.

Cheers

Vladimir

And often best observed through the bottom of the beer-glass. :-)

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jan 29 '06 #8

"serrand" <xa************ @free.fr> wrote in message
news:43******** *************** @news.wanadoo.f r...
Could someone tell me a beautiful way to exit from a switch and a loop in one statement ... without using a goto... and if possible without using an auxiliary variable as i did...
int res;
while (1)
{
res = 0;
if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1)
{
aff_erreurs ("msgrcv", "Error when recieving message : %d", errno); continue;
}
if (strcasecmp (rq_resa.mess," admin"))
printf ("Admin d'ont manage bad formatted messages...\n") ;
else
switch (rq_resa.rep)
{
case 'q':
res = working_q();
manage_error (res);
break;
case 'f':
res = working_f();
manage_error (res);
break;
default:
printf ("This function is not yet implemented...\ n");
}
if (res) break;
}

all ideas welcome,

Xavier


Like Sosman, I think 'continue' is the way to go. All loopable cases hit a
'continue' and all exitable cases hit a 'break' 'break'. Also, there is no
use of a temporary variable 'res'. Unfortunately, without the temporary
variable, manage_error() always gets called. Also, it's 'receiving', the
'i' and 'e' are switched. Also, it's "doesn't" not "d'ont".

while (1)
{
if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1)
{
aff_erreurs ("msgrcv", "Error when receiving message : %d",
errno);
continue;
}
if (strcasecmp (rq_resa.mess," admin"))
{
printf ("Admin doesn't manage bad formatted messages...\n") ;
continue;
}
else
switch (rq_resa.rep)
{
case 'q':
manage_error (working_q());
break;
case 'f':
manage_error (working_f());
break;
default:
printf ("This function is not yet implemented...\ n");
continue;
}
break;
}
Rod Pemberton
Jan 30 '06 #9
serrand wrote:
Could someone tell me a beautiful way to exit from a switch and a loop
in one statement ...
without using a goto... and if possible without using an auxiliary
variable as i did...

int res;
while (1)
{
res = 0;
if ((i = msgrcv (msqid, &rq_resa, SZ_MsgSrcResa, pid(), 0) == -1)
{
aff_erreurs ("msgrcv", "Error when recieving message : %d",
errno);
continue;
}
if (strcasecmp (rq_resa.mess," admin"))
printf ("Admin d'ont manage bad formatted messages...\n") ;
else
switch (rq_resa.rep)
{
case 'q':
res = working_q();
manage_error (res);
break;
case 'f':
res = working_f();
manage_error (res);
break;
default:
printf ("This function is not yet implemented...\ n");
}
if (res) break;
}

all ideas welcome,

Xavier


a while loop is executed as long as the condition is true.
I do not consider while(1) an elegant way to program.
It may make your code faster and that's fine if that's what you
want, but I would use a variable that can change to false
when you want it to.

--
Ioan - Ciprian Tandau
tandau _at_ freeshell _dot_ org (hope it's not too late)
(... and that it still works...)
Jan 30 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
1979
by: Scott Brady Drummonds | last post by:
Hi, everyone, I have a bug in a script of several hundred lines of code that I cannot figure out. I have attempted (unsuccessfully) to duplicate this problem in a smaller script that I can post here but have been unsuccessful. As such, I'm posting code snippets here in the hopes that someone recognizes a very basic mistake I've made and can straighten me out. First, the portion of my text file that executes my code:
5
33699
by: viza | last post by:
Hi! Suppose I have int i,j,k; for(i=0;i<I;++i){ /* loop 1 */ for(j=0;j<J;++j){ /* loop 2 */ for(k=0;k<K;++k){ /* loop 3 */ if(test){
25
3898
by: chunhui_true | last post by:
In <<expert c>>I know the break in if wich is scoped in switch is break the switch,like: switch c case 1: if(b){ break; } ...... But like this: while(a){
7
4052
by: Colin King | last post by:
Amusingly, one can use a while(0) statement to allow one to perform a switch statement without breaks. The while (0) enables the continue statements to break out of the switch. Ugly and beautiful! void sw(int s) { switch (s) while (0) { case 0: printf("zero\n");
3
5912
by: Krish | last post by:
Hello, This is an issue that I have encountered several times but have just used a workaround to solve. What is the accepted practice for breaking out of nested loops? For example: while( ... ) { for( ... )
6
1722
by: David | last post by:
I know that by some reasons... the use of "break;" in java language is not correct, is there any similar problems with c#????
26
10199
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
3
31424
by: Yansky | last post by:
Hi, I've looked through the tutorial on w3cschools.com, but I'm still uncertain as to the difference between using break and using return. If I have a simple "for" loop that I want to stop if a condition is met, should I use break or return to stop the loop? e.g. is something like this ok? Would using return do the same thing? var dow = ;
7
4195
by: jeddiki | last post by:
Hi, I am using a function called htmlwrap() which states that it does NOT add a "<br>" to the 70 character line so that it forces a line wrap. ( the script safely wraps long words without destroying html tags which wordwrap has a tendency of doing! )) What I want to do is add that line break so that it DOES force a line wrap - but I am not sure where to insert it in the function
1
8010
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8242
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6665
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5413
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3868
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1486
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.