Skip to main content
GameDev.net gamedev.net
🔒 Locked

arithmetic condition

Started by Luya Jan 4, 2003 at 2:02 AM 7 replies 450+ views
Original Post
Luya
Luya
6 tutorials later, I noticed some codes that can be easily by a single line code. This code is the arithmetic condition. For those who forgot the definition, it is like the normal if...else condition with a difference: it''s written like
(condition?true:false); 
for those who know algebra this example will help
(a > b?a:b); 
. The equivalent is
  
if(a > b)
{return a;}
else
{return b;}
  
It is possible to use the function to return value. Let''s take the look on this example from lesson 7:
  
if (!light)				// If Not Light

{
    glDisable(GL_LIGHTING);		// Disable Lighting

}
else					// Otherwise

{
    glEnable(GL_LIGHTING);		// Enable Lighting	

}
  
The equivalent is
(!light?glDisable(GL_LIGHTING):glEnable(GL_LIGHTING)); 
. Don''t forget to add this library: #include <math.h>. Use it wisely .
joanusdmentia
joanusdmentia
You don''t need math.h to use ?: it''s just part of the language.

"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
CraZeE
CraZeE
oh, and its called a ternary operator.. the one and only kind of it in C/C++. not sure of its existence in other languages.
To learn, we share... Give some to take some -
Fruny
Fruny
a few corrections and additions.

- Using the ternary operator doesn''t require <math.h>, it is a built-in. If you''re using C++, it is better to include <cmath> than <math.h>. Also note it is a header, not a library
- Be careful when using it with functions returning void. And in the general case, the types of the true-expression and the false-expression must be compatible.
- The ternary operator can be used in expressions, not just statements : printf( "You have %s", win?"won":"lost" ); (won?account:jackpot) += 100; and so on...

Not that it''s always a good idea to use it...


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
titan2782
titan2782
it is not recommended. I''ve read 3 books that explain it and how to use it, but they all say to not use it.

IT Administrator / Software Engineere
http://drdsoftware.cjb.net
Luya
Luya
Oooops, it''s true. is a header file. First time I hear about . I would like to know more about the non-recommended use of ternary operator.
wild_pointer
wild_pointer
What makes more sense in a tutorial?


if (!light) // If Not Light
{
glDisable(GL_LIGHTING); // Disable Lighting}
else // Otherwise
{
glEnable(GL_LIGHTING); // Enable Lighting
}

or

(!light?glDisable(GL_LIGHTING):glEnable(GL_LIGHTING));


You can cram anything you want onto one line. Hey, why have whitespace at all, right?




[My site|SGI STL|Bjarne FAQ|C++ FAQ Lite|MSDN|Jargon]
Ripped off from various people
[size=2]
Crispy
Crispy
personal recommendation: only use the ?: operator in inlined functions such as:


int TPerson::GetSomeFactor()
{
return(PersonType == PT_WOMAN ? (Age * Weight / Factor) : ((Age * Weight + SecretSize) / Factor));
}


Other than that, it's best to use the if...else block for the sake of readability.

Crispy

EDIT: missing parenthesis


[edited by - crispy on January 5, 2003 9:04:33 AM]
"Literally, it means that Bob is everything you can think of, but not dead; i.e., Bob is a purple-spotted, yellow-striped bumblebee/dragon/pterodactyl hybrid with a voracious addiction to Twix candy bars, but not dead."- kSquared

Topic Locked

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

Sign in to reply to this topic.