Matt
Matt

Reputation: 7249

for loop missing initialization

I've seen

for(;;)

and

for ( ; *s != '\0'; s++)

Why is it blank like that. Thanks.

Upvotes: 14

Views: 44420

Answers (6)

Vlasin
Vlasin

Reputation: 208

while(1) and while(true) are the same as for(;;)

Upvotes: 2

NullUserException
NullUserException

Reputation: 85458

The for statement works like:

for (initialization; test-condition; update)

And any or all of those three can be omitted (left blank). So:

  • for (;;) is an infinite loop1 equivalent to while (true) because there is no test condition. In fact, for (int i=0; ;i++) would also be an infinite loop1.

  • for ( ; *s != '\0'; s++) is a loop with no initialization. s will point to the beginning of (probably) a string and is incremented until it reaches the null character '\0' denoting end-of-string. This essentially means loop through all characters of the string s

1 The loop will still be interrupted if there's a break statement in the loop body, or a call to exit(), etc...

Upvotes: 32

Justin Ardini
Justin Ardini

Reputation: 9866

for(;;) is an infinite loop. It is effectively the exact same as while (true).

The reason this works is because when the middle condition in a for loop is empty, it is interpreted as always being true.

for ( ; *s != '\0'; s++) is used for reading strings character-by-character. This approach works because every C string ends with a null character (\0).

Upvotes: 5

AnT stands with Russia
AnT stands with Russia

Reputation: 320401

It is "blank like that" because the author of the code left it blank. The author did not want/need to do anything in the corresponding section of for statement, so it was left blank.

for (;;) is a statement that iterates indefinitely (unless it is interrupted from inside cycle body).

for ( ; *s != '\0'; s++) is a statement that does not need an initialization section, since everything necessary (like the initial value of s) was already initialized before that for statement.

Upvotes: 7

casablanca
casablanca

Reputation: 70701

The parts that are blank essentially do nothing. So for (;;) creates an infinite loop that does nothing at all, and never exits because there is no condition in the loop. Your second example:

for ( ; *s != '\0'; s++)

is just a normal loop without any initialization expression. This relies on the fact that s already has an initial value and just loops until it reaches the end of the string.

Upvotes: 2

Mawg
Mawg

Reputation: 40140

it means do forever

for (initial condition; increment; end condition); You can omit any of these

See http://en.wikipedia.org/wiki/For_loop

The three control expressions, separated by semicolons here, are from left to right the initializer expression, the loop test expression, and the counting expression. The initializer is evaluated exactly once right at the start of the loop. The loop test expression is evaluated at the beginning of each iteration through the loop, and determines when the loop should exit. Finally, the counting expression is evaluated at the end of each loop iteration - even if continue is called - and is usually responsible for altering the loop variable.

In most languages which provide this type of for loop, each of the three control loop expressions is optional. When omitted the loop test expression is taken to always be true, while the initializer and counting expressions are treated as no-ops when omitted. The semicolons in the syntax are sufficient to indicate the omission of one of the expressions.

Upvotes: 1

Related Questions