Skip to main content

We write loops like:

 for(x = 0; x < 10; x++) 

The language could have been defined so that loops looked like:

 for(x = 0, x < 10, x++) 

However, think of the same loop implemented using a while loop.:

 x = 0; while(x < 10) { x++; } 

Notice that the x=0 and x++ are statements, ended by semicolons. They aren't expressions like you would have in aa function call. Semicolons are used to separate statements, and since two of the three elements in a for loop are statements, that's what is used there. A for loop is just a shortcut for such a while loop.

Additionally, the arguments don't really act like arguments to a function. The second and third are repeatedly evaluated. ItsIt's true they aren't a sequence, but they also aren't function arguments.

Also, the fact that you can use commas to have multiple statements in the for loop is actually something you can do outside the for loop.

x = 0, y= 3; 

is a perfectly valid statement even outside of a for loop. I don't know of any practical use outside the for loop though. But the point is that commas always subdivide statements, itsstatements; it's not a special feature of the for loop.

We write loops like:

 for(x = 0; x < 10; x++) 

The language could have been defined so that loops looked like:

 for(x = 0, x < 10, x++) 

However, think of the same loop implemented using a while loop.

 x = 0; while(x < 10) { x++; } 

Notice that the x=0 and x++ are statements, ended by semicolons. They aren't expressions like you would have in a function call. Semicolons are used to separate statements, and since two of the three elements in a for loop are statements, that's what is used there. A for loop is just a shortcut for such a while loop.

Additionally, the arguments don't really act like arguments to a function. The second and third are repeatedly evaluated. Its true they aren't a sequence, but they also aren't function arguments.

Also the fact that you can use commas to have multiple statements in the for loop is actually something you can do outside the for loop.

x = 0, y= 3; 

is a perfectly valid statement even outside of a for loop. I don't know of any practical use outside the for loop though. But the point is that commas always subdivide statements, its not a special feature of the for loop.

We write loops like:

 for(x = 0; x < 10; x++) 

The language could have been defined so that loops looked like:

 for(x = 0, x < 10, x++) 

However, think of the same loop implemented using a while loop:

 x = 0; while(x < 10) { x++; } 

Notice that the x=0 and x++ are statements, ended by semicolons. They aren't expressions like you would have in a function call. Semicolons are used to separate statements, and since two of the three elements in a for loop are statements, that's what is used there. A for loop is just a shortcut for such a while loop.

Additionally, the arguments don't really act like arguments to a function. The second and third are repeatedly evaluated. It's true they aren't a sequence, but they also aren't function arguments.

Also, the fact that you can use commas to have multiple statements in the for loop is actually something you can do outside the for loop.

x = 0, y= 3; 

is a perfectly valid statement even outside of a for loop. I don't know of any practical use outside the for loop though. But the point is that commas always subdivide statements; it's not a special feature of the for loop.

Source Link
Winston Ewert
  • 25.1k
  • 12
  • 76
  • 104

We write loops like:

 for(x = 0; x < 10; x++) 

The language could have been defined so that loops looked like:

 for(x = 0, x < 10, x++) 

However, think of the same loop implemented using a while loop.

 x = 0; while(x < 10) { x++; } 

Notice that the x=0 and x++ are statements, ended by semicolons. They aren't expressions like you would have in a function call. Semicolons are used to separate statements, and since two of the three elements in a for loop are statements, that's what is used there. A for loop is just a shortcut for such a while loop.

Additionally, the arguments don't really act like arguments to a function. The second and third are repeatedly evaluated. Its true they aren't a sequence, but they also aren't function arguments.

Also the fact that you can use commas to have multiple statements in the for loop is actually something you can do outside the for loop.

x = 0, y= 3; 

is a perfectly valid statement even outside of a for loop. I don't know of any practical use outside the for loop though. But the point is that commas always subdivide statements, its not a special feature of the for loop.