Doubt about matrix travel


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

2 answers


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;

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.



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

#include <iostream>

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

    int i;

        std::cout << i << std::endl;

    return 0;   


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


Or also:


In your case:

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

answered by 13.08.2017 / 14:40

you can also put it in this abbreviated form:

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

The counter will start from 0 and the variable i will increase from 2 to 2

answered by 13.08.2017 в 15:25