Why For loop in C runs infinitely when only 'equal to' operator is given in condition
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.
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?
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.
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.
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.
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:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.