....而且我认为这比我滥用继续, 更清楚,但可能比你原来的更清晰。 - Eric Sosman es ***** @ acm-dot-org.inva 盖子 serrand写道:有人能告诉我吗一种美妙的方式退出开关h和一个声明循环...... [snip] 美丽在旁观者的眼中...... Eric Sosman写道: serrand写道: < snip OP and first suggection by Eric> 重新排列原件的另一种方法可能是这样的: 做{ ... 开关(rq_resa.rep) ){ case''q'': res = working_q(); break; case''f'': res = working_f(); 休息; ... } } while(res == 0); manage_error(res); ..我认为这比我滥用继续更清楚,但可能比你原来的更清晰。 我会的说这是正确的表示OP想要什么的方式, 和其中一个案例{} while()是一个自然的选择。 干杯 弗拉基米尔 - (NULL sig;希望'没关系' 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");elseswitch (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 解决方案 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 patternindiscriminately. Other programmers -- perhaps yourselfin six months'' time -- are likely to find the control flowconfusing and contrary to the usual expectations of theway `switch'' behaves. When you confuse the programmer(perhaps yourself), you increase the chance of introducingerrors during "routine" maintenance. There''s nothing wrongwith the pattern in your original code, and the "auxiliary"variable seems to be necessary anyhow.Another way to rearrange your original might go somethinglike 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.invalidserrand 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... 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.CheersVladimir--(NULL sig; hope that''s OK) 这篇关于在循环和开关外面打破的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 09:46