+ 2
I haven't understand increment and decrement operators completely. Anyone can give some examples to make me understand?
4 odpowiedzi
+ 2
++x means ADD 1 TO X ( THEN ) give me its value, BUT x++ means GIVE ME ITS CURRENT VALUE ( FIRST ) then increase it. Got it? Just look at what comes first (x or ++), and you will get the idea.
+ 2
дарова кодер
+ 1
Increment operator (++) increases the value by 1, decrement (--) decreases by 1.
For example, var i = 3.
You can make this var value 4 in this way: i++ ;
Also you can make i--; (where i=3) and make value of i = 2.
That was simple. Another example is loop, where this operators are often needed. Like in the attached code. (check JS and tap 'Run')
Also, there is a difference between the pre and post increment and decrement operators.
//wiki_code
int x;
int y;
// Increment operators
x = 1;
y = ++x; // x is now 2, y is also 2
y = x++; // x is now 3, y is 2
// Decrement operators
x = 3;
y = x--; // x is now 2, y is 3
y = --x; // x is now 1, y is also 1
https://code.sololearn.com/WsFui9z9XmR7/?ref=app
0
Increment operators is instead of putting x=x+1 you can simply put x=++1
Decrement operators is simply decreasing by one
Example x= --5 (so 5 is now 4 because it's been subtracted by one)
Note: (-- is minus one to the variable while ++ is additional 1 to the variable)