Output C++

Output of the program :
//INPUT VALUE OF A =0
1
2
3
4
5
6
7
8

int a,b=3;
cin>>a;
if(a)
b=a++ -1;
cout<<a<<endl;
cout<<b<<endl;


ANSWER IS :
a=0
b=3

please tell me how it came. i'm unable to get the output
closed account (EwCjE3v7)
Nothing is wrong with the program

Presuming you give it the input 0, this happens

1
2
3
4
5
6
int a,b=3; // b is initialised to the value 3
cin>>a;   // we get input in to a, in this case its 0
if(a) // a == 0
  b=a++ -1; // this does not execute
cout<<a<<endl; // print out the value of a, 0
cout<<b<<endl; // print out the value of b, 3 


If you would like the if to execute when the value is 0, you can do this

1
2
3
4
5
6
int a,b=3;
cin>>a;
if(a == 0)
  b=a++ -1;
cout<<a<<endl;
cout<<b<<endl;
thanks a lot :)
appreciate it ..it helps
Topic archived. No new replies allowed.