Remember how a for
loop typically works:
for ( init-expressionopt ; test-expressionopt ; update-expressionopt )
statement
init-expression
(if present) is evaluated exactly once; it (typically) sets up the thing we’re testing against. In this case, it initializes the value the of ch
by calling getche
.
Before each loop iteration, test-expression
(if present) is evaluated; if zero, the loop exits. In this case, the test is ch != 'q'
.
After each loop iteration, update-expression
(if present) is evaluated; it updates the thing we’re testing against. In this case, it updates the value of ch
by executing another getche
call. Without this step, the value of ch
would not change and the loop would execute indefinitely.