Well its the crazy unruly homegrown cpp programmer with another one. The classy programmers have cringed against using terminating conditions inside loop body but I highly disagree. I qualify my belief with the fact that I used loops much more than the regular programmer, since I do text parsing manually and not with regex. Plus other kinds of situtations where I needed loops. So I understand the need for terminating conditions inside loops. Infact, even if I only have one condition, I may prefer a while(true) loop; in order to do all my code inside the loop body. eg:
while(true)
{
if(condition)
{
// do something
return;
}
// .... long body
}
This format allows persons to see what action is taken on loop termination immediately.
Anyhow, I will explain the BREAK_OUT_OF keyword. It allows you to break out of outer loops or switch statements, from within an inner loop or switch statement. This keyword is set to come in the next version of cpp, cpp 2026. But dont worry, for now you can emulate it in your primitive cpp compilers. Here are the steps.
Step 1: Download the BREAK_OUT_OF keyword package with the following command:
#define BREAK_OUT_OF(ID) goto STATEMENT##ID
#define S_ID(ID)
#define DEF_S_ID(ID) STATEMENT##ID:
Dont be fooled by the goto statement in the commands. Its not a regular goto statement that we all hate. Its a jmp statement in disguise.
Step 2: Now that you downloaded the compatibility package; when you need to break out of an outer statement from within an inner statement; you first give it an ID. You do this using S_ID keyword.
while(true) S_ID(first_loop)
Step 3: Place DEF_S_ID keyword immediately after statement body. Note that in the up coming cpp 26, This will automatically be done for us.
while(true) S_ID(first_loop)
{
}DEF_S_ID(first_loop)
Last Step: break out of outer loop from within inner loop using BREAK_OUT_OF keyword
while(true) S_ID(first_loop)
{
switch(10) S_ID(1)
{
case 10: BREAK_OUT_OF(first_loop);
case 0: BREAK_OUT_OF(1);
}DEF_S_ID(1)
}DEF_S_ID(first_loop)
while (msg->state != DONE)' for the loop control.gotoappealing and, sometimes, the only clean way out. If you tend to organize your code into small functions which are only a few lines long and do one single thing each, you will never run into this problem. (Incidentally your code will be easier to read, too.)