Results 1 to 9 of 9
Sorry about another home work question but this is driving me bat looney.
any idea why a nested if statement in a for loop would cause the loop to terminate ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 12-06-2002 #1Just Joined!
- Join Date
- Oct 2002
- Location
- US
- Posts
- 7
For loop
Sorry about another home work question but this is driving me bat looney.
any idea why a nested if statement in a for loop would cause the loop to terminate when the if's question is false?
I am using bubble sort to make an array in order and once a is not less than b it stops.
thanks for any help......
- 12-07-2002 #2Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Probably you've forgot or accidently added a semicolon somewhere. I remember that I happened to do that after an if-statement once, and then after eight hours of debugging I finally found out why the statement always executed regardless of the conditions evaluation. That was rather frustrating.
I fyou post the code it will be easier to spot the error.
- 12-12-2002 #3Just Joined!
- Join Date
- Dec 2002
- Location
- Saweeden
- Posts
- 2
Re: For loop
for syntax:
Originally Posted by Speranza
for (initialisations; stop if statement is true; counter)
- 12-12-2002 #4Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
for could just as well be defined as a macro. Take the syntax "for(<f>; <c>; <r>) <stmt>". That could be expanded to:
<f>
while(<c>)
{
<stmt>
<r>
}
And that's it. It explains it all.
- 02-22-2003 #5Just Joined!
- Join Date
- Feb 2003
- Location
- Wah Cantt, Pakistan.
- Posts
- 8
then what is the difference b/w for and while loops they always seems to work same ? is there a situation when one can be used and not other??????
- 02-22-2003 #6Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
for is basically just an abbreviated while loop. In practice, they work exactly the same. I don't know if maybe the optimizer looks at them differently.
while loops are, of course much more general than for loops, such as when you don't want something to happen at the end of every cycly (of course, you could just leave that part of the for statement empty, but then there really wouldn't be much need for a for loop).
One common example is for using getopt to parse command line options, like this:
As you can see, you could use a for loop, but really: why?Code:while((c = getopt(argc, argc, "opts")) > 0) { switch(c) { case 'o': ... break; ... } }
- 02-22-2003 #7Linux Enthusiast
- Join Date
- Jun 2002
- Location
- San Antonio
- Posts
- 621
yes, the optimizer looks at them differently, and they translate into different machine code. The for loop (especially with a known iteration count) is highly optimized, where a while loop will not optimize out since the iteration variable could (technically) be changed outside of the loop scope (in a multi-threaded environment).
I respectfully decline the invitation to join your delusion.
- 02-22-2003 #8Linux Guru
- Join Date
- Oct 2001
- Location
- Täby, Sweden
- Posts
- 7,578
Technically, that could just as well happen in a for loop, also. I know that I've seen comments in gcc's optimizer code that they look at certain stuff in for loops, though. I didn't know how much while loops are optimized however.
I compared the assembler output of these two:
Code:for(i = 0; i < 10; i++) printf("test");Apart from a local label that was numbered differently (both with and without optimizing), there were no differences at all, so for simple examples, there are no differences at machine code level. I'd guess that the optimizer can do fancy stuff for more complex examples, though. For example, one of the things that I saw in the optimizer code was a comment that it was able to solve linear equations in for loops to pre-calculate the exact number of iterations. (As in for(; i < o; i++, o -= 2))Code:i = 0; while(i < 10) { printf("test"); i++; }
- 03-05-2003 #9Just Joined!
- Join Date
- Feb 2003
- Location
- Wah Cantt, Pakistan.
- Posts
- 8
thanks for providing information about what happens.


Reply With Quote
