user1292548
user1292548

Reputation: 33

Why is there a semi-colon right after this for loop?

I came across this code

for(; tail < len;tail++){
        str[tail] = 0;

Why is there a ";" right after the "for("?

When I took it out, it came up with a couple errors.

Upvotes: 2

Views: 2667

Answers (9)

abhi
abhi

Reputation: 347

for(initialize the variable; condition to variable;increment in variable);

for (;condition;increment);

implies there is no initialization here .

Upvotes: 0

ovais
ovais

Reputation: 369

This is because you have for loop Syntax

int tail;

for( tail = 0 ; tail < len; tail++ )
{
str[tail] = 0; }

The first parameter is the initializer so if you have initialized the variable then you have keep empty the initializer space and put the semicolon instead.

Upvotes: 0

Mark Byers
Mark Byers

Reputation: 838726

It means that there is no initialization (it has already been done on previous lines).

In general a for loop has the following syntax:

for (initialization; termination; increment) {
    statement(s)
}

All three expressions (initialization, termination and increment) are optional, but the semi-colons must be present. The code you have is equivalent to the following while loop:

while (tail < len) {
    str[tail] = 0;
    tail++;
}

It is also common to see for loops where all three expressions are missing:

for (;;) {
    // something
}

This is an infinite loop and equivalent to this:

while (true) {
    // something
}

Upvotes: 9

norbitheeviljester
norbitheeviljester

Reputation: 962

The for loop takes three arguments inside it's parenthesis:

the first is the initiation block, where you create variables that live only during the scope of the for loop (from opening to closing brackets or in your case for that one line after the for loop);

The second is the condition block, where you specify the condition under which the for loop should run

third is the post processing block, what should happen after every iteration in the for loop.

Those blocks are divided by semicolons and each and every one is optional.

normally you have

for(int i=0; i<10; i++);

but you can have

int i =0;
for(; i<10; i++);

you can even have a loop like this:

for(;;);

Upvotes: 1

casablanca
casablanca

Reputation: 70721

A for-loop has three sections:

for (initialization; condition; update)

of which any of the sections can be omitted.

In your example there is no initialization section (it is assumed that tail and len are already set to reasonable values).

Upvotes: 1

MadProgrammer
MadProgrammer

Reputation: 347314

A for-loop defined implies that the variable tail has previously been declared (and hopefully initalsed)

Upvotes: 0

Silviu Burcea
Silviu Burcea

Reputation: 5348

tail must be initialized somewhere above the for in your code. It is not a syntax error, it is just a for without initialization.

Upvotes: 0

FThompson
FThompson

Reputation: 28697

The syntax for a regular for loop is for (initialization; termination; increment). Because of this, all three components must be preset for the for loop to be valid and compile.

Upvotes: 1

Miljen Mikic
Miljen Mikic

Reputation: 15251

This means that you don't initialize anything. Usually, it's something like

for(i=1;i<something;i++)

In your case part i=1 (initialization) is omitted which is perfectly correct. However, there must be semicolon to separate initialization part and the condition part of the for loop.

Upvotes: 0

Related Questions