Increment rule inconsistencies | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

Increment rule inconsistencies

Problem 1 What is the output of this code? var x = 9; var y = x++; alert (++x * y); Sololearn says the answer is 99. I got 90 because I was under the impression that in the 2nd line, y = 9 and x = 9 since x++ is a post fix increment. So by the 3rd line (++x made 10 for me since the value didn't change from the 2nd line). --------- Problem 2 What will be alerted? n = 2; m = (n++ * 12); alert(m); Sololearn says the answer is 24. The increment rules are inconsistent with the answers to both these questions. How come in the first question we add the +1 to get 10 in the second line then add +1 again to get 11, but in the next question we don't add the +1, even though in both questions they're both post fix increments ? I hope I'm not confusing anyone. But I'll try to explain clearer if it's too confusing.

3rd Nov 2019, 1:03 AM
Zone
Zone - avatar
10 Answers
+ 11
var x = 9; var y = x++; alert (++x * y); Line 1: assign 9 to x. Line 2: postfix increment assigns the value of x to y, then increment x :- y=9 and x = 10. Line 3: Here there is another operation on x which is prefix increment which brings the value of x to 11 and then multipled to y which is 9 from line 2. :- ans = 11 * 9 = 99.
3rd Nov 2019, 8:41 AM
Meny Evolving
Meny Evolving - avatar
+ 9
V F happy to help
3rd Nov 2019, 10:40 AM
Meny Evolving
Meny Evolving - avatar
+ 5
y=x++ means y will be 9. x is raised after the deed to 10. ++x means, that x is raised again, this time before the deed. So it's 11*9. So in line 2, x is still 9. In line 3 it has been changed to 10, but before the calculation, it is changed once more. n++*12 means that n is raised after the calculation. So m is 24 now, and after that, n becomes 3.
3rd Nov 2019, 1:12 AM
HonFu
HonFu - avatar
+ 5
Zone , n is incremented after its value is used in the statement, system will see your statement as n=2; m=n*12; //Value of n used is 2 n=n+1; //Now "n" is incremented Alert(m); //2*12 =24 This is how a post increment operator works, value of n is incremented after tha statement gets finished if you would have used pre increment the it would have incremented and then used in the statement.
3rd Nov 2019, 1:21 AM
Arsenic
Arsenic - avatar
+ 3
Arsenic I'm aware that n is Incremented to 3 in the second problem. But if that's the case, why isn't the answer 36 instead of 24?
3rd Nov 2019, 1:15 AM
Zone
Zone - avatar
+ 3
Meny Evolving clear explanation! Thanks!
3rd Nov 2019, 9:56 AM
V F
V F - avatar
+ 2
Zone I think you are not clear with the concept of post increment operator. In your first problem:- In second line first value of x(which is 9) is assigned to y and then x is incremented to 10. In second problem:- Again in second line, in the whole statement value of n used will be 2 and then at the end of the statement n is incremented to 3 Hope you got it if not then tell me, I will try to make it more clear
3rd Nov 2019, 1:12 AM
Arsenic
Arsenic - avatar
+ 2
Arsenic HonFu thanks I think I'm getting the hang of it now
3rd Nov 2019, 1:30 AM
Zone
Zone - avatar
0
How to change characteristic value of a variable in python
3rd Nov 2019, 2:34 PM
Pankaj Mishra
Pankaj Mishra - avatar
0
Meny Evolving So in line 2, if postfix increment, the value will assign first and then increment? Var x = 9; Then var y =x++; Or var 9 = 10; Then alert (++x * y); Or alert (11 * 9); So postfix increment
3rd Nov 2019, 3:52 PM
Tevin Evans
Tevin Evans - avatar