# Help me understand how the loop and statements are working

explain me about the [ ] too…

//loop through all letters in the guess
int32 HiddenWordLength = MyHiddenWord.length();
for (int32 MHWChar = 0; MHWChar < HiddenWordLength; MHWChar++)
{
// compare letters against the hidden word
for (int32 GChar = 0; GChar < HiddenWordLength; GChar++)
{
// if they match
if (Guess[GChar] == MyHiddenWord[MHWChar])
{
if (MHWChar == GChar)
{
BullCowCount.Bulls++; // increment bulls
}
else
{
BullCowCount.Cows++; // increment cows
}
}

``````	}
}
return BullCowCount;
``````

}

To be specific I cannot understand how the if statement is working… lets say my hidden word in “amy” and when i enter my gues “any” how does the system know that the “a” in the start and “y” in the end match the hidden words… Doesn’t this happen at the backend that the whole of the word is compared with the whole of the other word… we didn’t even use any function for extraction of first single alphabet from the guess then compare it to the first single alphabet of the hidden word then the second and so on… THAT’S WHAT I CAN’T UNDERSTAND.

Uh, you do seem very confused…
You also ask about the [ ] operator, maybe that’s the key (damn! didn’t mean to make a pun there…). That operator used on strings allows you to extract characters at a given position. So for instance if your Guess is “any”, Guess[0] is ‘a’, Guess[1] is ‘n’, Guess[2] is ‘y’.
The double for-loop goes over all pairs of characters taken from the guess and the hidden word to do all the possible comparisons… So the first ‘if’ checks if the letters at the given positions match, and the second ‘if’ if they are indeed at the same position.
Still puzzled, or you can work it out now?

2 Likes

Ohhhhhh… Thanks man!!!

1 Like