C++ which if/else is faster [duplicate] - c++

This question already has answers here:
Ternary operator ?: vs if...else
(14 answers)
Closed 5 years ago.
I have seen two types of if/else, which one is faster?
if(a==b) cout<<"a";
else cout<<"b";
OR
a==b ? cout<<"a" : cout<<"b";

The ternary conditional is an abuse since it's a mere coincidence that decltype(cout<<"a") is a type that can be used in a ternary conditional:
cout << (a == b ? "a" : "b");
would be more palatable, and possibly more tractable than the if, else, which you should otherwise prefer for its clarity.
And trust your compiler to make the optimisations. checking the output assembly if you have any suspicions.

The performance of either would never be catastrophic for your program.
It all comes down to Code readability.
The tertiary operator has its limitation of only one statement either true or false.

Related

Is there always the same execution sequence for various conditions in a If Statement in C++ for all compilers? [duplicate]

This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 2 years ago.
Lets assume a simple If Statement with two conditions A and B:
If ( condA && condB)
Is the Sequenz for all compilers the Same?
condition A
condition B
And is the execution of condition B therefore optional, in case condition A is already false?
Yes. Not evaluating condition B if A is false is called short circuit logic, and this behavior is guaranteed by the language specification.

What does: "while( (stuff) ? false : (otherstuff)){}" mean? [duplicate]

This question already has answers here:
What does the question mark character ('?') mean in C++?
(8 answers)
Closed 6 years ago.
I have a c++ while loop I'm looking at:
while ((stuff) ? false : (otherstuff))
{
commands;
}
And I don't really understand what it's trying to do with the "? false :" part?
Can any one explain what this means please?
I already tried looking it up but I'm not really getting anything helpful.
It's using the ternary conditional operator to effectively perform the check:
while (!(stuff) && (otherstuff))
If stuff is true, then the first option on the ternary is evaluated (evaluating to false), if it's false, then it evaluates to otherstuff.
It's just a really bad way of writing this:
while (!stuff && otherstuff) {
}

Double increment on an integer variable, does it work as intended? [duplicate]

This question already has answers here:
Multiple preincrement operations on a variable in C++(C ?)
(2 answers)
Closed 6 years ago.
I have some code with lines which increment a counter.
++ count;
Sometimes I have an if condition which means I should increment count by 2.
count += 2;
Does "double increment'ing" work in the same way?
++ ++ count;
It would be helpful to know if both C and C++ compilers interpret this the same way.
As this is clearly syntactically correct, the question that remains is: "Is this UB because of unsequenced writes?"
It is not (in C++11 and later) because
5) The side effect of the built-in pre-increment and pre-decrement operators is sequenced before its value computation (implicit rule due to definition as compound assignment)
(From here)
So the code is fine as of C++11.
However, the sequencing rules were different before that, and pre-C++11, the code actually has UB.
In C, that code does not even compile.
The fact that the behavior is different between C and C++ and even between different C++ standards and that this question arises in the first place is a hint that the simple count += 2; is the safer and more readable version. You should prefer it over the "cute and clever" ++ ++count;.
There are two methods. One that obviously works, one where you have to ask a question on StackOverflow. There are comments saying "in C++ 11 or later"... How sure are you that your C++ code runs with C++ 11 and not something older? Even if you use extensions that were not part of an earlier language, your language could be C++0x with some extensions.
Clearly you shouldn't care whether the second method works or not, but use the one that obviously works.

Comparing bool with 1 in C++ [duplicate]

This question already has answers here:
Can I assume (bool)true == (int)1 for any C++ compiler?
(5 answers)
Closed 7 years ago.
Is it correct to compare bool to 1?
In a legacy code I find often:
if (xyz.isCounterActive() == 1)
where sCounterActive() returns bool.
Obviously, if ( xyz.isCounterActive() ) is sufficient, but If I change this, I don't know which side-effects it may cause. Software is big, buggy but the customer insists, that it is working.
Compiler is VS2008
In this case result of xyz.isCounterActive() will be implicitly converted to int. There're many rules of implicit conversion, which can be found here, for example.
Probably signature of isCounterActive changed since it was introduced, and the one, who changed it, forgot to modify all isCounterActive calls.

A multiple equality/inequalityconditions checking in if statements [duplicate]

This question already has answers here:
How can I check whether multiple variables are equal to the same value?
(3 answers)
Closed 7 years ago.
I am a newbie to programming in C++ and I have a question regarding if conditions. We are currently learning C++ at school(Using TC, i know it's an old compiler,but yeah). I am currently making a tic-tac-toe program, an undefeatable one. Now, this is my problem.
I want to check the equality of 3 variables, and run the if body only if the 3 variables are not equal to another variable.
Why is this set of code not working?
if(a==b==c!=d)
{
}
Adding parentheses doesn't help, I'm probably doing it wrong.(Please excuse my ignorance)
if((a==b==c)!=d)
{
}
Thanks in advance!
-CaptainAwesome
You have to write each condition individually and combine them using && (logical and):
if(a==b && b==c && c!=d)
{
// ...
}
Because you made this up. You can't do boolean comparisons like this.
Stick to two operands at a time and use && and || to combine results.
I'm not entirely clear on your requirements but start with something like this:
if (a == b && b == c && c != d)