Can anyone explain the ranged for loop?

i understand the gist of the code but can anyone explain in detail exactly what is happening within the for Loop. the conditional if statement is hard for me to wrap my head around.

thank you!

Hi Elijah,

I really struggled with this section but @DanM came along as usual and helped me understand it. I’ll try to break it down for you as best I can.

Above the For Loop we’ve created the definition of bool IsIsogram(), made an if statement that will make sure single-letter entries are returned as Isograms, and set up a Map named “LetterSeen” that will assign boolean values to each character it is given.

Now for the For Loop.

for (auto Letter : Word)

This line is creating a For Loop in which it is declaring a variable of type Char (“auto” detects that it is a Char) and naming it “Letter”. “:” here means “In” so the For Loop will check for each “Letter” in the Word (an FString).

This is how I read my range based for loops:

for (each member in collective)

In our case here, a letter is a member of a word. At least that helps me translate this concept in my mind.

Letter = to lower(Letter);

This line is making sure that each Letter (which we instantiated above as a Char) that passes through the For Loop will be set to lower case. “tolower” is a method available to us through “#include string” and it takes a Char like “Letter” as its argument and returns it in its lower case version.

if (LetterSeen[Letter]) { return false; }

This if statement is taking the map (you might also call it a “List” - that helped me understand this) and accessing it using the “[]” operator. Inside of “[]” you are providing a key, in this case a Char named “Letter”. “[]” Will look for the “Letter” in the “LetterSeen” list and if it finds it then it will tell “IsIsogram()” to return a false value (ie. If the same character has been used twice, we do not have an isogram).

else { LetterSeen[Letter]) = true; }

This else section of the If Statement will run if the previous line did not find the “Letter” (char) already assigned a value in the list. In this case, “[]” will assign a value to “Letter”. Here we tell it to assign the boolean value, “True”. Now when the for loop runs again, if it finds this same character already assigned this value of “True” in the LetterSeen list, it will return a false value for the “IsIsogram()” function and never make it back to this else statement.

{ return true;}

This line is our catchall in case any weird entries like “\0” are entered by munchkins playing the game.

I hope this information helps. Please let me know if you have any more questions or concerns. I’m very new to this as well but I’ll do my best to help you find the answers. If anyone more powerful than myself read this and found any incorrect information, please chime in as I’d like to be corrected!



Was in the middle of my explanation and you swooped in with a great explanation. Only thing I will add is a little bit more detail about the std::map. So a std::map uses key-value pairs and when you access it via [] you give it the key to look for and it will return the paired value.

Going through this for loop with “Hello” as the Word you will get.

//Letter = 'H', LetterSeen is empty
Letter = tolower(Letter); //Letter = 'h'
if(LetterSeen[Letter])  //'h' not in LetterSeen, gets inserted with default value of false
    return false;
//LetterSeen = {'h', false}
    LetterSeen[Letter] = true //LetterSeen['h'] = true.

Now going through those steps with the other letters, e gets added to LettersSeen then l then when the second l goes through LettersSeen['l'] will be true so false is returned from the function.


Thank you very much! The LetterSeen portion was what i was confused with! thank you for the help!

Wow, great reply! This is exactly what i needed, thank you for the help! Your explanation was clear and concise.
Much appreciated!

Thanks for this, it really helped me out!!


i would like to thank Flynn_Ringrose and DanM a lot for the explanation.
I tried to wrap my head around and could not understand how the map works.
Now i understand!

Hopefully this question makes sense because I’m really struggling here… How does this function get the players guess and perform the IsIsogram checks? I’m really struggling to see how everything is linked back to each other, if i purely look at the code for this challenge i cant see any reference to Guess?

Hi Trikz,

Sorry for the delay.

I can see why you might be confused as there is no sign of the word “Guess” in the IsIsogram() method. The key to understanding how this function is receiving the guess is back in Main.cpp.

There, inside of the GetValidGuess() implementation, you’ll see that whatever the user inputs is being labelled “Guess” by the std::getline tool and std::cin. From there, you can see that CheckGuessValidity() is being passed “Guess” as its parameter. When you follow that over to FBullCowGame.cpp to its implementation, you can see that it’s waiting to receive an FString, IE. “Guess”!

From there you can see that CheckGuessValidity() is passing “Guess” to IsIsogram() and waiting for IsIsogram() to judge the string. IsIsogram() is relabelling “Guess” to “Word” but it’s still an FString, and it’s still whatever the user typed in. When IsIsogram() finishes it will return a true or false value to CheckGuessValidity() which will help CheckGuessValidity() in evaluating the guess before it finishes and returns its final judgement back to GetValidGuess() in Main.cpp.

I hope this helps clear up your confusion. Please let me know if you have any other questions and I’ll do my best to help.


1 Like

Gosh… as always at first it looks so intimidating, but once you see an explanation like this and start looking at it, grasping each concept carefully over and over and processing the logic, it really starts to make sense. Thanks so much!

VB uses for each … in …
One more character, but makes it so much easier to understand.

Relabeling Guess to Word probably makes some sort of sense, but in this case it massively confused me. I also went into this not having an appreciation for the [] operator or its usefulness.

I’ve struggled quite a bit with the past few challenges (this is my first time programming in any language), and I’ve gotten to the point where I don’t spend too much time banging my head before looking at proper implementation and trying to work backward from that for a proper understanding.

For example; most map code samples I found point to hard-coded keys/values, and here we’re looking to build the map based on keys generated by player input, so the samples really just confused me further. All to say, in pseudo-code this was easy to factor but implementation stumped me.

This is very helpful. Thank you.

Privacy & Terms