if/else vs "?"(conditional operator)

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?
I would assume that the ? operator is quicker but is really only useful for macros and such
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.
Don't second-guess the optimizer. Write clear code.
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
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
Topic archived. No new replies allowed.