Q:

What takes priority in a loop: For or While?

Let's say I have the following block of code:
x <- 0

while (x < 50){
  for (i in letters){
    print(i)
    x <- x + 10
  }
}

What is the order of operations? Does the While stop criteria apply even if there are more loops in the for loop?
I tested this and it ran through the the whole for loop, despite x reaching 250.
Is there a way to add a rule that says follow the while loop before the for loop?

A:

Your for loop is nested inside the while loop. Each time the while loop enters an iteration, control is handed over to its loop body which is the for loop in this case.The for loop then takes control and executes its iteration. After that, the for loop exits its control and hands over back to while loop. The while loop kind of decides the number of times the whole for loop executes.
To break a for loop when the criteria is broken or with any other logic, you can use a break statement inside an if block inside the loop body.

