Clarification on one of the fuction in TargetMover.cs example code

Firstly forgive me if this is too obvious and my basic unity/c# fundamentals seem lacking but I want to understand why there is a while(true) in this function in TargetMover.cs example code:

``````IEnumerator OptimizeFormationDestinations (List<IAstarAI> ais, List<Vector3> destinations) {

var alreadySwapped = new HashSet<(IAstarAI, IAstarAI)>();

const int IterationsPerFrame = 4;

// This guy over here what I don't understand
while (true) {
for (int i = 0; i < IterationsPerFrame; i++) {
var a = Random.Range(0, ais.Count);
var b = Random.Range(0, ais.Count);
if (a == b) continue;
if (b < a) Memory.Swap(ref a, ref b);
var aiA = ais[a];
var aiB = ais[b];

if ((MonoBehaviour)aiA == null) continue;
if ((MonoBehaviour)aiB == null) continue;

if (alreadySwapped.Contains((aiA, aiB))) continue;

var pA = aiA.position;
var pB = aiB.position;
var distA = (pA - destinations[a]).sqrMagnitude;
var distB = (pB - destinations[b]).sqrMagnitude;

var newDistA = (pA - destinations[b]).sqrMagnitude;
var newDistB = (pB - destinations[a]).sqrMagnitude;
var cost1 = distA + distB;
var cost2 = newDistA + newDistB;
if (cost2 < cost1 * 0.98f) {
// Swap the destinations
var tmp = destinations[a];
destinations[a] = destinations[b];
destinations[b] = tmp;

aiA.destination = destinations[a];
aiB.destination = destinations[b];

alreadySwapped.Add((aiA, aiB));
}
}
yield return null; // this is reach after the 4 iterations of the for loop and would immendiately break the while loop? if that the case why have the while loop
}
}
``````

also would setting aiA.destination to some destination automatically start the the agent with followerEntity to move to that destination until given a new path or he reaches there?

It is used to run the inner loop every frame. Note the `yield return null` statement. When using unity coroutines this essentially means “wait one frame”.