Breaking out of shorthanded if statement [duplicate] - c++

This question already has answers here:
Why can't I use a "break" statement inside a ternary conditional statement in C++?
(2 answers)
Closed 7 years ago.
I like to use shorthanded "if statements" of the format
if-condition?then-statement:else-statement
Whys is it that this works...
if (num==0)break;else continue;
And this doesn't?
num==4?break:continue;
Seeing as the two statements are logically equivalent.

The first is a statement, the second is an expression. You cannot use statements in expressions.
The syntax of if is:
if (<expression>) <statement> [else <statement>]
The syntax of the expression is:
<Boolean expression> ? <expression> : <expression>
The syntax does not allow statements in an expression.

Related

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

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.

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.

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.

why this works: C++ last statement as result of expression [duplicate]

This question already has answers here:
Are compound statements (blocks) surrounded by parens expressions in ANSI C?
(2 answers)
Warning "Use of GNU statement expression extension"
(4 answers)
Closed 7 years ago.
I found strange macros in a driver implementation that I can't explain to myself.
Simplified example is:
cout << ({int i=0; while(i<10) {++i;} i;}) << endl;
It will output 10.
But why this does expression become an rvalue at all? It seems to work in C and in C++.
Can someone explain me? Pointing to keywords and to reference will be great.
The is a GCC extension:
A compound statement enclosed in parentheses may appear as an
expression in GNU C.
The last thing in the compound statement should be an expression
followed by a semicolon; the value of this subexpression serves as the
value of the entire construct.