Question: Assumed question I don’t wanna 7 in the random number so I do this. I know this is not a good example. Sorry for that first.

```
int temp[8];

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(8);
    if (temp[i] == 7){
        temp[i] = random(8);
        i--;
    }
}

```

My ideal is don’t do i— where inside the for loop. Use extra space to record it then do the somehhing outside the first for loop. The code will become:

```
ArrayList i_index;
int count = 0 ;
int temp[8];

for (int i = 0 ; i < 8 ; i ++ ){
    temp[i] = random(8);
    if (temp[i] == 7){
        i_index.add(i) ;    
    }
}

for (int i = 0 ; i < 8 ; i ++ ){
    if ( i_index.get(count) == i ){
        temp[i] ++ ; //do something or whatever just not return 7 
        count ++ ;  
    }
}

```

Is that correct or is there any better ideal?
Or hope someone can give a good example.

Answer: 
I don't see anything wrong in manipulating the index like that. I do it my self very often.

For example lately I had to find items in an array that might come in pairs but not always so the loop looked similar to this (it's pseudocode):

```
for(int i = 0; i < itemCount; i++)
{
    item1 = items[i]
    if (i + 1 < itemCount)
    {
       item2 = items[i+1]
       if (item2 is a pair to item1)
       {
           ...do something
           i++ // item2 is already processed so skip it next time by increasing the i
       }
    }
}

```

If your algorithm requires it I would say it's totaly ok to manipulate the `i`. Sometimes there is no other way (like in my example).

---

EDIT: Now that you showed us the full case I still think it is ok in to change the `i` in your algorithm. However there are a few other solutions possible:

**Example-1**: you could use a `while-loop` inside the `for` loop

```
for (int i = 0 ; i < 8 ; i ++ )
{
    int num = random(8);
    while(num == 7)
    {
        num = random(8);
    }
    temp[i] = num;
}

```

**Example-2**: or the other way round by using a `do-while` inside the `for` loop

```
for (int i = 0 ; i < 8 ; i ++ )
{
    int num;
    do
    {
        num = random(8);
    } 
    while (num == 7);

    temp[i] = num;
}

```

**Example-3**: or just a single `do-while` loop until it finds 8 numbers different from 7:

```
int count = 0;
int num;

do
{
    num = random(8);
    if (num != 7)
    {
        temp[count] = num;
        count++;
    }
}
// Repeat as long as there are fewer then 8 items or the number is 7
while (count < 8 || num == 7);

```

**Example-4**: if you can use a collection other then an array, like a list or something where you can add items it can even be simpler:

```
int num;
bool isValidNumber;
do
{
    num = random(8);
    isValidNumber = (num != 7);
    if (isValidNumber)
    {
        numList.add(num);
    }
}
// Repeat as long as there are fewer then 8 items or the number is invalid
while (numList.count < 8 || !isValidNumber);

```
i would like to try in this way since not sure what language you going to use and what mean by XXX and YYY action.

Example in PHP

```
$arr=array();

    for($x=0;$x<8;$x++)
    {
        if(xxx)
        {
            array_push($arr,$x);
        }
    }

```

Now we have a array which matched with XXX condition, so we just do YYY action based on what it stored in array.

```
foreach($arr as $y)
{
    YYY($y);
}

```