Is there a difference in implementing if and if - else in GetBullCows()

If it is Bull then Bull++ and skip to the next character. Execute Cow checking loop.
If it is Bull then Bull++ and skip to the next character. Else (not a Bull) execute Cow checking loop.
Does including ‘else’ affect the program at all?
Would it be better to write a function to check for Bulls and Cows separately?

[code]
FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
FBullCowCount Counts;

for (int32 GuessIndex{ 0 }; GuessIndex < Guess.Len(); GuessIndex++)
{
	if (Guess[GuessIndex] == HiddenWord[GuessIndex])	// Check Bulls
	{
		Counts.Bulls++;
		continue;
	}
	else// Check Cows
	{
		for (auto HiddenChar : HiddenWord)
		{
			if (Guess[GuessIndex] == HiddenChar)
			{
				Counts.Cows++;
				break;
			}
		}
	}
}

return Counts;

}

[code]

I have refactored the Cow check . ‘else if’ is now compelling.

[code]
FBullCowCount UBullCowCartridge::GetBullCows(const FString& Guess) const
{
FBullCowCount Counts;

for (int32 GuessIndex{ 0 }; GuessIndex < Guess.Len(); GuessIndex++)
{
	if (Guess[GuessIndex] == HiddenWord[GuessIndex])	// Check Bulls
	{
		Counts.Bulls++;
		continue;
	}
	else if (IsCow(HiddenWord, Guess[GuessIndex]))	// Check Cows 		
	{
		Counts.Cows++;
	}
}
return Counts;

}

[code]

Using HTML code tag is not accurate. What am I doing wrong?
I have just realised IsCow is a member function and has access to HiddenWord so no need to pass it into the function parameter.

@RanMan was having trouble with this as well…

to make code format on the forums type three backticks (`) the button below escape, and insert code and end it with three backticks

No, because of continue.

Well it might be better to do it like

for (int32 GuessIndex =  0; GuessIndex < Guess.Len(); GuessIndex++)
{
    for (int32 HWIndex =  0 ; HWIndex < Guess.Len(); HWIndex++)
    {
        if (Guess[GuessIndex] == HiddenWord[HWIndex])
        {
            if (GuessIndex == HWIndex) Counts.Bulls++;
            else Counts.Cows++;
            break;
        }
}
return Counts;

For some words this results in less comparisons. Don’t know how often it is or if that is significant enough to warrant this.

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

Privacy & Terms