# Thread: 'For loop' looping one too many times.

1. ## 'For loop' looping one too many times.

The issue occurs in the following procedure. I have included the entire procedure, as the contained for loop calls the procedure.

The first part of the procedure generates all sets of size 'size', of ints 1 to 9 (inclusive), but in ascending order (not necessarily 1, 2, 3..., could be 1, 7, 9). If you've played sudoku before, you might recognise this as part of a 'locked set' algorithm that.

Once a set of size n is generated, the locked set algorithm uses it.

The issue in more detail: the for loop has two test conditions, one for ensuring 'value' does not exceed 9, the second test condition harder to explain, not essential, but prevent unnecessary computation (same set being generated many times). For some reason, the for loop ends when value reaches 10 (shown when I print out contents of array).

If I run procedure with size = 9, a set of 1 to 10 inc. is generated, so for loop is looping one too many times.

Code:
void setScan(int size, int value, int *arrayPtr, int count)  // Size is size of wanted number set.
{
int remainSize = size;  // Stops loop 1 generating same set as other recursions.
int x, y;
if(size > 0)
{
while((value <= 9) && (value <= (10 - size)))  // This loop generates all sets of size 'size', and for each set, looks for a locked set.
{
if(value == 10);
printf("Hmm\n");
arrayPtr[count] = value;
remainSize--;  // Size signifies how much of set left to create.
value++;
count++;
setScan(remainSize, value, arrayPtr, count);
}
}
else  // size == 0, count == size of set
{
printf("Current set is: ");
for(y = 0; y <= count; y++)
printf("%i, ", arrayPtr[y]);
printf("\n");
/*for(y = 0; y <= 8; y++)
{
for(x = 0; x <= 8; x++)
{
//rowSetScan(x, y, arrayPtr, count);
//colSetScan(x, y, arrayPtr, count);
//boxSetScan(x, y, arrayPtr, count);
}
}*/
}
}

2. are you aware that in the array of size n availeble indexes are
0, 1, ..., n-1?

so standard form of the loop is
Code:
for(i =0; i < n;i++)

3. I am aware of the standard form. Initial value of loop is determined by 'value', passed in by procedure call. So, it is very possible for value to exceed 9, but loop should not begin/continue if value exceeds 9.

4. I think what vart actually meant was that you normall use < rather than <= to determine the limit of the loop.

And if there is NEITHER an initialization, nor a increment step in the loop, then I would rather use while than for. What I'm saying is that if two of the three units that make up a for-loop are empty, why is it a for-loop?

--
Mats

5. Although they are both good points, they don't address the problem. Still arises with a while loop which uses '<' over '<='.

6. Originally Posted by thealmightyone
Although they are both good points, they don't address the problem. Still arises with a while loop which uses '<' over '<='.
Sorry, but if you have modfied the code, can you please post the modified code. There may be other issues, but certainly using <= instead of < is a common reason for getting one too many, and your code shows that very pattern. Since your new code isn't identical to what is being posted, having the new code will help analyze the problem. If we start guessing what changes you have made, then we may well go wrong - not to mention that your new code may have CHANGED something else that is affecting the problem.

--
Mats

7. I have simply replaced the for with a while, but kept the '<=' as a personal preference. Code in first post updated.

8. Are you actually seeing it loop too many times, or saying that it does because it prints "hmm"?

Have another look at this:
Code:
if(value == 10);
printf("Hmm\n");
If you don't spot anything wrong, look again. Keep looking until you spot something wrong.

Edit: Or if you are using gcc, add -Wextra to your command line.

--
Mats

Popular pages Recent additions