Break and While

Since a loop body might be a single statement (i.e. not a block), is it worthy to add new scope whenever we enter a loop body? Considring the next example where the program should be rejected.

int x;
void foo() {
       while (cond) 
              x = 3;
       break;
     /*^error*/
}

In the end, how you implement this is up to you.

However, I do agree that it might make sense to have your scope keep track of whether you are inside a loop or not.

1 Like