473,396 Members | 1,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Replacing break and continue with structured equivalent

Hi

Is there any way by which we can replace break and continue with structured equivalent.

We can cause loop continuation test to fail but break exits immediately from the point it is encountered so the statements after it are not executed.

Eg

int i=1,j;
for(j=0;j<10;j++)
{
break;
i++;
}

I can replace this as

for(j=0;j<10;j++)
{
j=11;
i++;
}
But i is incremented in second case. I am struck in this. Please help
Jun 11 '11 #1

✓ answered by donbock

Consider the following abstract code snippet ...
Expand|Select|Wrap|Line Numbers
  1. while (not primaryTermination) {
  2.    block1;
  3.    if (secondaryTermination)
  4.       break;
  5.    block2;
  6.    if (skipCondition)
  7.       continue;
  8.    block3;
  9.    }
The break and continue statements can be eliminated as follows. There are many similar variants.
Expand|Select|Wrap|Line Numbers
  1. while (not primaryTermination) {
  2.    block1;
  3.    if (secondaryTermination) {
  4.       primaryTermination = true;
  5.       }
  6.    else {
  7.       block2;
  8.       if (not skipCondition) {
  9.          block3;
  10.          }
  11.       }
  12.    }
Whether this results in better code is in the eye of the beholder.

1 7508
donbock
2,426 Expert 2GB
Consider the following abstract code snippet ...
Expand|Select|Wrap|Line Numbers
  1. while (not primaryTermination) {
  2.    block1;
  3.    if (secondaryTermination)
  4.       break;
  5.    block2;
  6.    if (skipCondition)
  7.       continue;
  8.    block3;
  9.    }
The break and continue statements can be eliminated as follows. There are many similar variants.
Expand|Select|Wrap|Line Numbers
  1. while (not primaryTermination) {
  2.    block1;
  3.    if (secondaryTermination) {
  4.       primaryTermination = true;
  5.       }
  6.    else {
  7.       block2;
  8.       if (not skipCondition) {
  9.          block3;
  10.          }
  11.       }
  12.    }
Whether this results in better code is in the eye of the beholder.
Jun 11 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jay | last post by:
G'day all This registration form checks for the submit button then displays the next form from the include statement. But before it displays the next form it will check to make sure the user...
5
by: Ann | last post by:
I have trouble sometimes figuring out where break and continue go to. Is there some easy way to figure it out, or a tool? TIA Ann
3
by: Jianli Shen | last post by:
const DInst *stopAtDst = 0; while (dinst->hasPending()) { if (stopAtDst == dinst->getFirstPending()) break; ///////break? break? break? is this break outof the whole while loop and...
5
by: tony collier | last post by:
To break out of a loop i have seen some people use RETURN instead of BREAK I have only seen RETURN used in functions. Does anyone know why RETURN is used instead of BREAK to kill loops?
25
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
6
by: Stone | last post by:
I am looking for a code that gives the following output.It will help understand the break and continue statements. 0 0 1 0 2 4 0 3 6 9 0 4 8 12 16 0 5 10 15 20 25 0 6 12 18 24 30 36
26
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++) {
1
by: Matt Chisholm | last post by:
Hi. I was wondering if there had ever been an official decision on the idea of adding labeled break and continue functionality to Python. I've found a few places where the idea has come up, in...
16
by: lovecreatesbea... | last post by:
Are the following two lines equal? Suppose the expression i++ doesn't overflow. They behave differently in my code. if (!p ? i++ : 0) break; if (!p){ i++; break;} Thank you for your time.
18
by: fshsoup | last post by:
If i write this $page = 'start'; for (;;) { include "$page.inc"; } and inside start.inc I put a "break;" it should work, right? no it doesn't. And why isn't there any "goto" statement in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.