IPB

Welcome Guest ( Log In | Register )

> Problems With The For Routine
pnegrini
post Dec 14 2007, 09:07 PM
Post #1


Newbrie


Group: Members
Posts: 1
Joined: 27-November 07
Member No.: 3,916



Hi all,

I tried to used a simple routine look like:

for (i=0;i==10;1++) and it works ok.
But When I tried to used a
for (i=64;i==100;1++) the progrmam, when in debbuger mode just jump it.
Any one know what's the problem?
Some times the " for(i=10;i==20;i) donīt work fine. When I replace the "==" to "<" the progrma work very well !!

Any one??
update Picpack 3.0 Released
Go to the top of the page
 
+Quote Post
 
Start new topic
Replies
Orin
post Dec 15 2007, 05:58 PM
Post #2


Regular
*

Group: EstablishedMember
Posts: 55
Joined: 17-November 07
Member No.: 3,897



QUOTE (pnegrini @ Dec 14 2007, 01:07 PM) *
Hi all,

I tried to used a simple routine look like:

for (i=0;i==10;1++) and it works ok.
But When I tried to used a
for (i=64;i==100;1++) the progrmam, when in debbuger mode just jump it.
Any one know what's the problem?
Some times the " for(i=10;i==20;i) donīt work fine. When I replace the "==" to "<" the progrma work very well !!

Any one??


As has been pointed out, you should have i != 100 and i++. But in general, it's said to be safer to use "<" than "!=" in such a loop as it can prevent infinite loops if the "i" gets changed somewhere else in the loop.

If you KNOW that the loop is going to be executed at least once, you are better off using the following:

i = 0;
do { /* loop contents */ } while ( ++i != 10 );

Or if "i" is ONLY used as a counter, the following can be better still:

i = 10;
do { /* loop contents */ } while ( --i != 0 );

Both can produce shorter, faster code!

Orin.
Go to the top of the page
 
+Quote Post

Posts in this topic


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



Lo-Fi Version Time is now: 3rd September 2010 - 02:48 PM