IsIsogram in if statement - why does it print letters of a Word?

Hi,

First - thank you all for the great course.

Second, here is my definition of the IsIsogram:

bool UBullCowCartridge::IsIsogram(FString Word) const
{
for (int32 i=0; i<Word.Len(); i++)
{
PrintLine(TEXT("%c"), Word[i]);
}
return true;
}

And in different place the execution:

void UBullCowCartridge::ProcessGuess(FString Guess)
{
if (!IsIsogram(Guess))
{
PrintLine(TEXT(“No repeating letters, guess again”));
return;
}

Question is, why do i see loop printed letter of the Word in the terminal? I mean, it’s working like in the video, but i do not understand why. In ProcessGuess we are not executing IsIsogram itself, we only check if it returns true or false for a given argument. In my understanding result of this line:

    PrintLine(TEXT("%c"), Word[i]);

should not be visible in the terminal (because Its executed in if statement, but only to check if it returns true or false, in my understanding only “No repeating letters, guess again” should be printed), but it is. Hope I expressed my problem clearly :slight_smile:

So when you call it, it runs to check the value I believe. And in doing so, it does the code within (the printline %c). Now when it returns it’s always true, so the !IsIsogram is of course false (meaning it’s not not). And since it only wants to execute if it’s not true… it skips that. If I’m understanding you right. A quick way to check would be to return false, then you’d see the Printline No repeating activate because IsIsogram would be false, aka !. It’s a bit weird to put in human speech though. It’s a check if something is not true, and because it is true, it’s not going to activate when the condition is if not true. Hopefully I didn’t make that more confusing.

Hey, thanks for great answer.

I made my question more confusing than it was supposed to be, sorry. I understand the condition in if statement is not met, therefore “No repeating letters, guess again” is not being printed. When i wrote only “No repeating letters, guess again” should be printed i meant it assuming the conditions are met. However still not sure why PrintLine(TEXT("%c"), Word[i]); is being printed, a bit unexpected for me. but i guess it’s just the way how C++ works.

1 Like

Because that is what your function does. It can’t know whether the function returned true or false without executing it.

1 Like

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

Privacy & Terms