hi there

im wondering about the difference between:

int nr;
if(!((nr--)%5)){...}
if(!((nr--)%5)){...}

and:

int nr;
if(!(--nr)%5)){...}
if(!(--nr)%5)){...}

is it right that in case 1 nr will be decremented after true/false comparison and in case 2 in advance?

assuming nr=123456;
in case 1 it would be true for second if.... and
in case 2 it would be true for first if?


im not too sure about the difference between --var and var--

--var is predecrement where var is decremented before being used
and var-- is postdecrement where var is decremented after being used.

hi there

im wondering about the difference between:

When you wrote the test program to output the values, were you able to see what happened?

--var computes the condition first and updates the variable afterwards

var-- updates the variable first and uses the updated value of var to compute the condition

--var computes the condition first and updates the variable afterwards

var-- updates the variable first and uses the updated value of var to compute the condition

the other way round

In the 1st case, the first if is executed
nr-- updates the variable first and uses the updated value of nr to compute the condition

Here is the explanation...

if(!((nr--)%5))

nr will be first decremented to (nr--) 123455 then 123455%5 is computed which is 0 and !(0) calculates out to be 1
Hence if (1) gets executed{ execute the statement}

the second computed with 123455 is first decremented to (nr--) 123454 which %5 is never a 0, so simply forget about this.

In the 2nd case,nr=123456, 2nd if is executed

--nr computes the condition first and updates the variable afterwords

Here is the explanation...

if(!(--nr)%5))

In this case the initial value of nr which is 123456 is used in the expression I.e 123456%5 is computed which is never a 0, so again the statement under this if never gets executed, once the expression is computed no matter it results out to be a true (1) or false (0), the value in nr in updated to 123455, after 123455%5 is computed the resultant is 0 and !(0) calculates out to be 1 and statement between the { and } of if gets executed. And nr is then ubdated to 123454

I hope this will be usefull.

In the 1st case, the first if is executed
nr-- updates the variable first and uses the updated value of nr to compute the condition

Here is the explanation...

Listen to irre. He's correct, you are not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.