Skip to main content
gamedev.net
🔒 Locked

weird syntax

Started by Gaiiden Jan 31, 2004 at 4:50 AM 18 replies 1.4k views
Original Post
Gaiiden
Gaiiden
Okay guys I got one for you What the heck is this?
if (b_timer_on, !b_turn)
...
  
I didn't know you could put commas in an if statement. I know you can use them in a for loop, but what do they do in an if statement? I had meant to place && in there instead of the comma (don't ask how that happened ) but it compiled and executed cleanly. I'd appreciate any light that can be shed on it, my curiosity has been piqued _________________________________________________________________ Drew Sikora President, Lead Programmer - Blade Edge Software Staff Writer, Newsletter Editor - GameDev.net Community Relations - Game Institute
Drew Sikora
Executive Producer
GameDev.net
OdinsKrieger
OdinsKrieger
Commas in an if-Statement forces the compiler to evaluate every Expression.

ex:

if (Func1() && Func2()) { DoWhatever();};


In this case if Func1() returns false Func2() will never be called.

if you seperate them with commas, then the compiler evaluates every expression, even if the result is known after the first one.

[edited by - OdinsKrieger on January 31, 2004 6:40:28 AM]
davepermen
davepermen
quote:
Original post by Gaiiden
Okay guys I got one for you What the heck is this?

if (b_timer_on, !b_turn)
...



will result into


b_timer_on;
if(!b_turn)
...


if i''m right.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Mathematix
Mathematix
The result!

Run this

#include <iostream>
using std::cout;
using std::endl;

int main()
{
const bool b_timer_on = 0;
const bool b_turn = !b_timer_on;

if(!b_timer_on, !b_turn)
cout << "Something happened" << endl;
else
cout << "Something DID NOT happen" << endl;

if(!b_turn)
cout << "Something happened" << endl;
else
cout << "Something DID NOT happen" << endl;

if(!b_timer_on)
cout << "Something happened" << endl;
else
cout << "Something DID NOT happen" << endl;

return 0;
}

In 'if(!b_timer_on, !b_turn)' !b_turn is evaluated, as davelopermen guessed .

[edited by - mathematix on January 31, 2004 12:15:24 PM]
krez
krez
yes, they are all evaluated... in the example, though, the first one is useless and might be optimized out.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Mathematix
Mathematix
quote:
Original post by Anonymous Poster
The really interesting question though is, what happens if both (well, at least one of the non-final) terms are functions...do they both get executed ? obviously if functions alter the program state, they should be executed

As was said, nothing is ignored it's just that either 'true' or 'false' can be returned from evaluating the if-statement. I'd imagine that the reason why the last part is evaluated is to allow all updates to take place and applied prior to its evaluation.



[edited by - mathematix on January 31, 2004 12:44:21 PM]
Mathematix
Mathematix
Proof of my post just above:

#include <iostream>
using std::cout;
using std::endl;

void Statement();

int main()
{
const bool b_timer_on = 0;
const bool b_turn = !b_timer_on;

//if(!b_timer_on, !b_turn)

if(Statement(), !b_turn)
cout << "Something happened" << endl;
else
cout << "Something DID NOT happen" << endl;

if(!b_turn)
cout << "Something happened" << endl;
else
cout << "Something DID NOT happen" << endl;

if(!b_timer_on)
cout << "Something happened" << endl;
else
cout << "Something DID NOT happen" << endl;

return 0;
}

void Statement()
{
cout << "Just wanna do this first!" << endl;
}

Just had to use the shades.
Gaiiden
Gaiiden
Hey now, that''s pretty cool. Can''t think of any immediate applications for myself but at least I''ll know what to do when I think of one Thx guys

_________________________________________________________________

Drew Sikora
President, Lead Programmer - Blade Edge Software
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute
Drew Sikora
Executive Producer
GameDev.net
Mathematix
Mathematix
quote:
Original post by Gaiiden
Hey now, that''s pretty cool. Can''t think of any immediate applications for myself but at least I''ll know what to do when I think of one Thx guys

_________________________________________________________________

Drew Sikora
President, Lead Programmer - Blade Edge Software
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute

Cool, Gaiiden! :D I imagine it''s just another way to compact code and make it less readable.

Doc
Doc
quote:
Original post by Anonymous Poster
Here''s one for you (assuming a standards complient compiler)

char *bob;
...
return (1+2)[bob];



Is the same as

return bob[1+2];

So long as one''s a pointer and the other''s an integer it doesn''t matter which is in the square brackets and which is on the outside.

Image loads when I''m online since I''m too lazy to find a permanent host.The following statement is true. The previous statement is false.
Shameless promotion:
FreePop: The GPL Populous II clone.

Trienco
Trienco
hehe, im still trying hard to find a good use.. i remember one of our profs using it and it was the only time i thought "what the...".

i dont remember the exact thing, but it was one of those situations, where you dont want a new line or brackets, but have two lines.

if (something>limit) {
something=limit;
return true;
}

could be done as
if (something>limit) return something=limit, true;

not exactly readable and i guess if its just saving the extra lines
if (something>limit) {something=limit; return true;}
would be fine as well.
f@dzhttp://festini.device-zero.de
Gaiiden
Gaiiden
quote:
Original post by Mathematix
Cool, Gaiiden! :D I imagine it''s just another way to compact code and make it less readable.


haha, very true

_________________________________________________________________

Drew Sikora
President, Lead Programmer - Blade Edge Software
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute
Drew Sikora
Executive Producer
GameDev.net

Topic Locked

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

Sign in to reply to this topic.