scrblnrd3
scrblnrd3

Reputation: 7426

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++?

Upvotes: 3

Views: 1602

Answers (7)

IceCold
IceCold

Reputation: 21194

It is just syntactic sugar. In the languages of the Pascal group there is no semicolon:

for i:= 0 to 99 do 

for i:= 99 downto 0 do

while i > 0 do 

And there are plenty of languages (outside the Pascal group) that does not use C-like syntax.


But while Pascal was designed with "easy to read" in mind, other languages were designed with efficiency in mind and forgot about syntax :)

There were times where even the program code itself could not fir in memory. I guess back then in the 80s when C lang was invented tried to minimize the size of the .c files.
Then everybody copied from it and the "issue" propagated. There are many issues in our languages that can be marked as "legacy". You really need to go read some stuff about the history of the prog languages. It is fascinating especially if you consider that they used 64KB of RAM and costed thousands while my PC has 64GB and was under $1000.

Also, there is a limited number of non-alpha numeric characters (only 128 total chars in ASCII) on the keyboard. They had to use something :) Consider yourself lucky that they haven't use `. Can you even find that on your keyboard? :)

Another interesting thing is that you might think that human-like syntax like the one shown above might be difficult to parse. But those with the experience in both C and Pascal (groups) will tell you the opposite.

Upvotes: 0

developer
developer

Reputation: 728

for is a language keyword/instruction - not to be confused with calls to library functions.

keyword/instructions are part of the language; if, while, do, for, etc. As such the compiler will need to generate assembly code (or instruction code of some kind) during compilation, interpretation. When the for instruction was being developed in the c language (by Brian Kernighan and Dennis Ritchie), they would have had to choose the syntax of the for operation, and how the operation was going to break down into assembler

From:

for( start-condition ; end-condition ; step-control )

to something like this:

mov eax, $x
beginning:
cmp eax, 0x0A
jg end
inc eax
jmp beginning
end:
mov $x, eax

this syntax was then used in C++, and other languages followed suite.

in c the semicolon is a line/command terminator. in other language it is a line separator. In the for loop it is a separator for the terms.

in c not all terms are required:

for(;;)

is valid code. and is equivalent to while(1) (pseudo: while(true))

in Pascal/modular2 the structure is different

for i:= start_value to end_value do

Upvotes: 1

lodo
lodo

Reputation: 2383

The general form is:

for ( expression; expression; expression ) { ... }

The parser can easily recognize these expression because their syntax is identical to the sintax of "normal" expressions:

{
    expression;
    expression;
    ...
}

The last expression can easily be recognized because it ends with a ')'. Furthermore, commas couldn't be used because they can be put inside single expressions:

for ( i=1,j=10; i<10,j>0; i++,j--) { ... }

Upvotes: 0

Geeky Guy
Geeky Guy

Reputation: 9399

The semicolon is not for ending lines. It's for ending instructions. In most of those languages you can do:

int i;i = 0;

And it's legal. Look for any minified Javascript code. You'll see thousands of semicolons per line.

By the same principle, a for block takes three instructions. They are separated by semicolons so that the compiler or interpreter knows where each command starts and ends.

This is perfectly legal (though it results in an infinite loop):

for (;;) {}

Upvotes: 2

Stefano Sanfilippo
Stefano Sanfilippo

Reputation: 33056

Probably there is no rational explanation for why this specific for(;;){} syntax was born. You should ask Kernighan or Ritchie about that.

Going back in the history of programming languages to the first '60s, the curly braces could have appeared in BCPL programming language, while the parantheses wrapping conditions where glorified by B (which only had a while statement, no for). C was modelled over B.

Since 1972, C has penetrated all sectors of computer engineering and subsequent languages where often modelled on C syntax (C++, Java, Javascript, C#, Scala, just to name a few) not to upset the habits of established programmers. This includes the for(;;) loop syntax and curly braces.

As a sidenote, there are many widespread languages not resorting to a C-style syntax, such as Python, whose for loop you may find more logic (obviuosly, this is a personal opionion), or Ruby.

Upvotes: 1

Chris Hutchinson
Chris Hutchinson

Reputation: 9212

I feel this can be answered sufficiently by examining how language parsers resolve the syntax. For example, a common depiction of a for loop is:

for (initialization; condition; increment-decrement) {
    /** statements **/
}

You can generalize that to:

for (expression; expression; expression) {
    /** statements **/
}

Note that the generalization is not entirely accurate, because the middle expression is typically reserved only for relational expressions, and the other two are either statements or statement lists. For example, in C and C++ you can have multiple statements in the initializer or increment-decrement regions by using the comma (,) operator.

It may help to note that statements are usually a collection of zero or more expressions, often separated by operators.

Why does the for loop syntax use semicolons?

In many languages, a semicolon doesn't occur at the end of a line of code, it typically occurs at the end of a statement. A statement is a often defined as the smallest standalone executable element of a piece of code. A common type of statement is an expression statement, which is a statement composed of exactly one expression. This helps to explain why there is a semicolon at the end of each expression in the for loop construct, because it is consistent with how the language parser interprets statements.

Why is there no semicolon at the end?

There is no semicolon at the end simply because that's how the language grammar is defined. The other components, as mentioned above, could be for consistency.

What about languages that don't use semicolons as mentioned?

This is difficult to answer, but I think a probable reason is that it's consistent with how it has been done in the past. C was/is a very popular language, and many languages base their syntax on some variant of C including C++, C#, Objective-C, Java, Python, Perl, and JavaScript.

Upvotes: 1

wchargin
wchargin

Reputation: 16047

This pseudo-code:

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

can be internally converted to

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

Upvotes: 11

Related Questions