C postincrement in condition

do {instructions...}
while (x--)

should first do the instructions, then check if x is not 0 and go on/or leave the loop according to result and only after all these operations decrement x.

But my compiler decrements first.

Who is wrong? Me or my compiler ;-)? Could You please explain me why?

Answers


What You are saying

should first do the instructions, then check if 'x' is not '0' and go on/or leave the loop according to result AND ONLY AFTER ALL THESE OPERATIONS DECREMENT 'x'

What should be

should first do the instructions, then check if 'x' is not '0', then DECREMENT 'x' and go on/or leave the loop according to result


Many expressions in C have a value and a side-effect.

For the expression x-- the value is the value x had before; and the side-effect is decrementing x's value.

So, if you start with x being 1, when the execution reaches that expression, it will yield the value 1 and set x to 0. The next time the expression is evaluated, the value is 0 and x is set to -1, ...


the flow is :

1) do instructions

2) check value of x

3.1) is x == 0? then do x-- (x becomes -1) and exit the loop

3.2) is x != 0? then do x-- and go to step 1)


Need Your Help

Using convertDateTime tag in DataTable component and every ajax requests re-creating Managed Bean

jsf-2 managed-bean date-conversion ajax-request

I'm using convertDateTime tag in DataTable component. Connected managed bean property to timezone attribute. In this situation, every ajax requests re-creating Managed Bean.

Output Console.WriteLine from WPF Windows Applications to actual console

c# wpf batch-file cmd

Background: I am struggling to add command line and batch processing capabilities to an existing WPF Windows Application. When I detect some options at startup I suppress the window from appearing,...