+ 1

I don't get it

So, I was in playground mode the other day and I was trying this code: int num = 1; for (x = 1; x < 6; x+= 3) { num*= x; } Console.WriteLine(x) the output was 4. Normal, right? So, I decided to sum 2 instead of 3. This code was like this: int num = 1; for (x = 1; x < 6; x+= 2) { num*= x; } Console.WriteLine(x) I thought "Oh, the output will be 5" but NO. The output is 15! My only question is why? Why is it 15? I don't get it, please help.

31st Dec 2016, 4:58 PM
ç« é­šç„Œă420
ç« é­šç„Œă420 - avatar
2 RĂ©ponses
+ 5
i am sure that you mean console.writeline(num) _______________________________ for first code : ______________ *) x=1 so x<6 then num=num*x=1*1 *)x=x+3=1+3=4 *)x=4 so x<6 then num=num*x=1*4=4 *)x=x+3=4+3=7 *)x not <6 then we go out of loop *)writing x give 7 but writing num will give 4 ___________________________________________ for second code: _________________ *) x=1 so x<6 then num=num*x=1*1 *)x=x+2=1+2=3 *)x=3 so x<6 then num=num*x=1*3=3 *)x=x+2=3+2=5 *)x=5 so x<6 then num=num*x=3*5=15 *)x=x+2=5+2=7 *)x not <6 then we go out of loop *)writing x give 7 but writing num will give 15 ___________________________________________ when you say a*=b you mean a after this operation=the value of a before this operation *the value of b and same thing for += but with + ___________________________________________
31st Dec 2016, 5:22 PM
Ihab Taleb
Ihab Taleb - avatar
+ 3
Yeah, it was WriteLine(num) sorry and thank you! Now I get it
31st Dec 2016, 5:45 PM
ç« é­šç„Œă420
ç« é­šç„Œă420 - avatar