Consider:
for (auto i = 0; i < g.size(); ++i) for (auto j = 0; j < g.size(); ++j) if (g[i][j] == 0) dfs(g, i, j), ++regions; return regions;
I don't like one line code. What does the code execute in the if()
?
I am confused by the "," sign.
Usually I would write it as:
for (auto i = 0; i < g.size(); ++i) { for (auto j = 0; j < g.size(); ++j) { if (g[i][j] == 0) { dfs(g, i, j) } ,++regions; // I am not sure what to do here. Inside the "if" scope?? } } return regions;
The comma operator ( , ) evaluates each of its operands (from left to right) and returns the value of the last operand. This lets you create a compound expression in which multiple expressions are evaluated, with the compound expression's final value being the value of the rightmost of its member expressions.
The comma operator (represented by the token, ) is a binary operator that evaluates its first operand and discards the result, it then evaluates the second operand and returns this value (and type).
The comma operator in c comes with the lowest precedence in the C language. The comma operator is basically a binary operator that initially operates the first available operand, discards the obtained result from it, evaluates the operands present after this, and then returns the result/value accordingly.
The comma operator evaluates both of its arguments in turn, throwing away the result, except for the last. The last evaluated expression determines the result of the entire expression. i<=8,i++ - here the value of the expression is the value of i++ , which is the value of i before being incremented.
The programmer has used the comma operator to provide two unrelated expressions in a single statement. Because it's a single statement, both expressions are "inside" the if
condition.
It's a poor hack, which would be better done with actual {}
braces surrounding two statements.
Your example is not equivalent; it should be:
if (g[i][j] == 0) { dfs(g, i, j); ++regions; }
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