Our website uses cookies to enhance your browsing experience.
Accept
to the top

Webinar: Let's make a programming language. Lexer - 29.04

>
>
>
V7019. A variable is used as a...
menu mobile close menu
Additional information
toggle menu Contents

V7019. A variable is used as a counter for both inner and outer loops.

Apr 03 2026

The analyzer has detected that an outer loop counter is used as an inner loop counter, which can lead to program malfunction.

The example:

let i = 0, j = 0;

for (i = 0; i < 5; i++) {
  for (i = 0; i < 5; i++) { // <=
    matrix[i][j] = ....;
  }
}

The code fills a 5x5 matrix. However, due to a typo, the same variable is used as the loop counter in both the outer and inner loops. As a result, after the inner loop completes, the i variable becomes 5, causing the outer loop to terminate early, and only the first matrix column is filled.

The fixed code:

let i = 0, j = 0;

for (i = 0; i < 5; i++) {
  for (j = 0; j < 5; j++) { 
    matrix[i][j] = ....;
  }
}