A trap of switch
pattern
#8124
Replies: 4 comments 2 replies
-
I'd imagine it'd be |
Beta Was this translation helpful? Give feedback.
-
The result is Seems to be an issue of precedence, |
Beta Was this translation helpful? Give feedback.
-
You should treat int c = 3;
int x = c % (3 switch
{
0 => c / 3,
1 => (c - 4) / 3 + 2,
_ => (c - 2) / 3 + 1,
});
Therefore the value is 0 ( There's a potential rule for operators: The precedence of binary expressions is lower than unary expressions, which means you should firstly calculate for the result of an unary, and then binary if they are mixed without braces. From this rule, the expression won't be considered as
|
Beta Was this translation helpful? Give feedback.
-
Switch will work first *because the precedence of unary expressions is higher than binary. |
Beta Was this translation helpful? Give feedback.
-
Without debugging, tell me the result of
x
.Beta Was this translation helpful? Give feedback.
All reactions