Reputation: 269
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.
Upvotes: 2
Views: 107
Reputation: 18675
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);
Upvotes: 1
Reputation: 201447
I believe you're trying to run the loop again with the same i
(without decrementing it in the loop body). One option is to move the increment into the loop body and use a continue
like
for (int i = 0 ; i < 8 ; ) { // <-- remove the i++ from here
if (xxx){
YYY; // do something
// i--; // <-- don't need to subtract one from i
continue; // <-- will rerun the loop with the same i
}
i++; // <-- add it here.
}
Another option would be an else
like
for (int i = 0 ; i < 8 ; ) { // <-- remove the i++ from here
if (xxx){
YYY; // do something
// i--; // <-- don't need to subtract one from i
} else {
i++; // <-- add it here.
}
}
And then you don't need the continue;
Based on your newly updated post,
for (int i = 0 ; i < 8 ; i ++ ){
temp[i] = random(7); // <-- 0,6 (will never be 7).
}
If you want a gap, I suggest you still reduce the range and test like,
for (int i = 0 ; i < 8 ; i ++ ){
int r = random(8); // <-- assuming it's 8 exclusive,
temp[i] = (r != 7) ? r : 8; // <-- skip 7.
}
Upvotes: 0
Reputation: 129
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);
}
Upvotes: 0
Reputation: 193
Yea what I typically do is use a second index or counter variable like you've shown in your code.
count=0;
for(i=0,i<800,i++)
{
if(x)
{
//do something using count
//ex: array[count] = i+3*count;
count++;
}
}
I don't think your second loop is probably necessary, but it also isn't entirely clear what you are looking to do. Regardless, that's how you use a secondary counter inside a for loop.
Upvotes: 0