Raised This Month: $7 Target: $400
 1% 

[TUT/INFO] Condition operator aka ? and :


Post New Thread Reply   
 
Thread Tools Display Modes
stupok
Veteran Member
Join Date: Feb 2006
Old 10-25-2008 , 17:58   Re: [TUT/INFO] ? and : function
Reply With Quote #11

Code:
server_print("%s: %d kill%s", kname, g_Kills[k] > 1 ? "s" : "")

->

Code:
server_print("%s: %d kill%s", kname, g_Kills[k] == 1 ? "" : "s")

0 kills
1 kill
2 kills

Also, since this is supposed to be instructive, I think you should simply show two methods of achieving the same result.

Example 1:
Code:
if( x > 1 ) {     y = 1 } else {     y = 0 }

Example 2:
Code:
y = ( x > 1 ? 1 : 0 )

These are equivalent.
stupok is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 10-25-2008 , 17:58   Re: [TUT/INFO] ? and : function
Reply With Quote #12

Quote:
Originally Posted by atomen View Post
aka "condition operator"...
Yeah....changed thread name with this.
__________________

anakin_cstrike is offline
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 10-25-2008 , 18:42   Re: [TUT/INFO] Condition operator aka ? and :
Reply With Quote #13

Updated: comparison between condition operator and if or switch statement.
__________________

anakin_cstrike is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 10-26-2008 , 07:54   Re: [TUT/INFO] ? and : function
Reply With Quote #14

Quote:
Originally Posted by danielkza View Post
Don't forget you should be very careful while using this expression. It has strange precedence orders, and can lead to unexpected behavior.
Could you give an example or two please?
__________________
No support via PM.
Lee is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 10-26-2008 , 10:10   Re: [TUT/INFO] ? and : function
Reply With Quote #15

Quote:
Originally Posted by Lee View Post
Could you give an example or two please?
Code:
new a=1, b=2, c=3,d
d=a ? b : a + b
Could you tell what would be the result of this expression? It could be interpreted in many ways.

With the correct brackets:
Code:
d = a ? b : (a+b)
But you have to be careful. It may not be what you want. You may want this:
Code:
d = (a ? b : a) + b
So if you have any doubts, just make sure the results of the expression are within brackets so you don't run in any problems like those, which can be hard to find out.
__________________

Community / No support through PM
danielkza is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 10-26-2008 , 14:42   Re: [TUT/INFO] Condition operator aka ? and :
Reply With Quote #16

I wouldn't call that strange at all. Then again, what one considers strange is heavily dependent on past experience and as I'm sure you know, different languages have different precedence rules. Superfluous parentheses happen to be a pet peeve of mine. They're certainly useful in complicated expressions, but when used as part of a trivial statement, they really annoy me. The absolute worst:

Code:
write_byte((0<<1))
This happens because many people first see bit shifting in preprocessor definitions. The parentheses are a necessary part of the preprocessor definition because operators of higher precedence could be used in conjunction with the macro, but are useless in this instance.

I think your advice about using parentheses if in doubt is sound as long as people make some kind of an effort to familiarise themselves with Pawn's precedence rules (page 112).

Can you tell that I've been wanting to get that off my chest for a long time? More good advice; take no notice of me and worry about getting stuff done.
__________________
No support via PM.

Last edited by Lee; 10-26-2008 at 14:58.
Lee is offline
danielkza
AMX Mod X Plugin Approver
Join Date: May 2007
Location: São Paulo - Brasil
Old 10-26-2008 , 15:17   Re: [TUT/INFO] Condition operator aka ? and :
Reply With Quote #17

Quote:
Originally Posted by Lee View Post
I wouldn't call that strange at all. Then again, what one considers strange is heavily dependent on past experience and as I'm sure you know, different languages have different precedence rules. Superfluous parentheses happen to be a pet peeve of mine. They're certainly useful in complicated expressions, but when used as part of a trivial statement, they really annoy me. The absolute worst:

Code:
write_byte((0<<1))

This happens because many people first see bit shifting in preprocessor definitions. The parentheses are a necessary part of the preprocessor definition because operators of higher precedence could be used in conjunction with the macro, but are useless in this instance.

I think your advice about using parentheses if in doubt is sound as long as people make some kind of an effort to familiarise themselves with Pawn's precedence rules (page 112).

Can you tell that I've been wanting to get that off my chest for a long time? More good advice; take no notice of me and worry about getting stuff done.
The ternary operator is an special exception among the others since it's right-associative. I just advised for unexperiencied coders, or those who didn't know it before. I also think excessive parentheses make code bloated, but if used correctly, they are a great tool to improve clarity.
__________________

Community / No support through PM
danielkza is offline
Lee
AlliedModders Donor
Join Date: Feb 2006
Old 10-26-2008 , 18:36   Re: [TUT/INFO] Condition operator aka ? and :
Reply With Quote #18

Quote:
Originally Posted by danielkza View Post
The ternary operator is an special exception among the others since it's right-associative.
It is in PHP and C/C++, but not in Pawn, according to the current version of the specification anyway.

Quote:
Originally Posted by danielkza View Post
I also think excessive parentheses make code bloated, but if used correctly, they are a great tool to improve clarity.
You're absolutely right; I just needed an excuse to whinge.
__________________
No support via PM.
Lee is offline
Hawk552
AMX Mod X Moderator
Join Date: Aug 2005
Old 11-01-2008 , 16:26   Re: [TUT/INFO] Condition operator aka ? and :
Reply With Quote #19

Your last example is pointless and has a bunch of useless, extra instructions. You can just use "return g_Gag[id]".
__________________
Hawk552 is offline
Send a message via AIM to Hawk552
anakin_cstrike
Veteran Member
Join Date: Nov 2007
Location: Romania
Old 11-01-2008 , 18:48   Re: [TUT/INFO] Condition operator aka ? and :
Reply With Quote #20

Quote:
Originally Posted by Hawk552 View Post
Your last example is pointless and has a bunch of useless, extra instructions. You can just use "return g_Gag[id]".
Yea but is a bool...or is equivalent with 0 (false) and 1 (true) when using in return ?
And is a sample example
__________________

anakin_cstrike is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 05:20.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode