In the first program, why does count gives 6 and in the second program, why 7 instead of 13? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 7

In the first program, why does count gives 6 and in the second program, why 7 instead of 13?

//first program var count = 0; for(var i=0; i<3; i++) { count--; for(var j=0; j<=2; j++) { count++; } } alert(count); //second program function func(a) { return a++ a+=5; } document.write(func(7));

8th Jul 2019, 6:26 AM
eMBee
eMBee - avatar
3 Answers
+ 7
First: If you think about it, the first loop runs three times, meaning count would be -3. But there's another for loop, which also executes thrice, meaning count will be added 3 times. If you do the math, which isn't that complicated: -3 + 3 * 3 = 6 Second: The return keyword stops executing the function, whatever is after it, is ignored. Meaning it will return 7++, which is still 7
8th Jul 2019, 6:32 AM
Airree
Airree - avatar
+ 4
Oh, thanks Airree for your answer, it helped
8th Jul 2019, 6:44 AM
eMBee
eMBee - avatar
+ 3
when first loop run inner loop will run 3 times which means when first loop run count-- is -1 (0-1) and inner loop increment count three times( -1 +3 = 2), first loop run second time count-- is (2-1=1) and inner loop increment count three times (1+3=4). first loop run third time count--(4-1=3) and inner loop increment count three times which is 3+3=6 and loop end //second program a++ mean a==a+1 postincrement (assign value first and add 1 is ignored after that) if it is ++a preincrement.
8th Jul 2019, 4:04 PM
Danielov
Danielov - avatar