Is there a pattern in C to execute a while loop one more time. Currently I'm using
while(condition) {
condition = process();
// process() could be multiple lines instead of a function call
// so while(process());process(); is not an option
}
process();
which is horrible if process is of multiple lines and not a single function call.
The alternative is
bool run_once_more = 1;
while(condition || run_once_more) {
if (!condition) {
run_once_more = 0;
}
condition = process();
condition = condition && run_once_more;
}
Is there a better way?
Note: A do while loop is not a solution as it is equivalent to
process();
while(condition){condition=process();}
I want
while(condition){condition=process();}
process();
Per requests, a bit more specific code. I want to fill buffer from another_buffer and get (indexof(next_set_bit) + 1) into MSB while maintaining both masks and pointers.
uint16t buffer;
...
while((buffer & (1 << (8*sizeof(buffer) - 1))) == 0) { // get msb as 1
buffer <<= 1;
// fill LSB from another buffer
buffer |= (uint16_t) (other_buffer[i] & other_buffer_mask);
// maintain other_buffer pointers and masks
other_buffer_mask >>= 1;
if(!(other_buffer_mask)) {
other_buffer_mask = (1 << 8*sizeof(other_buffer[0]) -1)
++i;
}
}
// Throw away the set MSB
buffer <<= 1;
buffer |= (uint16_t) (other_buffer[i] & other_buffer_mask);
other_buffer_mask >>= 1;
if(!(other_buffer_mask)) {
other_buffer_mask = (1 << 8*sizeof(other_buffer[0]) -1)
++i;
}
use_this_buffer(buffer);
process is of multiple lines and not a single function call
Then make it (possibly inline) function call. – Microbalancedo ... while (condition);
can also provide an additional iteration in some cases. – Epileptoidcondition
? Is it something numeric or what? – Angio1 << (8*sizeof(buffer) - 1)
, use1u << (8*sizeof buffer - 1)
(add u). or even better1u << (CHAR_BIT * sizeof buffer - 1)
. – Cuttlebone