Jump to content
Sign in to follow this  
Hobknob

Do While

Recommended Posts

Guest JoeB

doesnt look like it! I get error "unexpected opperand 'do' "

 

LOL !!

 

How can you NOT support do/while ???

 

ok, work arround...

do{
..code to do ...
}
while(break_condition);

changes to ::

while(1)
{
... code to do ....

if( break_condition ) break;
}

 

assuming that the compiler can handle some of the basics like if(...) ...

 

 

joe

#

Share this post


Link to post
Share on other sites
Guest Pavel

You are right, the do-while construction is not supported in c2c. Personally I think it's not used by most programmers and can  be easily replaced with while or for loops. (Actually this is the first time when somebody complained about missing do-while construction). My next generation compiler will support do-while.

 

Regards,

Pavel

Share this post


Link to post
Share on other sites
Guest JoeB

Your also right, i have not used do-while loops for many years now...

 

i always use:

for(;;) {
.. do this ..;
if (condition) break;
}

 

can your compiler handle pointers? how many levels of indirection can you handle?

joe

#

Share this post


Link to post
Share on other sites

Hi pchiou,

 

your for loop equivalent is not the same as a do while  loop, but it is the same as a while loop!.

 

Consider when the condition is tested, in the do while loop the code in the loop is always executed at least once, in the while loop senario the loop code may never be excuted.

 

I'm really surprise that you don't use do while, it makes some some loops look very tidy and therefore highly readable!

 

 

 

Hi JoeB,

Equivalent code, but it does not look clean.

 

:D

Share this post


Link to post
Share on other sites
Guest JoeB

Is there a cleaner way to write an equlivilent do..while loop without actually using do..while?

 

ok, really bad thing to do i C, but...:

restart_code:
 do this;
 do this;
if( !break_condition) goto restart_code;

// loop exited

 

That is nice and tidy and readable, although goto's are frowned upon. In all my time as a C programmer, i have NEVER used a goto statment except when writing 30-second test code to evaluate an idea - this is then re-written to remove the gotos :)

 

More than often in a do..while loop, there is the need to exit the loop with complex expressions:

Consider:

do{
 x+=2;
 y+=3;
 z+=5;
if( (x*5) / (4%z) >= 12) break;
}
while( (x < 8) && (x < 9) && (z < 15) );

 

Now... we now have 4 exit conditions, and this can be rewritten as:

do{
 x+=2;
 y+=3;
 z+=3;
if(x >= 8)  break;
if(y >= 9)  break;
if(z >= 15) break;
if( (x*5) / (4%z) >= 12) break;
}
while( 1 );

Which i consider to be much less messy. This is how i derived the subsitution for the do..while loop. Shilt the while(1) to the top and rewrite as for(;;) and i end up with this code:

for(;;) {
 x+=2;
 y+=3;
 z+=3;
if(x >= 8)  break;
if(y >= 9)  break;
if(z >= 15) break;
if( (x*5) / (4%z) >= 12) break;
}

 

What about if we wanted to exit the do..while after incrementing x, but before incrementing y ? This would require an if(...) break;  condition put into the body of the do..while anyway!

 

Maybe we should run a competition for the best subsitution code... Winner gets a free licence or renewal after 1 year? (admin?)

 

 

Cheers,

 

Joe

#

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×