Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
C++
#include <iostream.h>
void main()
{

for(int n=1;n<4;n++);
cout<<"abc";



my question is that when i use semicolon at the end of loop then for loop execute body of loop only once. how for loop works in this case
Posted

Way too simple.

Semicolon means "empty statement". If you use it, the it becomes the body of the loop; so the loop repeats 4 times but does nothing. The second line goes out of the loop, so it is only executed once.

If you remove this semicolon, the body of the loop statement is your second line; it will be executed 4 times.

—SA
 
Share this answer
 
Comments
ridoy 22-Aug-13 1:45am    
exactly what i will say,my 5!
Sergey Alexandrovich Kryukov 22-Aug-13 2:07am    
Thank you.
—SA
Refer this link

Effect of Semicolon after 'for' loop[^]

It states that:
Quote:
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

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

is interpreted as follows:

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

If we look at your code
C#
for(int n=1;n<4;n++);//loop four times,do nothing.
cout<<"abc";//Executed once


Regards..:)
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 22-Aug-13 2:08am    
Sure, a 5.
—SA
Thanks7872 22-Aug-13 2:09am    
Thanks Sergey. :)
for the sake of understanding the for loop construct usually have following format

C#
for(<statement>;<condition>;<statement>;)
  <body statement>;
</statement></condition></statement>


Lets take a simple example, the below for loop will execute the body 4 times;

C#
for (int i=1;i < 5;i++)
  cout << i;


but semicolon at the end of for loop instruct that the said for loop does not have any body so will not execute the cout instruction as part of for loop

C#
for (int i=1;i < 5;i++)
  cout << "Test";


For loop parameter are optional for example this is valid for loop, the only difference is that it does not have declaration and increment statements;

C#
For (;i < 5;)
  cout << i++;


similarly this is also valid but it will be an infinite for loop

C#
for (;;)
  cout << i;
 
Share this answer
 
v2
Comments
Stefan_Lang 22-Aug-13 7:22am    
<quote>
for (int i=1;i < 5;i++);
cout << i;


Actually this will not compile as the scope of the variable i ends at the end of the first line. If you got an up-to-date compiler, it will issue an error "undefined symbol: i".
_Asif_ 23-Aug-13 2:12am    
My bad, corrected!
In this case abc is print only once because you use ; at the end of loop because cout<<"abc"; not body of loop actually .
 
Share this answer
 
for(int n=1;n<4;n++); means that there is no other statement inside the for loop. So, the loop will be execute 4 times but will print nothing or will do no processing. After the loop execution is finished, the statements following the for loop will be executed; that's why you get the "abc" printed only once. The reason is very simple that cout<<"abc"; is not part of for loop because for loop has finished without doing any processing or printing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900