How would you write this?

A

aleksa

do {
skip: val = *src++; if (val == 1) goto skip;

*dst++ = val;
} while (val != 2);

I'm copying from src to dst all values except ONE and I stop when TWO
is copied.

How to write this so there is no "skip" label?
Or, how do I jump to the beginning of the DO/WHILE loop?

I've tried BREAK/CONTINUE:
break jumps outside of the loop.
continue jumps to the decision point (val != 2)
and what I would like is to jump to the beginning of the do/while loop.
 
T

Tim Rentsch

aleksa said:
do {
skip: val = *src++; if (val == 1) goto skip;

*dst++ = val;
} while (val != 2);

I'm copying from src to dst all values except ONE and I stop when TWO
is copied.

How to write this so there is no "skip" label?
Or, how do I jump to the beginning of the DO/WHILE loop?

I've tried BREAK/CONTINUE:
break jumps outside of the loop.
continue jumps to the decision point (val != 2)
and what I would like is to jump to the beginning of the do/while loop.

do {
while( *src == 1 ) src++;
*dst++ = *src++;
} while( dst[-1] != 2 );
 
D

David RF

do {
    skip: val = *src++; if (val == 1) goto skip;

    *dst++ = val;

} while (val != 2);

I'm copying from src to dst all values except ONE and I stop when TWO
is copied.

How to write this so there is no "skip" label?
Or, how do I jump to the beginning of the DO/WHILE loop?

I've tried BREAK/CONTINUE:
 break jumps outside of the loop.
 continue jumps to the decision point (val != 2)
and what I would like is to jump to the beginning of the do/while loop.

while ((val = *src++) != 1);
 
J

Jean-Christophe

On Apr 24, 9:54 pm, aleksa
do { skip: val = *src++; if (val == 1) goto skip;
    *dst++ = val;
} while (val != 2);

do
{ if( (val = *src++) != 1 ) *dst++ = val;
} while (val != 2);


// note that if the src buffer is large, it's
// better to use memcpy for faster performance.
 
F

Francois Grieu

do {
skip: val = *src++; if (val == 1) goto skip;

*dst++ = val;
} while (val != 2);

I'm copying from src to dst all values except ONE and I stop when TWO
is copied.

How to write this so there is no "skip" label?
Or, how do I jump to the beginning of the DO/WHILE loop?

I've tried BREAK/CONTINUE:
break jumps outside of the loop.
continue jumps to the decision point (val != 2)
and what I would like is to jump to the beginning of the do/while loop.

Hint1: do loops can be nested.

Hint2: in the context, "continue" does what just you ask, albeit not
optimally for many (if not most) compilers as we know them today. Which
is a pity, if you ask.

Hint3: if you really want a single loop and be able to jump at the
beginning without a goto, there are "for(;;){;}", "while(1){;}", or
"do{;}while(1), with the exit condition using break.

Francois Grieu
 
P

Peter Nilsson

do {
    skip: val = *src++; if (val == 1) goto skip;

    *dst++ = val;

} while (val != 2);

I'm copying from src to dst all values except ONE and I
stop when TWO is copied.

How to write this so there is no "skip" label?

while ((val = *src++) == 1 || (*dst++ = val) != 2)
;
 
S

spinoza1111

"Peter Nilsson" <[email protected]> ha scritto nel messaggio


i whould write it so:
    goto  b;
m:  ++dst
a:  ++src;
b:  val=*src; if(val==1) goto a;
    *dst=val;
    if(val!=2)  goto  m;



  while ((val = *src++) == 1 || (*dst++ = val) != 2)
    ;

it is unreadable to me

Then, with all due respect, consider learning to read code before you
write code.
 
A

aleksa

Thanks everyone.

My taste is "hint 1" from Francois Grieu:

do {
do {val = *src++;} while (val == 1);
*dst++ = val;
} while (val != 2);
 
P

Paul N

do {
    skip: val = *src++; if (val == 1) goto skip;

    *dst++ = val;

} while (val != 2);

I'm copying from src to dst all values except ONE and I stop when TWO
is copied.

How to write this so there is no "skip" label?
Or, how do I jump to the beginning of the DO/WHILE loop?

I've tried BREAK/CONTINUE:
 break jumps outside of the loop.
 continue jumps to the decision point (val != 2)
and what I would like is to jump to the beginning of the do/while loop.

Perhaps I'm missing something, but what's wrong with the following?

do {
val = *src++;
if (val != 1) *dst++ = val;
} while (val != 2);

No label required, and no nested loops. It jumps to the beginning of
the loop by getting to the end. Or is your actual code slightly
different from what you posted, and you want to avoid the test for
some reason?
 
W

Willem

Richard wrote:
)> do {
)> val = *src++;
)> if (val != 1) *dst++ = val;
)> } while (val != 2);
)>
)> No label required, and no nested loops. It jumps to the beginning of
)> the loop by getting to the end. Or is your actual code slightly
)> different from what you posted, and you want to avoid the test for
)> some reason?
)>
)
) Much better.
)
) But I prefer this. Only check for 2 when we know its not 1 ...
)
) for(int val=0;;){
) if ((val=*src++)!= 1)
) if((*dst++=val)==2)
) break;
) }
)
) I think. Not compiled. Not tested blah blah blah ... ;)

Nah, I would expect any compiler to optimize this out anyway.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
T

Tim Rentsch

Peter Nilsson said:
while ((val = *src++) == 1 || (*dst++ = val) != 2)
;

This suggests another alternative

do *dst = *src++; while( *dst == 1 || *dst++ != 2);
 
P

Phil Carmody

Tim Rentsch said:
This suggests another alternative

do *dst = *src++; while( *dst == 1 || *dst++ != 2);

I have on my desk at work an architecture where your version
would do very much the wrong thing.

Micro-spoilerette - the things that aren't seen are the
things that are important.

Rest of spoiler in headers.

Phil
 
T

Tim Rentsch

Phil Carmody said:
I have on my desk at work an architecture where your version
would do very much the wrong thing.

Micro-spoilerette - the things that aren't seen are the
things that are important.

Yes, I wrote my alternative presuming that 'volatile' wasn't
involved. Stylistically, I think it's better not to use an
extra variable if it isn't needed, and it doesn't seem to be
here. Also I expect that anyone who knows what volatile is can
make the necessary adjustments if volatile accesses are a
factor.

For that matter, 'val' could be volatile, in which case Peter's
rewrite also behaves differently from the original.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top