Infinite for-loop :: Engine bug or my code Issue? C++

Hi, I want to know why this simple for loop is infinite?
Tested the code with Engine version 4.20 and 4.26, both execute it as infinite loop.

for (int32 i = 1; i <= 10; i++)
	{
		print100s("Loop Executed!");
	}

Full code of the function where that loop is called

void AMyGameMode::RandomItemID(EItemType ItemType, FST_ItemTypeAndID &Item)
{
    FST_ItemTypeAndID TempItemTypeAndID_var{};
	ItemType = TempItemTypeAndID_var.Type = EItemType::E_Weapon;

	switch (ItemType)
	{
		case EItemType::E_Weapon:
			{
				if (const UDataTable *DT_ItamWeaponRows = LoadObject<UDataTable> (GetWorld(), TEXT("/Game/DataTables/DT_ItamWeapon")))
				{
					OutRowNamesWeapons = DT_ItamWeaponRows->GetRowNames();
					for (auto &WeaponArrayElem: OutRowNamesWeapons)
					{
						if (const UDataTable *DT_ItamWeapon
							{
								LoadObject<UDataTable> (GetWorld(), TEXT("/Game/DataTables/DT_ItamWeapon")) })
						{
							if (const FST_ItemWeapon *OutRow
								{
									DT_ItamWeapon->FindRow<FST_ItemWeapon> (FName(WeaponArrayElem), "") })
							{
								if (OutRow)
								{
									for (int32 i = 1; i <= 10; i++)
									{
										print100s("Loop Executed!");
									}
								}
							}
						}
					}
				}
			}

			break;
		default:
			{
				print100s("Default State-");
			}

			break;
	}
}

commenting all if statements and running the program gives correct results.
image_2022-08-08_201209336

heya alexa, this problem is because of the nested for loops, you are calling this for loop in other for loops.
call this for loop standalone and it will fix your issue.

hope it helps
cheers!

1 Like

thank you sir very very much for helping in my studies :slight_smile: , you just solved my problem