Advertisement

arithmetic condition

Started by January 04, 2003 02:02 AM
7 comments, last by Luya 22 years, 1 month ago
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 .
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 only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Advertisement
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 -
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
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
Dustin DavisOwner / CEOProgrammers Unlimitedwww.Programmers-Unlimited.com
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.
Advertisement
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]
Pouya''s tetris tutorial, page 2

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

This topic is closed to new replies.

Advertisement