I am addicted to "braceless" ifs, like this:
if (a) b++, c++, d = e;
But one annoying thing is that return
cannot be a part of the last part. Intuitively I feel why is that, but can anyone explain in programming language terms why this will not compile?
main() {
int a, b, c, d, e;
if (a) b = c, d = e, return;
}
If you care, please also explain why is that designed like that, it seems like a flaw to me. I can understand in C but in C++ it could have been redesigned without major compatibility loss with the existing C code.
Just for comparison: these will compile and do exactly what expected:
while (a < 10) a++, b--, c += 2;
while (a < 10) if (a == 5) half = a, save();
Compile-time errors occur when there are syntactical issues present in application code, for example, missing semicolons or parentheses, misspelled keywords or usage of undeclared variables. These syntax errors are detected by the Java compiler at compile-time and an error message is displayed on the screen.
Line 2 will generate a compiler error. C. The code will compile but will give an error at execution time.
The clearError() method of PrintStream Class in Java is used to clear the error state of this PrintStream instance. It clears any error that might have or not happened in the stream. Hence the checkError() method will always return false after this method. Parameters: This method do not accepts any parameter.
Answer. Answer: here is your answer; system.
The "comma" operator is exactly that, an operator. It's left and right sides must be expressions, and return
is not an expression.
To elaborate, the comma operator evaluates its left-hand side first, and discards the value. Then, it evaluates its right-hand side, and the whole comma expression evaluates to the right-hand side's value.
It's similar to this:
template <typename T, typename U>
U operator,(T t, U u)
{
return u;
}
Therefore, you cannot put anything in a comma expression that is not an expression itself.
If you're looking to simultaneously execute a series of statements and group them together, that's exactly what ;
and {}
are for. There is no reason to duplicate that behavior in the comma operator.
It can be done the following way
if (a) return ( b = c, d = e, 0 );
Oe if there is no return expression
if (a) return ( b = c, d = e, ( void )0 );
It may be open to question whether this answers the question the OP was really asking, but in case anybody cares about why the comma operator was designed the way it was, I think it goes back to BCPL.
In BCPL, you could combine a series of assignments like:
L1 := R1
L2 := R2
...into a single statement (command) like:
L1, L2 := R1, R2
Much like in C and C++, these were executed in order from left to right. Unlike C and C++, this "comma operator" didn't produce a single expression (at least as C uses the term).
BCPL also had a resultis
that let you make a block of statements into something almost like a function.
At least to me, it looks like in C, Dennis1 decided decided to sort of combine these two concepts into a single one that was rather simpler: a comma operator that would allow evaluation of a number of expressions in succession, and yield a single result.
Reference: BCPL Reference Manual
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With