I have a question about how to go through the even rows of a matrix, I raised it like this:
for(int i=2;i<=get_m();i+2)
My question is:
In the row increment part, can it be done i+2
to go through only the even rows?
I have a question about how to go through the even rows of a matrix, I raised it like this:
for(int i=2;i<=get_m();i+2)
My question is:
In the row increment part, can it be done i+2
to go through only the even rows?
increment operator
Remember that there is an increment operator (++) and also a decrement operator (--), which are in charge of increasing/decreasing a unit to the variable on which they apply.
That is, if you do this:
The value of
i
will be 1 , but it is not necessary to reassign that value toi
, that is, it is the same as if you did:Therefore, in loops it is common to use this operator, since the increment is automatically assigned to the variable that is compared in the condition.
Code
Explanation
If you want a loop to run through every even element, just do this in your loop:
Or also:
In your case:
What it would do is add
i + 2
but its result would not be assigned to any variable, therefore it would not go from 2 to 2.online example
You can also put it in this abbreviated form:
The counter will start from 0 and the variable i will increase by 2 by 2