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
🔒 Locked
weird syntax
Advertisement
Original Post
Okay guys I got one for you
What the heck is this?
) 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
Commas in an if-Statement forces the compiler to evaluate every Expression.
ex:
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]
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]
quote:
Original post by Gaiiden
Okay guys I got one for youWhat 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
The result!
Run this
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]
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]
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]
Proof of my post just above:
Just had to use the shades.
#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.
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
President, Lead Programmer - Blade Edge Software
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute
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 oneThx 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.
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.
The following statement is true. The previous statement is false.Shameless promotion:
FreePop: The GPL Populous II clone.
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.
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.
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
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement