LinuxQuestions.org
Review your favorite Linux distribution.
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 02-23-2009, 09:52 AM   #1
rmnature
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Rep: Reputation: 0
Why For loop in C runs infinitely when only 'equal to' operator is given in condition


Hi,

I am new to c-programming,i noticed that in the For loop whenever i give equal to in condition the loop runs infinitely but if i give <= the loop is ok.

example: for(i=1;i<=5;i++) print("a")

a is printed 5 times but if i give

for(i=1;i=5;i++) the loop runs infinitely.In this case shouldn't the loop exit after(or even before) first iteration?
 
Old 02-23-2009, 09:58 AM   #2
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
These two do not mean the same thing:
Code:
i==5   /* comparison */
i=5    /* assignment */
Taken as an expression, the first will be true (nonzero) only when, well, i==5.

Taken as an expression, the second will be true (nonzero) always, because that assignment itself is an expression and will always be 5.

Hope this helps.
 
Old 02-23-2009, 10:02 AM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
That's a very common error, but usually once someone sees that the problem is "=" they have the answer (usually people ask about that because they haven't yet narrowed the problem in their to the "=").

In C i=5 is always an assignment (it changes the value of i to be 5). The result is always 5. As a condition, any non zero result acts like true.

In C you must use i==5 to test whether i is equal to 5.
 
Old 02-23-2009, 10:11 AM   #4
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
This is because the syntax of a for loop is
Code:
for (initialization, condition, operation) {
some code here
}
this means that the variable is initialized, tested against the condition, and then the code between braces is executed. After the code execution has finished, an operation (for example "++", the increment) is performed on the variable, tested again, and if the condition is matched, the program enters another execution cycle. If you put an equal sign in the condition, the result is that the variable is re-initializated to 5 at every cycle and not matched against the condition: this brings to an infinite loop. If you try to put a double equal sign:
Code:
for (i=1; i==5; i++) {
you will see that the code within the for loop is not executed at all, since at the first test against the condition, the result is false.
 
Old 02-23-2009, 10:57 AM   #5
alan43
LQ Newbie
 
Registered: Jan 2009
Posts: 5

Rep: Reputation: 0
The value of the expression
Code:
i=5
is 5
so your codes become
Code:
for(i=1;5;i++)
which is an infinite loop, as 5 is treated as true.
 
Old 02-23-2009, 11:01 AM   #6
gergely89
Member
 
Registered: Feb 2009
Posts: 100

Rep: Reputation: 21
The
Quote:
for(i=1;i=5;i++)
especially i=5 will give i the value 5, and at the same resolve as a TRUE condition for the simple reason that i has become a non-null value. This way the looping condition is always true and never ends.

linux

Last edited by gergely89; 02-27-2009 at 10:50 PM. Reason: typo
 
Old 02-23-2009, 12:24 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by johnsfine View Post
That's a very common error, but usually once someone sees that the problem is "=" they have the answer (usually people ask about that because they haven't yet narrowed the problem in their to the "=").

In C i=5 is always an assignment (it changes the value of i to be 5). The result is always 5. As a condition, any non zero result acts like true.

In C you must use i==5 to test whether i is equal to 5.
There is an immortal deadly joke regarding the error:

Code:
if(EnemyMissilesLaunchDetected = 1)
  {
  CommenceCounterStrike();
  }
- I do not remember the exact words, but along the above lines.

In order to avoid such errors reverse the order of operands:

Code:
if(1 = EnemyMissilesLaunchDetected)
  {
  CommenceCounterStrike();
  }
- the compiler wouldn't allow assignment to constant.

I.e. meaning to compare with constant using '==' always put the constant to the left of '=='.
 
Old 02-24-2009, 08:28 AM   #8
rmnature
LQ Newbie
 
Registered: Feb 2009
Posts: 5

Original Poster
Rep: Reputation: 0
Thanks to all of you
 
  


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
Bash script - Infinite loop caused by a logical OR operator skuzye Programming 9 02-04-2009 01:31 PM
facing problem in increment operator of set :infinite LOOP ashwinipahuja Programming 0 06-03-2004 12:05 AM
need a 'how-to' on creating my own distro American Psycho Linux - General 10 04-13-2004 08:09 AM
not smart enough to find an alias 'how-to' rickenbacherus Linux - General 6 04-19-2003 10:20 PM
Peanut Linux 'how-to' Baracuda Linux - Distributions 4 08-26-2002 11:53 PM

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

All times are GMT -5. The time now is 09:16 AM.

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