# How to not iterate on 0:values in an Array

• 01-09-2008
Coding
How to not iterate on 0:values in an Array
I will give an example of what I am doing. I use a code that look like this:

Code:

```std::vector<int> Integers(10);                                      //This is my declared Array for (int Calculate = 1; Calculate < 6; Calculate++)                //The for loop {       if Integers[Calculate] > 0             {           Do something       } }```
Now is my question this:
This for loop iterates on value from 1-5 with 1 in interval. So this meens 5 steps.

The Integers Array has in this case 5 positions with either a value of 0 or a value of 1 that I have stored from before.
As for the if-statement I am only interested of positions in the Array that are greater than 0, in this case positions that contain the number: 1.
An live example could look like this for the 5 positions in the Array.

0 1 1 0 0

In the example I am only interested to iterate on position 2 and 3 because the if statement wont execute on the 0:s anyway.

So what I am after is to "jump over" the 0 values. So instead of doing 5 steps in this case, only 2 will be made to increase speed...

Is this possible in any way to do ?
• 01-09-2008
laserlight
Quote:

So what I am after is to "jump over" the 0 values. So instead of doing 5 steps in this case, only 2 will be made to increase speed...
Not possible, since there is no way to know that a value is 0 without accessing it, and accessing it means iterating over it.
• 01-09-2008
bithub
Also note that your loop is missing indexes of your vector. It should look more like:
Code:

```std::vector<int> Integers(10);                                      //This is my declared Array for (int Calculate = 0; Calculate < Integers.size(); Calculate++)                //The for loop {       if Integers[Calculate] > 0             {           Do something       } }```
• 01-09-2008
Coding
okay I understand.. I was afraid that you had to iterate over it in order to know if there is a 0 value...