4

I have a question about how to go through the even rows of a matrix, I put it this way:

`````` for(int i=2;i<=get_m();i+2)
``````

My question is:

In the increment part of the row can `i+2` be made to go through only the even rows?

asked by AER 13.08.2017 в 14:28
source

5

## Increase operator

Remember that there is an increment operator (++) and also a decrement operator (-), which are responsible for increasing / decreasing a unit to the variable on which they apply.

That is, if you do this:

``````int i = 0;
i++;
``````

The value of `i` will be 1 , but it is not necessary to reassign that value to `i` , that is, it is the same as if you did:

``````int i = 0;
i = i + 1;
``````

Therefore in loops it is common to use this operator, since the increment is automatically assigned to the varible that is compared in the condition.

## Code

In this code we will print only the even numbers from 0 to 10.

``````#include <iostream>

int main(int argc, char **argv) {

int i;

for(i=0;i<10;i=i+2){
std::cout << i << std::endl;
}

return 0;
}
``````

## Explanation

If you want a loop to run from each even element, you just have to do this in your loop:

``````for(i=0;i<10;i=i+2)
``````

Or also:

``````for(i=0;i<10;i+=2)
``````

``````for(int i=2;i<=get_m();i+2)
``````

What I would do is add `i + 2` but the result would not be assigned to any variable, therefore I would not go through 2 in 2.

## Example online

``````for(int i=0;i<=get_m();i+=2)