Skip to main content
gamedev.net
🔒 Locked

amazing while statement !!

Started by haust May 25, 2002 at 5:10 PM 8 replies 800+ views
Original Post
haust
haust
hi all, well i''ve just discovered something i want to share with you right now (maybe some of you already knew it however....). did you know that vc6 allow this ?? : while () { } while (); i didn''t. i didn''t try to know if both conditions are actually tested at runtime but no error occured at compile time.... as far as i can remember i only learned 3 ways of using the while statement : 1 - do { } while(); 2 - while() { } 3 - while () ; well it seems there''s a 4th one does gcc allow this ??
IndirectX
IndirectX
Hate to break your excitement, but the second while statement just has an empty body, and you actually have two while statements in your code, not one.
---visit #directxdev on afternet <- not just for directx, despite the name
dalleboy
dalleboy
Sure gcc allows this, it is part of the C standard.
while (expression1)
{
statement1;
} while (expression2);
is the same thing as:
while (expression1)
{
statement1;
}

while (expression2);
Remember that whitespace does not matter. First the first block (statement1) will execute until expression1 are evaluated to false then the second (empty) block will execute until expression2 are evaluated to false.

EDIT1: Corrected some spelling and grammar errors, added some new.

[edited by - dalleboy on May 25, 2002 7:20:34 PM]
Dean Harding
Dean Harding
quote:
Original post by Anonymous Poster
gcc doesn''t accept while();. Remember, use for( ; ; ) for inifinite loops. (:


Well nothing does, that was meant to be psuedo-code. An infinite while loop would be something like while(1) {...} or while( true ) {...}


codeka.com - Just click it.
haust
haust
"the second while statement just has an empty body" : IndirectX, i don't know what you're talking about but here is some more info
while (a != 0) {
....
} while (exilir > 3);
as you see both while work with the same body

dalleboy : why did you talk about whitespaces ?? i didn't.... ok you knew that this kind if coding was allowed but how many of us knew it was ?? few of us i guess

[edited by - haust on May 26, 2002 4:09:52 AM]
Alimonster
Alimonster
I still say that Duff''s Device is cooler...

     register n = (count + 7) / 8;      /* count > 0 assumed */

switch (count % 8)
{
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
CGameProgrammer
CGameProgrammer
No, haust, they are different bodies. Your example is using a Style #2 ("while() {}") followed by a Style #3 ("while();"). It's like saying:

while(a)
{
} while(b) {}

You can see the two blocks. What's confusing you is "do" which is unusual because the while affects the previous block when using do. But when not using do, the while affects the next block. A semicolon indicates an empty while statement.

~CGameProgrammer( );

[edited by - CGameProgrammer on May 26, 2002 4:42:25 AM]
~CGameProgrammer( ); Developer Image Exchange -- New Features: Upload screenshots of your games (size is unlimited) and upload the game itself (up to 10MB). Free. No registration needed.
haust
haust
ok i understand it now !!
thanks IndirectX, dalleboy and CGameProgrammer for lighting me on this
stoopid me

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.