Search code examples
language-agnostic

Why does the for loop use a semicolon?


In most C-derived languages (C, Java, Javascript, etc), the for loop is of the same basic syntax

for (int i = 0; i < 100; i++) {
    // code here
}

Why does this syntax contain semicolons, when semicolons are usually reserved for the end of the line? Also, why is there no semicolon after i++?


Solution

  • This pseudo-code:

    for (A; B; C) {
        D;
    }
    

    can be internally converted to

    { // scope bracket
        A;
        while (B) {
            D;
            C;
        }
    }