JavaRush /Java Blog /Random EN /What is increment and decrement
L2CCCP
Level 9

What is increment and decrement

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

What is an increment?

An increment is an operation in many programming languages ​​that increments a variable. In most cases, an increment means an increase in a variable by 1 unit. The reverse operation is called a decrement. A decrement is a decrease in a variable. Most often also per unit. In Java, increment and decrement are primarily unary operators that apply to some 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 extra if we need to increase or decrease the variable by just 1? Well, what is both incrementing and decrementing figured out, but that's not all. Increment and decrement have 2 forms - prefix and postfix . Oh my God, what is this, you thought?) Do not be alarmed: everything is easy here too. Prefix , or Prefix form, is declared before a " ++xor --x" variable, and initially increments or decrements the variable. Postfix , or Postfix form, is declared after the variable " x++orx--", increments or decrements a variable after a calculation. If you paid attention to the word calculate, then you did it for good reason, since both forms work the same if they are not used in calculations. Here is 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 a 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 the increment operator are used. As a result of calculations, the variable y will be 3, but zit will be 2. You have probably already guessed why, based on what is written above. If not, then let's “chew” everything together. The postfix form will work after calculations, which means that 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 it was useful and informative for you. Good luck in learning Java ;) You can learn and practice using increment, decrement and other operators in Java on the Javarush course in the Java Multithreading quest:
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION