Could someone help me with the c# code. I want to calculate new a value- the value is calculated in the way: a=a-2*b, than to see if the result is less than zero and if it is in the range (0,a). I am usually doing that in few steps, but I have found a code on the internet which looks much better than mine, and the explanation of the problem the code solves is like mine, but I am not sure if the code is written in the proper way or not, because it doesn't give me the correct result. Also, there is no reported error in code.
a = a - 2 * b < 0 ? 0 : a;
Is the code ok for the thing I need, or not?
(a - 2) * bora - (2 * b)you're trying to calculate? Because default operator precedence will cause the multiplication to occur first, then the subtraction. Also, what does your code look like?a=0, ifa - 2 * bis less than 0. if not, it sets it to a's valuea = Math.Max(0, a - 2*b);is IMO much more clear.