How does the execution occurs in c++? [duplicate] - c++

This question already has answers here:
How does C++ handle &&? (Short-circuit evaluation) [duplicate]
(7 answers)
Closed 6 months ago.
if(condition1 and condition2){
//body
}
If condition1 turns out to be false, will c++ compiler check for condition2 or will it directly return false?

What you described is called short-circuit evaluation and C++ does use it: if condition1 is false, condition2 will not be checked.

Related

Is there a C or C++ equivalent to 'pass' in python? [duplicate]

This question already has answers here:
Is there an equivalent of Python's `pass` in c++ std11?
(7 answers)
Closed 2 years ago.
Is there a C or C++ equivalent to 'pass' in python? Also the same with break. For example:
while True:
if x == 1:
break
else:
pass
But in C/C++ instead?
Is there a C or C++ equivalent to 'pass' in python?
Yes. There are actually two equivalents. One is the null statement:
;
Another is the empty block statement:
{}
These are in most cases inter-changeable except the null statement cannot be used in all cases where the empty block statement can. For example, the function body must be a block statement.
In the example case, you can omit the else-statement entirely just like you can in Python.

what is the specific usecase for !! in C++ [duplicate]

This question already has answers here:
Double Negation in C++
(14 answers)
Closed 2 years ago.
Came across using !! in C++ during condition check
if ( !! (flag != 0 )){..
}
This could be directly used like
if( flag != 0 ){..
}
Is there any specific corner use case in C/C++ or is it just a form of coding style ?
In this case, it's superfluous, but in general this style is used to convert the actual expression value to
an integer type in C.
a boolean type in C++.

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.

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

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.

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) {
}