What does incrementing mean?

Asked

Viewed 3,901 times

11

I wonder what the term means increase, and in what situation I should use it. I’m learning programming logic and I hear a lot about it, but I don’t understand exactly what it means.

  • 5

    Hi, Welcome to the site :) I suggest you check out the guides [about], [Ask] and [Answer], because it seems that you are starting on the left foot... A general check on [help] is also important.

  • 4

    Increment variables? Ex: x++; or x += 1;

  • 8

    Why the votes against? Certainly this question - in the way it is (can be edited and improved, if the OP wants) - should be closed as decontextualized, but it is not the case to vote against... (P.S. In the context of programming, "increment a variable" is the same as "add 1 to its value" - in general keeping the result in the variable itself in which the increment was applied. That’s what you’re asking?)

  • 1

    What would be the most appropriate tag for this? Java is not.

  • 1

    I do not consider this question as "too broad". The accepted answer shows this well.

  • It would be interesting if AP marked the answer that he identified as the solution to his question.

Show 1 more comment

7 answers

20

It looks like you’re starting to program or learning programming logic, right?

When I went through this phase, I heard quite a lot of the term increase studying loops where in a loop do up for example, a variable is incremented or decremented until a certain condition is met. In such cases:

Increase

As @mgibsonbr has already commented, it is the same as adding an amount 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 15, since 10 + 5 = 15.

Decrease

It is much like incrementing, but in this case subtract a value from the variable instead of summing.

x = 10;
x = (x - 5); //decrementar 5

In this example x becomes 5, because 10 - 5 = 5.

Note: Do not feel ashamed 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’ve helped.

  • 2

    My next question will be: Why is it that when I declare two variables (X and Y) and initialize them with different values, when the program runs X = Y; Y = X; their values do not change?

9

According to the dictionary, INCREMENT is:

  1. development
  2. augmentation
  3. act of growing up
  4. raising

In programming the meaning is the same, we will increase something already existing. And there is also the option to decrease something already existing, that is, decrease/remove.

Incremental and Decreasing Operators

The incremental and decreasing operators have the function of increasing or decreasing exactly by 1 the value of a variable. They can be pre- or post-incremental and pre- or post-decremental. See the concepts of each of them and a practical example to follow:

- Incremental (++):

Preincremental or prefix - It means that if the sign is placed before the variable, first the value 1 will be added to this variable, then continuing the resolution of the expression.

$x = 0;
$resultado = ++$x + 20;
echo $resultado; // o valor de 21

Incremental post or suffix - It means that if the sign is placed after the variable, the expression is solved first, be it addition, subtraction, multiplication or any other, and then the value 1 is added to the variable.

$x = 0;
$resultado = ($x++) + 20;
echo $resultado; // o valor de 20
echo $x // o valor é 1

- Decremental (--):

Preincremental or prefix - Means that if the sign is placed before the variable, first the value 1 will be subtracted for this variable, then continuing the resolution of the expression.

$x = 0;
$resultado = --$x + 20;
echo $resultado; // o valor de 19

Post-incremental or suffix - It means that if the sign is placed after the variable, the expression is solved first, whether it is addition, subtraction, multiplication or any other, and then subtract the value 1 from the variable.

$x = 0;
$resultado = ($x--) + 20;
echo $resultado; // o valor de 20
echo $x // o valor é -1

Source: What is INCREASE

8

Short answer

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

Because there is an increment operator

This term has become so common because it is a widely used operation, having even specific processor instructions intended for increment operation.

It is widely used, mainly 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 this way is a widely used code structure, and so requires maximum performance, so that the impact of the loop itself is minimal and affects little of the code that really matters, which is within the loop.

Almost all languages have an increment operator. C# is just one example, but the operator exists in several other languages: javascript, C, C++, java.

The operator ++

In the languages that use the operator ++, generally the language allows to use 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
    

5

Increment = Sum

Increment 5 to result > resultado = resultado + 5

Increment the result > resultado = resultado + 1

5

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 subtraction to addition envés.

Example:

test--;

3

  1. Increase

In a situation where you are learning to use the loop of repetition for, increment a number will be related to how many times you will operate a sum +1 in that variable.

Exemplifying with a very simple algorithm, in which a user "X" decides the number that will be incremented, and how many times it will be incremented:

int main(){
    
    int A; /* A vai ser o número que será incrementado */
    int B; /* B vai ser quantas vezes esse número que será incrementado */
    
    scanf ("%d", &A); /* Leitura do número */
    scanf ("%d", &B); /* Leitura da quantidade de vezes */
    
        for (int i = 0; i <= B; i++){
            
            printf ("\n%d", A);
            A++;
        }
    
    return 0; 
}

Note: This is not the best and most correct way to make an increment, but is a very simple example for understanding

  1. Decrease

It is the same situation of the increment, only the change occurs that instead of incrementing a number with +1, decrease with -1

Exemplifying with a very simple algorithm, in which a user "X" decides the number that will be decreased, and how many times it will be decreased:

int main(){
    
    int A; /* A vai ser o número que será decrementado*/
    int B; /* B vai ser quantas vezes esse número que será decrementado*/
    
    scanf ("%d", &A); /* Leitura do número */
    scanf ("%d", &B); /* Leitura da quantidade de vezes */
    
        for (int i = 0; i <= B; i++){
            
            printf ("\n%d", A);
            A--;
        }
    
    return 0; 
}

Note: It was only necessary to change the A++ for A--

1

The act of incrementing is: add one more value to the initial value, so the final value will 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++;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.