0

How is that code works?!?! And what is the difference between +++count and count+++ ???

what is the output of this code? var count =0; for (i=0;i<3; i++){ for(j=3; j>0; j-- ){ ++count; } } alert(++count);

21st Apr 2017, 6:43 PM
Ionut Marian
Ionut Marian - avatar
2 odpowiedzi
+ 10
Question: Did you write the code correct? Shouldn't it be ++count instead of +++count? ++ operator is used for increment. When it is put before the variable, the variable value is incremented and then returned. When it is after the variable, the variable value is returned and then incremented: int x = 4; To inicialize y to 4 and then increment x do: int y = x++; // y will have value 4 and x will have value 5 To increment x and initialize y with 5 do: int y = ++x; // x will be incremented to 5 and then assigned to y which gets value 5 also. Hope that helps you analize the code.
21st Apr 2017, 11:34 PM
Ulisses Cruz
Ulisses Cruz - avatar
+ 1
You also don't need semicolons after i++ and j--. It should be like this: var count =0; for (i=0;i<3; i++){ for(j=3; j>0; j--){  ++count;   } } alert(++count)
21st Apr 2017, 11:55 PM
SamG