JavaRush /Java Blog /Random EN /What is incrementing and decrementing
L2CCCP
Level 9

What is incrementing and decrementing

Published in the Random EN group
Many beginners don't know what increment and decrement are, but we're sure everyone has seen the use of " i++, ++i, i--or --i" in a loop for. So, a little clarification. What is incrementing and decrementing - 1

What is an increment?

An increment is an operation in many programming languages ​​that increases a variable. Most often, an increment means an increase in a variable by 1 unit. The reverse operation is called decrement. Decrement is a decrease in a variable. Most often also by one. In Java, increment and decrement are primarily unary operators that are applied to any numeric variable. Increment: indicated by two plus signs " ++" and increases the value of the variable by 1. Decrement: indicated by two minus signs " --" and decreases the value of the variable by 1. Example:
int x = 10, z = 10;
x++;
z--;

System.out.println("x: " + x + ", z: " + z);
As a result x, it will be incremented, and z- decremented. And in the console we will see: x: 11, z: 9 In fact, you can write like this:
int x = 10, z = 10;
x = x + 1;
z = z - 1;

System.out.println("x: " + x + ", z: " + z);
But why write unnecessary things if we need to increase or decrease a variable by only 1? It’s good that we’ve figured out what incrementing and decrementing are, but that’s not all. Increment and decrement have 2 forms - prefix and postfix . Oh my God, what is this, you thought?) Don’t be alarmed: everything is easy here too. Prefix , or Prefix form, is declared before the variable " ++xor --x", initially incrementing or decrementing the variable. Postfix , or Postfix form, is declared after a variable " x++or x--", incrementing or decrementing a variable after evaluation. If you paid attention to the word calculation, you did so for good reason, since both forms work the same if they are not used in calculations. Here's an example without calculations:
int x = 10, z = 10;
x++;
++z;

System.out.println("x: " + x + ", z: " + z);
As a result, both variables will be equal to 11. And here is an example with the calculation:
int x = 10, c = 10;
final int y = 13 - x++;
final int z = 13 - ++c;

System.out.println("y: " + y + ", z: " + z);
As you can see, both variables " xand c" are equal and the equations are similar, BUT different forms of writing the increment operator are used. As a result of the calculations, the variable y will be 3, but zit will be 2. You probably already guessed why, based on what was written above. If not, then let’s “chew” everything together. The postfix form will work after the calculations, which means the equation will work like this:
x = 10;

// Начало уравнения.
y = 13 - x; // С начала производим вычисление (x все еще 10)
x += 1; // Производим увеличение на 1
// Конец уравнения.
The prefix will work like this:
c = 10;

// Начало уравнения.
c += 1; // Производим увеличение на 1
z = 13 - c; // Производим вычисление (c уже 11)
// Конец уравнения.
Well, that's the whole story :) We hope this was useful and informative for you. Good luck in mastering Java ;) You can learn and practice using increment, decrement and other operators in Java in the Javarush course in the Java Multithreading quest:
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION