[SOLVED] why , this for loop is executing infinite times
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I'm not an active C programmer, but I remember a simple rule: if any of the three conditions in the for loop is empty, it is evaluated as true. Your example simply brings to an endless loop that doesn't involve any loop variable. Hope this helps.
initialization is optional, so is increment. Lack of condition most likely generates a compiler warning, but defaults to non-zero condition, as if it were:
A for loop without an initialization or increment is just a while loop. In this case, with no conditional the compiler is just setting the conditional to true. So your for loop is essentially just a
Sir , Nevemteve , now think about this , because when i am trying this , it is running to the infinite times !
Code:
initialization;
while (condition);
{
statements
increment
}
& sir , please think this thing also for for loop , because for ( ---- ; ---- ; ----- ); , in this why , statements are not executed , because after condition phase statements are processed and after that condition are checked !
Last edited by tushar_pandey; 08-22-2012 at 02:02 AM.
Sir , Nevemteve , now think about this , because when i am trying this , it is running to the infinite times !
Code:
initialization;
while (condition);
{
statements
increment
}
Without knowing the condition and increment it is impossible to know why (and if) it runs infinitely. If you set up a condition that is always true (may be because your increment is unable to make the condition false) the loop will run infinitely.
Because of the ; after the while command. In your case the command-block of the loop is ended directly after your while statement, the code-block after that is seen as being independent. Remove the ; and the loop will work as intended.
Keep in mind that your first loop will nonetheless run infinitely, since your condition will never be false.
The statements are not executed if you put a ; after the for. If you put a ; after for and while it is for the compiler the same as an invisible empty code-block. So your for command will do 5 times exactly nothing and after that your statement-block is executed one time.
In short:
Code:
for ( i = 0 ; i < 5 ; i ++ );
is the same as
Code:
for ( i = 0 ; i < 5 ; i ++ )
{}
So your example expands to
Code:
for ( i = 0 ; i < 5 ; i ++ )
{}
{
statement
}
Last edited by TobiSGD; 08-22-2012 at 02:38 AM.
Reason: added info
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.