박주희
박주희

Reputation: 25

Why ";" is not used for i++ in a for loop. Doesn't everything have to end with ";"?

Why not use ; after the i++ in a for loop?

Consider,

for (int i= 0; i < 50; i++) {

}

In this case why people don't add ; after i++?

Upvotes: 0

Views: 327

Answers (6)

Johan Segerlund
Johan Segerlund

Reputation: 21

This is how the for-loop is constructed which is the same in many other languages. The for-loop has 3 options which are seperated by ;. The first options is the initial statement where you usually initialize the counter but you can add other things here and seperate these with , instead. Here's an example:

(int i = 0, int k = 100; i < k; i++, k--)

And you don't have ; after i++ for the same reason you don't have it within methods parameters. For example you can't write while(true;)

Upvotes: 2

John Bollinger
John Bollinger

Reputation: 181008

Why “;” is not used for i++ in C language. Isn't everything to be end up “;”?

No, "everything" does not end with a semicolon. Most statements and all declarations end with a semicolon, but i++ is an expression. That is, it expresses an operation that produces a value. That it also has a side effect is not relevant to this analysis. Other expressions include i + 1, i > 0, i alone, and even i = 42, among innumerable others. None of these require a trailing semicolon in the general case, and all can be used as sub-expressions of more complex expressions.

C does have the concept of a "statement expression" wherein an expression can be used as a statement (for its side effects). In that case, the statement is terminated by a semicolon, just like many other kinds of statements. These are in fact very common indeed in C code, because assignments and function calls are expressions, and expression statements made from expressions of those types make up the bulk of most C codes. But statements must not be construed as "everything".

Why not use ; after the i++ in a for loop?

In the simplest possible terms, that's because the language requires that you not do so. With respect to my preceding comments, it's because the third clause of a for statement's control block requires an expression, not a statement.

Upvotes: 0

machine_1
machine_1

Reputation: 4454

There is a distinction between an expression and a statement. For example, the following is considered an expression:

i++

while the following is considered an expression statement:

i++;

Since the standard specifies that the syntax of a for loop is:

for ( expression(opt) ; expression(opt) ; expression(opt) ) statement

it would be a syntax error to add a semicolon ; after the third expression because that would make it a statement, but what is expected is an optional expression.

Note: opt stands for optional.

Upvotes: 2

John Bode
John Bode

Reputation: 123558

In this case why people don't add ; behind of i++?

Because the language syntax doesn't allow it:

for ( expressionopt ; expressionopt ; expressionopt ) statement

In the control section of a for loop, the ; acts as a separator between the three (optional) expressions, not as a statement terminator.

Upvotes: 3

zwol
zwol

Reputation: 140758

If you wrote i++ as an independent statement you would need to put ; after it.

void foo(int i)
{
    i++   // syntax error, missing ';'
}

But you don't need to put a semicolon after the third expression in a for loop header, because there is a close parenthesis to end it instead. That's a rule about for, not a rule about ++. No matter what you put in a for loop header, you put a close parenthesis after the third expression, and not a semicolon.

for (listnode *p = list_head; *p; p = p->next) // correct

Upvotes: 12

Patrick
Patrick

Reputation: 764

Because it's in a for loop. A for loop has the form:

for ( init_clause ; cond_expression ; iteration_expression ) loop_statement 

i++ is an expression. i++; is a statement.

Upvotes: 8

Related Questions