+ 6

Pls explain me this code..

#include <iostream> using namespace std; int main() { int c,j=0,n=0; for (c=1;c<=5;c++) { j=j++; } n=j+2; j=n; cout<<j; return 0; }

18th May 2017, 4:17 PM
Vignesh
Vignesh - avatar
13 Answers
+ 2
I was confused. But then I realize it's j++ not ++j
18th May 2017, 4:51 PM
Chandra Wibowo
Chandra Wibowo - avatar
+ 8
Everytime j=j++ gives the same value to j it means j=0 in every loop . After loop it is added by 2. Thus u get j=2. This is one quiz question.
18th May 2017, 5:04 PM
GOKULNATH MADHIVANAN
GOKULNATH MADHIVANAN - avatar
+ 5
Good question. Now,here if we could use the keyword "static" to declare j, then may be, as per definition, it would update "j" everytime the for loop revolve.
19th May 2017, 5:57 AM
JAMSHAID FAROOQ
JAMSHAID FAROOQ - avatar
+ 4
Thanku @mattias Ericsson now I understood
18th May 2017, 4:34 PM
Vignesh
Vignesh - avatar
+ 3
no the output is 2
18th May 2017, 4:28 PM
Vignesh
Vignesh - avatar
+ 3
that's why am confused
18th May 2017, 4:28 PM
Vignesh
Vignesh - avatar
+ 3
the key is to study j=j++;
18th May 2017, 4:30 PM
Mattias Eriksson
Mattias Eriksson - avatar
+ 3
no the answer is 2.. @vishnu ks
18th May 2017, 4:34 PM
Vignesh
Vignesh - avatar
+ 3
K @vishnu
18th May 2017, 4:35 PM
Vignesh
Vignesh - avatar
+ 3
yeah sure
18th May 2017, 4:36 PM
Vignesh
Vignesh - avatar
+ 3
done Vishnu
18th May 2017, 4:36 PM
Vignesh
Vignesh - avatar
+ 2
Ya @vignesh The output is 2. I got it.
18th May 2017, 4:34 PM
Vishnu ks
Vishnu ks - avatar
0
so easy 2
25th Oct 2017, 8:59 PM
Rami Alqanbar
Rami Alqanbar - avatar