OK, so you need to understand the overall concept of programming. You have a main "loop()" whose task is to "get things done" by sequentially attending to all relevant matters. This means that it absolutely cannot tarry on any one matter and most certainly cannot wait at any point for for some particular thing to happen.
This specifically means that you cannot freely use loops within your code. "While" loops are particularly inappropriate as they imply waiting for some event. "For" loops are only appropriate if it is guaranteed that they will complete immediately (which is to say within a millisecond or so) without any need to wait.
This means that whatever may have been indexed in a loop as a dependency, simply needs to be kept as a global index (or "state") variable which is tested to determine what should happen in consequence and that variable may be advanced (only) as appropriate for successive passes through the loop().