LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-20-2012, 01:21 PM   #1
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Rep: Reputation: Disabled
why , this for loop is executing infinite times


Code:
for ( ; ; )
why i am saying this because there is no initialization term and no condition and no increment !
 
Old 08-20-2012, 01:32 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.
 
Old 08-20-2012, 05:19 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
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:

Code:
for ( ; 1; ) ...
 
Old 08-20-2012, 09:04 PM   #4
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Sorry ( colucix & Nevemteve )sir , but i dont understand !
 
Old 08-20-2012, 10:21 PM   #5
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
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
Code:
while 1
So it runs forever.
 
1 members found this post helpful.
Old 08-21-2012, 03:58 AM   #6
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
thanks , ( suicidaleggroll )sir , but look at this ,

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

can you please tell me that , initialization portion is executed at once why ! because i know that only static elements are initialized at once !
 
Old 08-21-2012, 04:18 AM   #7
Celyr
Member
 
Registered: Mar 2012
Location: Italy
Distribution: Slackware+Debian
Posts: 321

Rep: Reputation: 81
Quote:
Originally Posted by tushar_pandey View Post
thanks , ( suicidaleggroll )sir , but look at this ,

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

can you please tell me that , initialization portion is executed at once why ! because i know that only static elements are initialized at once !
This is the same as this:

Code:
i=0;
while (i<5)
{
...

i++;
}

Last edited by Celyr; 08-21-2012 at 04:19 AM.
 
1 members found this post helpful.
Old 08-21-2012, 10:36 AM   #8
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> can you please tell me that , initialization portion is executed at once why ! because i know that only static elements are initialized at once !

This is another "initialization" it has nothing to do with static/automatic variable-initialization; don't mix these things.

Read this way:

Code:
for (expr1; expr2; expr3) { statements }
Equivalent to:

Code:
expr1; 
while (expr2) {
    statements
    expr3
}
 
1 members found this post helpful.
Old 08-22-2012, 01:59 AM   #9
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
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.
 
Old 08-22-2012, 02:02 AM   #10
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
Quote:
Originally Posted by tushar_pandey View Post
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.
 
Old 08-22-2012, 02:06 AM   #11
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Code:
int i = 0 ;
while ( i < 5 ) ;
{

}
and

Code:
int i = 0 ;
while ( i < 5 ) ;
{
       i++ ;
}
, why these two are ::: infinite !
 
Old 08-22-2012, 02:20 AM   #12
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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.
 
Old 08-22-2012, 02:31 AM   #13
tushar_pandey
Member
 
Registered: Jun 2012
Location: ghaziabad , delhi , india
Posts: 105

Original Poster
Rep: Reputation: Disabled
Ok , but still i am not getting your point , now see this ... i am putting your words in for loop !

Code:
for ( i = 0 ; i < 5 ; i ++ );
{
   statement 
}
// now here we have set ";" this , but why statements are not executing , after each and every condition satisfaction !

// because i have use ";" after , incrementing phase and each and every statements in for loop are processed after condition phase !
 
Old 08-22-2012, 02:36 AM   #14
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
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
 
1 members found this post helpful.
Old 08-22-2012, 04:44 AM   #15
NateJacobs
LQ Newbie
 
Registered: Aug 2012
Posts: 2

Rep: Reputation: 0
You not to use ";".Use so loop can't go to infinite.

for ( j = 0 ; j < 10 ; j ++ );
{
statement
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] infinite loop that isnt infinite? frieza Programming 2 10-27-2010 02:16 PM
infinite loop on fclose()? joe2748 Programming 10 03-05-2010 08:09 PM
Slackware 12.1: rc.inet1, dhcpcd and infinite lease times (bug?) El Nigromante Slackware 1 08-21-2008 10:20 AM
Infinite Loop ewt3y Programming 3 08-16-2005 09:48 AM
infinite loop beginner_84 Programming 5 08-15-2004 02:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration