Question about For Loops and getting wonky results

Hello!

I’ve got a question about for loops specifically regarding initializing variables in different places and causing different results, even though I expected the same:

Using this nested for loop, I’ve initialized both integers in the first for loop:

for(int32 Index = 0, Comparison = Index +1; Index < Word.Len(); ++Index)
    {
        for (;Comparison < Word.Len(); Comparison ++)
        {
            if (Word[Index] == Word[Comparison])
            {
                 return false;
            }
        }
    }

The loop doesn’t find any repeating letters other than the first character in the array, shown here:

However, using THIS nested for loop, with the integers initialized within different for loops in this way:

for (int32 Index = 0; Index < Word.Len() -1; ++Index)
    {
        for (int32 Comparison = Index +1; Comparison < Word.Len(); Comparison++)
        {
            if (Word[Index] == Word[Comparison])
            {
                return false;
            }
        }
    }

Allows the code to run as intended:

My question is why does this happen? Shouldn’t initializing the variables wherever they may be still have the same result? I wonder if this has something to do with scope, which is a question I asked on the forums last week, and I’m concerned that maybe I haven’t fully grasped the idea of scope yet.

Thanks in advance for your replies!

It does.

When the outer loop ends, Comparison will go out of scope and then Index gets incremented and Comparison will be re-initialised to Index + 1

1st version for a 3 letter word go

0, 1
0, 2 -- end here as

1, 3 -- Comparison not reinitialised

With the second version with Comparison declared within the loop

0, 1
0, 2 -- Comparison goes out of scope
1, 2 -- end here as

2, 3 -- Comparison not < Length

Hmmm I apologize but I’m still not sure I fully understand. Shouldn’t it work the same was as the second version, since Comparison is dependent on the incremented Index in both cases? I feel like I’m missing a lot hahaha.

Where and when does Comparison = Index +1 happen?

In the first example, Comparison = Index + 1 happens in the first for loop, while in the second it happens in the second for loop.

When it happens though… I’m not sure actually, when does it happen?

On the initial execution of the loop. Hopefully an “aha” moment… Would it be useful for it to do that on each iteration?

for (int i = 0; i < 10; ++i)

What if int i = 0 happened on each iteration? Would it go anywhere?

Sorry for the late response been busy with stuff last few days!

What if int i = 0 happened on each iteration? Would it go anywhere?

Ehm no probably not becasue it would keep on getting initialized to 0, which is counter intuitive to what you’re looking to do, which is to increment int i by 1 right?

Does this mean that having it in the first for loop basically reverts Index to 0 and comparions index to 1 every time, regardless of the increment command??

No. My point is that that is done once.

for (int i = 0, j = i + 1;

This only happens when initially enterering the for loop.

for (int i = 0, j = i + 1; i < 3; ++i)
{
    for (; j < 3; ++j)
    {
    }
}

This would go

0, 1
0, 2 -- exit inner
1, 3 -- inner loop condition is false so doesn't execute
2, 3 -- inner loop condition is false so doesn't execute

Having the declaration of Comparison inside the inner loop would mean when the outer loop iterates Comparison will go out of scope as it was only local to the inner for loop. So it will be reinitialised to Index + 1

https://godbolt.org/z/84c8YT

Ooooh I think things are getting a little clearer. The second loop runs through the Comparison word until it reaches the final character of that word. Since Comparison isn’t out of scope, its not reinitialized to Index + 1, which is what you wnat it to do to be able to have the Comparison character compared with the Index character.

But why does the scope act in this way? My logic was that once it goes back to the first loop, it would reinitialize to Index + 1 since you’ve initialized it to do so, but clearly my logic is missing a few steps since that’s not what’s happening right? I’m sorry if I’m asking a silly question or a question you’ve already just answered but this is helping a lot!

You previously said the opposite

1 Like

Aaaaaah hahaha look man why you gotta call me out like that :stuck_out_tongue:

Ok so what you’re saying is in this example:

for (int i = 0; i < 10; ++i)

int i would never be reinitialized because that doesn’t make sense, so why would int32 Comparison be reinitialized. This whole time you were playing chess while I was playing checkers… I think maybe I saw Index + 1 and assumed there was a calculation being made there since it looked like a formula!

Thanks Dan I think I understand now, is my explanation of these things on the ball or am I still missing something? I feel like I got something wrong though…

Yes the part of the for loop syntax is for an init statement.

for (int i = 5 + 3;

Is also valid. Or even using variables like what is done.

Well that was kind of the point of asking you that question (though hoping you would see the contradiction yourself) :stuck_out_tongue:

1 Like

Awesome! Thanks again Dan, ppreciate the help!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms