Learning javascript. The textbook says:
i ++; // shorter record for i = i + 1
A shorter entry, as I understand it, is done for the following:
optimize (shorten) the code and make it clearer; it is logical that the shorter is equal to the full.
But I ran into a simple example (in the future we will call it Example # 1 ):
i = 0; while (i++ < 5) { alert(i); } And I decided to write it down, in an abbreviated form (after all, they are equal). I could not get:
i = 0; while ((i = i + 1) < 5) { alert(i); } Because of the parentheses, the priority changes (with the five, the comparison is already after +1) and the last alert produces a four. It is surprising that I can not make a complete record, where made abbreviated.
- I want to get the result, as in Example # 1 , but at the same time make a record not abbreviated, but full (i.e.
(i = i + 1)) - The postfix form and the prefix form, I know about them, they do not play a role here in my opinion. If this seems important to you, use them in your explanation.
- This option:
i = 0; while (i < 5) { i = i + 1; alert(i); } I also do not like it, it is necessary that the complete entry be in parentheses of the while, as well as abbreviated.
My question is:
Explain to me how it works. ! It is necessary to use a while loop as in my example, if the records are equal, you will have no difficulty writing it down. It is advisable that your explanation be reduced to the following:
i++; // более короткая запись для i = i + 1i++; // более короткая запись для i = i + 1This is not true, entries are not equal and ...i++; // более короткая запись для i = i + 1i++; // более короткая запись для i = i + 1Records are equal and your code (we read what result I want to get above) ...
PS This is not a duplicate question, I write because another user ru.stackoverflow, marked the question "as a possible duplicate." This question is well decorated and clearly stated, detailed answers were given. If in this question you find something familiar and close, it does not mean that it becomes a duplicate. It is better to read the question again, read and comment on it, rather than write a link to a topic that has already been raised here several times. This question reflects the desire to understand, it is understandable and is beneficial (the author certainly).
++i(I hope in JS also has such a record) closer toi = i + 1. / Author, one little tip for the future. At first, once you read the topic completely and only then (preferably, even after a little thought), you are taken for exercises. - avpwhile (++i < 5)andwhile ((i = i + 1) < 5)(at least in C) is the same (the cycle is executed 4 times), and both differ fromwhile (i++ < 5)( 5 times) - avp