Mxyk
Mxyk

Reputation: 10698

Behavior of a Semicolon after for Loop Declaration

Most for loops have the syntax:

for(initializer; condition; incrementer) {
    // code
    // code
}

If theres only one line of code, it may follow this syntax:

for(initializer; condition; incrementer)
    // code

Or

for(initializer; condition; incrementer) // code

So, my question is, how does this,

for(initializer; condition; incrementer)
    ;

Or this,

for(initializer; condition; incrementer);

behave? ; is a valid statement in many programming languages. So, does ; at the end of the for loop signify that the loop should keep looping with no statements to execute, or is the ; considered the statement to execute and loops this ; statement until the loop terminates?

Upvotes: 1

Views: 3750

Answers (1)

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

In C-like languages (really the only place this makes sense), your second description is the correct one: the empty statement is executed as the loop body.

Upvotes: 2

Related Questions