0

2 - Explain the working of this code

int a = 8; for(int a = 4 ; a <8 ; a += 2) { cout << --a; } output : 3456

18th Jan 2019, 8:11 AM
Sm Developer
Sm Developer - avatar
12 Answers
+ 5
Here's an edited version of my previous answer, I will follow Gordon's style for writing the steps, because I see it is much more comprehensible : ) First iteration: a = 4 --a decrement a -> 3 then outputs 3 a += 2 adds 2 into a then a becomes 5 Second iteration: a = 5 --a decrement a -> 4 then outputs 4 a += 2 adds 2 into a then a becomes 6 Third iteration: a = 6 --a decrement a -> 5 then outputs 5 a += 2 adds 2 into a then a becomes 7 Fourth iteration: a = 7 --a decrement a -> 6 then outputs 6 a += 2 adds 2 into a then a becomes 8 And finally ... a = 8 The condition a < 8 no longer satisfies, and the loop ceases repetition. Thanks to Gordon for an excellent example of a way to describe the steps with better details 👍. Hth, cmiiw
18th Jan 2019, 2:28 PM
Ipang
+ 4
a = 4, loop while a < 8, increment by 2. Note the prefix decrement operator is being used within the loop. a = 4 outputs 3, add 2 -> 5, a = 5 outputs 4, add 2 -> 6 a = 6 outputs 5, add 2 -> 7 a = 7 outputs 6, add 2 -> 8 a = 8, loop terminates ... Hth, cmiiw
18th Jan 2019, 9:23 AM
Ipang
+ 4
Ipang means output is 3456, for example "a = 4 outputs 3, add 2 -> 5" means In first iteration, At beginning a = 4 --a so outputs 3 (without line break) After first iteration a+=2 so a becomes 5 You interpret his answer like this please
18th Jan 2019, 12:37 PM
Gordon
Gordon - avatar
+ 3
Gordon Not at all, I have seen inspiring people who gave kind and clear answers before, and didn't give up on explaining, and I think you are one : ) Well it is good then you passed him that lesson link, in that case 👍 Hth -> Hope that helps, and ... cmiiw -> correct me if I'm wrong ... I thought everybody knew : )
18th Jan 2019, 2:38 PM
Ipang
+ 3
Saad Mughal Great 👍 and you're welcome : )
19th Jan 2019, 6:07 AM
Ipang
+ 2
https://www.sololearn.com/learn/CPlusPlus/1610/ Read here about the difference between prefix and postfix And please share your understand here
18th Jan 2019, 12:41 PM
Gordon
Gordon - avatar
+ 2
Saad Mughal Sorry for late reply, just got home. Yes, Gordon explanation is right, I meant to write that way, apologies for unclear explanation. And if it wasn't clear enough please tell, we will try best to assist : )
18th Jan 2019, 1:49 PM
Ipang
+ 2
Ipang wow, you are super kind and super patient to type all these 👏👏👏 I thought he didn't understand the ++/-- he asked another similar question a moment after I answered this question. By the way, i am very curious What's hth, cmiiw?
18th Jan 2019, 2:32 PM
Gordon
Gordon - avatar
+ 2
Oh now I know 😂
18th Jan 2019, 2:40 PM
Gordon
Gordon - avatar
+ 1
Ipang the answer is 3456 not 5678 the a+=2 not working in the output please explain more
18th Jan 2019, 12:14 PM
Sm Developer
Sm Developer - avatar
+ 1
Oke
18th Jan 2019, 12:39 PM
Sm Developer
Sm Developer - avatar
+ 1
Thanks Ipang i just forget operators no problem now
18th Jan 2019, 8:58 PM
Sm Developer
Sm Developer - avatar