I would like to know what the term increment means, and in what situation I should use it. I'm learning programming logic and I hear a lot about it, but I do not understand exactly what it means.
I would like to know what the term increment means, and in what situation I should use it. I'm learning programming logic and I hear a lot about it, but I do not understand exactly what it means.
It seems like you're starting to program or learn logic programming, right?
When I went through this phase, I overheard the term increment by studying repeat loops, where in a loop do for example, a variable is incremented or decremented until it meets a certain condition. In these cases:
Increase
As @mgibsonbr has commented, it is the same as adding a quantity to the current value of a variable, usually storing the result in itself (in the variable).
x = 10;
x = (x + 5); //incrementar 5
In this example x becomes worth 15, because 10 + 5 = 15.
Decrease
It is very similar to increment, but in this case a value of the variable is subtracted instead of adding.
x = 10;
x = (x - 5); //decrementar 5
In this example x becomes 5, because 10 - 5 = 5.
Note: Do not feel embarrassed to ask basic things, even if your questions receive several negative votes, because everyone went through this stage to get where they are today ... I hope I have helped.
According to the dictionary, INCREMENT is:
In programming the meaning is the same, we will increase something already existing. And there is also the option of decrementing something already existing, that is, decrease / withdraw.
Incremental and Decremental Operators
The incremental and decremental operators have the function of increasing or decreasing exactly the value of a variable by 1. They can be pre or post incremental and pre or post decremental. See the concepts of each of them and a practical example below:
- Incremental (++):
· Pre incremental or prefix - Means that if the signal is placed before the variable, first it will add the value 1 for this variable, then continue the resolution of the expression.
$x = 0;
$resultado = ++$x + 20;
echo $resultado; // o valor de 21
· Incremental post or suffix - Means that if the signal is placed after the variable, the expression is solved first, be it addition, subtraction, multiplication or any other, and then add the value 1 to the variable. / p>
$x = 0;
$resultado = ($x++) + 20;
echo $resultado; // o valor de 20
echo $x // o valor é 1
- Decremental (-):
· Pre incremental or prefix - Means that if the signal is placed before the variable, it will first subtract the value 1 for this variable, followed by the resolution of the expression.
$x = 0;
$resultado = --$x + 20;
echo $resultado; // o valor de 19
· Incremental post or suffix - means that if the signal is placed after the variable, the expression is solved first, be it addition, subtraction, multiplication or any other, then subtract the value 1 from the variable. / p>
$x = 0;
$resultado = ($x--) + 20;
echo $resultado; // o valor de 20
echo $x // o valor é -1
Source: What is INCREASE
Increment is a common term in programming, which refers to adding 1 to a variable, and storing the value in the variable itself.
It would be the same as doing so:
valor = valor + 1
This term has become so common because it is a much-used operation, even having specific processor-specific instructions for the incremental operation.
It is very used, especially in loops (example in C #):
for (var i = 0; i < 1000; i++)
{
}
Note that the increment operation will be used at least 1000 times in this loop.
It turns out that the loop made in this way is a heavily used code structure, and therefore requires maximum performance, in such a way that the impact of the loop itself is minimal and little affects the code that really matters, which is inside the tie.
Almost all languages have an increment operator. C # is just an example, but the operator exists in many other languages: javascript, C, C ++, java.
++
In languages that use the ++
operator, generally the language allows you to use it in two ways.
prefix ++valor
: The value to be returned by this expression is the value after the increment. That is:
valor = 10;
valor2 = ++valor; // valor2 será atribuido com o valor 11
suffix valor++
: The value to be returned by this expression is the value before the increment. That is:
valor = 10;
valor2 = valor++; // valor2 será atribuido com o valor 10
Increment = Add
Increase 5 to result > resultado = resultado + 5
Increase the result > resultado = resultado + 1
Increment is the term that defines the action of adding a new value to an existing one. In most languages by default the term defines the sum of an existing number with 1.
Example in javascript:
// A variavel test é definida com o valor 1.
var test = 1;
// A variável test é "incrementada" e passa a ter o valor 2.
// É o mesmo que:
// test = test + 1;
test++;
There is also the term decrement that is used to define the subtraction on the underside of addition.
Example:
test--;
The act of incrementing is: adding one more value to the initial value, so the final value is going to have one more value.
// A variavel valor é definida com 1.
var valor = 1;
// A variável valor é "incrementada" e passa a ter 2.
// valor = valor + 1;
valor++;