user1830323
user1830323

Reputation: 141

Effect of semicolon after 'for' loop

Say I want to print a message in C five times using a for loop. Why is it that if I add a semicolon after for loop like this:

for (i=0;i<5;i++);

the message does not get printed 5 times, but it does if I do not put the semicolon there?

Upvotes: 14

Views: 76837

Answers (8)

Abhishek Mane
Abhishek Mane

Reputation: 648

When for loop body contains only single statement then it is not necessary to use curly braces {} around the body of for loop.

for(i=0; i<5; i++)
{
    printf("Napoleon");
    printf("Bonaparte");
}
      
for(j=0; j<5; j++)
    printf("Napoleon");

So for(j=0; j<5; j++); evaluates itself as

for(j=0; j<5; j++)
    ;

So this is a for loop with single statement which is ; null statement. null statement does nothing. As 5 times loops run, null statement executes for 5 times but it does nothing as it's definition.

Upvotes: 0

It functions same as :

for(i=0;i<5;i++) {}

here it has nothing inside the braces and will simply increment the value of i to 5. You can check this using print statement in next line and get i=5.

`Compiled code ss here!!!

Upvotes: -1

pranay nallapaneni
pranay nallapaneni

Reputation: 11

#include <stdio.h>
    
int main() {
    int sum = 0; // variable initialization  
    int k = 1;  
    //for(int i = 1; i <= 10; i++);
    {
        sum = sum + k;  
        k++;
    }  
    printf("The  value of sum is %d", sum);  
    return 0;
}

if there is a semicolon after for loop then code in the curly braces is treated as regular body and will be executed normally.

In the above program I have commented the for loop and it does the same thing as putting ; at the end of the for loop. It prints the sum as 1 because code inside the curly braces executes once.

Upvotes: 0

Rahul Vashisth
Rahul Vashisth

Reputation: 81

Many compilers show a syntax error when you put a semicolon after a for loop but according to gcc compiler(Linux) or Dev-cpp you can put a semicolon after a for loop and it will not show you any errors.

For example

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

or

for(int i=0;i<=5;i++) 
{//blank body}

From the above example it is clear if we put blank braces or semicolon after for loop that means we haven't entered any variable yet.

Now come to your question.
If you want to print hello five times, you have to write your program as

for(int i=0;i<5;i++)
{
    printf("hello");
}

I hope you understand
cheers!!
Rahul Vashisth

Upvotes: 1

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

Semicolon is a legitimate statement called null statement * that means "do nothing". Since the for loop executes a single operation (which could be a block enclosed in {}) semicolon is treated as the body of the loop, resulting in the behavior that you observed.

The following code

 for (i=0;i<5;i++);
 {
     printf("hello\n");
 }

is interpreted as follows:

  • Repeat five times for (i=0;i<5;i++)
  • ... do nothing (semicolon)
  • Open a new scope for local variables {
  • ... Print "hello"
  • Close the scope }

As you can see, the operation that gets repeated is ;, not the printf.


* See K&R, section 1.5.2

Upvotes: 30

Sunil B N
Sunil B N

Reputation: 4225

This code below will print "Hello" 5 times..

    for(i=0;i<5,printf("Hello\n");i++);

Upvotes: 1

ouah
ouah

Reputation: 145899

The statement consisting of just the ; token is called the null statement and it does just... nothing.

For example, this is valid:

void foo(void)
{
     ;
     ;
     ;
} 

It can be used everywhere a statement can be used, for example in:

if (bla)
    ;
else
    ;

See the C Standard paragraph:

(C99, 6.8.3p3) "A null statement (consisting of just a semicolon) performs no operations."

Upvotes: 3

Prince John Wesley
Prince John Wesley

Reputation: 63698

for (i=0;i<5;i++);

is equivalent to

for (i=0;i<5;i++){}

Upvotes: 14

Related Questions