-1

Possible Duplicate:
Post Increment and Pre Increment concept?

I cant understand how the "if condition" works with the increment/decrement operator in this scenario:

#include<stdio.h> 
void main() 
{ 
  int n=0; 
  if(n++) 
  { 
    printf("C-DAC"); 
  } 
  else if(n--) 
  { 
    printf("ACTS"); 
  } 
}

Its output is ACTS.

what is happening in the IF condition?

Community
  • 1
  • 1
Anvay
  • 335
  • 1
  • 2
  • 11

10 Answers10

24

if (n++) It checks if n is not equal to zero and then increments n

else if (n--) It checks if n is not equal to zero and then decrements n

Your first if statement is not true (because n is zero), then n is incremented, and else if statement is checked (n is equal to 1 at this point), if (1) is true and printf("ACTS") is called

tomlogic
  • 11,489
  • 3
  • 33
  • 59
Ribtoks
  • 6,634
  • 1
  • 25
  • 37
  • I am sorry, but it is not specified anywhere in the program that it has to check whether n!=0... how does it understand this ?? – Anvay Jun 15 '12 at 11:57
  • Statement `if (n)` in `c++` is equal to `if (n != 0)` statement – Ribtoks Jun 15 '12 at 11:57
  • 1
    There is actually no if condition at all, at least not ouside a debug build. As the program is written, the compiler can trivially prove at compile time that the first test is always false and the second test is always true, and the variable is not otherwise used. Thus it will reduce the program to `int main() { printf(ACTS"); return 0;}` – Damon Jun 15 '12 at 12:01
  • @Damon true, but we're talking about c++ itself, but not about compiler optimizations – Ribtoks Jun 15 '12 at 12:02
  • @Anvay If Ribtok's answer solved your problem you should [**accept it**](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work/5235#5235) by clicking the checkmark next to this answer. This will give you both some rep points and also mark this problem as solved – Levon Jun 15 '12 at 12:16
  • Thanx @Levon .. I didn't know this..!!! Accepted his answer now..! – Anvay Jun 15 '12 at 12:22
3

Both n++ and n-- are post-increment/decrement operations.

That means the value of the variable is used for evaluation in the current expression first and only then changed, (i.e., incremented/decremented afterward).

0 means FALSE in a Boolean expression. Since n is initially 0, the test in your first if fails and the else clause is evaluated.

At that point n has become 1 due to the n++ in the first test, so this results in this test becoming true and

printf("ACTS"); 

to be executed.

Levon
  • 138,105
  • 33
  • 200
  • 191
3

if(0++) ==> if(0) else if(1--) ==> else if(1)

so ACTS is printed

Elavarasan
  • 186
  • 1
  • 6
2
int n=0; 
if(n++)

Meaning: check and then increment by 1.

int n=0; 
if(++n)

Meaning: increment by 1 and then check.

Dzha
  • 71
  • 2
2

An int always gives true, unless it is zero. The postincrement operator, i++, increments the i variable after the check. If you had used the preincrement operator, ++i, the incrementation would take place before the check.

EvenLisle
  • 4,672
  • 3
  • 24
  • 47
2

while checking your IF part, value of n is '0'. And because of the i++ , 'n' increments by 1. So, now the value of n is 1. So, while entering ELSE part, as value of n is 1 , it executes ELSE part. And you are getting the output of the ELSE part.

Tirumudi
  • 423
  • 1
  • 4
  • 18
1

It first checks the condition that n != 0 and after that it will increment the value of n

mathematician1975
  • 21,161
  • 6
  • 59
  • 101
1

The ++ and -- operator not only modify the value, they also have a return value. Behaviour is different depending on the position: ++var increases and returns the increased value, whereas var++ increases but returns the old value.

mensi
  • 9,580
  • 2
  • 34
  • 43
1

because i is 0. ++i would increment before evaluation, i++ increments after

SetSlapShot
  • 1,298
  • 1
  • 21
  • 48
0

The compiler reads the command in the left to right fashion. Meaning first it checks if the condition is true of false

if (n) 

and then increments/decrements

 n = n +/- 1
Zain Khan
  • 3,753
  • 3
  • 31
  • 54