My IsLowerCase function with a break in the loop

I’ve used a temporary variable (bool returnValue) and a break in the loop.
Otherwise, the loop runs through even if only the first character is an uppercase.

Question : Would the temporary variable cost more in memory?

bool FBullCowGame::IsLowerCase(FString GuessWord) const
{
	bool returnValue = true;
	for (auto Character : GuessWord)
	{
		if (!islower(Character)) //if not a lower case letter
		{
			returnValue = false;
			break; //Break the loop in case it has been found once, optimization.
		}
	}
	return returnValue;
}

Your implementation is fine, although it could be made a little simpler by returning false directly where you assign the value of “returnValue” to false. On the other hand, what the extra temporary variable could cost is, at most, a negligible amount of memory (1 byte) and processing time (1 assignment), so it is not a problem at all to use it.

(Actually, the extra temporary variable might be kept in a processor register or even optimized away by the compiler, so it might even cost zero memory and zero processing time).

Privacy & Terms