What does the expression S+=i*(2*(i%2)-1) mean while writing a code in C?
I know that % 2 mean dividing something and the remainder is our answer but how about the whole thing?
What does the expression S+=i*(2*(i%2)-1) mean while writing a code in C? I know that % 2 mean dividing something and the remainder is our answer but how about the whole thing? |
|||
|
Questions on Programmers Stack Exchange are expected to relate to software development within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.
Lets assume the code is something like this :
S is call an accumulator, because each step you will take the last value and add to that the result of i*(2*(i%2)-1). The i*(2*(i%2)-1) is pretty simple :
So basically if the number is even you add -i, if the number is not even you add i to S. |
|||||||
|