How to make the loop to get to the end and stop at the end?

This is what I’m trying to archive and it’s almost working fine :

using System.Collections; using System.Collections.Generic; using UnityEngine; using FIMSpace.FLook;  public class Test : MonoBehaviour {     public FLookAnimator lookAnimator;     public Transform[] targets;     public float switchingTime;     public bool switchingLoop = false;      private bool loopOnce = true;      // Start is called before the first frame update     private void Start()     {         StartCoroutine(SwitchTargetsPeriodically());     }      private void Update()     {      }      IEnumerator SwitchTargetsPeriodically()     {         for (int i = 0; true; i = (i + 1) % targets.Length)         {             if (i < targets.Length - 1 && switchingLoop)             {                 loopOnce = true;                  lookAnimator.ObjectToFollow = targets[i];             }              if (switchingLoop == false && loopOnce)             {                 if (i < targets.Length - 1)                 {                     lookAnimator.ObjectToFollow = targets[i];                   }                 else                 {                     loopOnce = false;                 }             }              yield return new WaitForSeconds(switchingTime);         }     } } 

The problem it’s looping the targets.Length – 1 and if I change it to targets.Length then it will not loop at all. but if I’m doing targets.Length – 1 it will loop between 3 targets but there is 4 targets.