Aug 11, 2011 at 9:45pm UTC
Hey guys,
for normal coding i use both "if/else" and "?". "?" made code size small to have easier view and "if/else" made code easier for others to read.
But can someone help me to know which operation is faster of them?
Aug 12, 2011 at 12:11am UTC
I would assume that the ? operator is quicker but is really only useful for macros and such
Aug 12, 2011 at 12:35am UTC
The ? operator is no faster than if/else. Often times they will compile to the exact same thing.
It's just a style issue.
if/else tends to be more verbose/clear
?: tends to be more compact/obfuscated
There are times and places for each of them.
Aug 12, 2011 at 12:54am UTC
Don't second-guess the optimizer. Write clear code.
Aug 12, 2011 at 1:26am UTC
grade>=60?cout<<"Passes":cout<<"Failed";
if grade is greater than or equel to 60, then pass else fail
This Is A Good Example . Hope It Helps U
Aug 12, 2011 at 1:40am UTC
Chathu:
That's the kind of situation where you'd be better off with if/else.
Or if you want to use the ternary operator, you'd probably want to do it like this:
cout << (grade >= 60) ? "Pass" : "Fail" ;
Last edited on Aug 12, 2011 at 1:41am UTC