Find the answer to your Linux question:
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.
  1. #1
    Just 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......

  2. #2
    Linux 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.

  3. #3
    Just Joined!
    Join Date
    Dec 2002
    Location
    Saweeden
    Posts
    2

    Re: For loop

    Quote Originally Posted by Speranza
    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......
    for syntax:

    for (initialisations; stop if statement is true; counter)

  4. #4
    Linux 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.

  5. #5
    Just 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??????

  6. #6
    Linux 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:
    Code:
    while&#40;&#40;c = getopt&#40;argc, argc, "opts"&#41;&#41; > 0&#41;
    &#123;
        switch&#40;c&#41;
        &#123;
            case 'o'&#58;
                ...
                break;
            ...
        &#125;
    &#125;
    As you can see, you could use a for loop, but really: why?

  7. #7
    Linux 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.

  8. #8
    Linux 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&#40;i = 0; i < 10; i++&#41;
        printf&#40;"test"&#41;;
    Code:
    i = 0;
    while&#40;i < 10&#41;
    &#123;
        printf&#40;"test"&#41;;
        i++;
    &#125;
    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))

  9. #9
    Just Joined!
    Join Date
    Feb 2003
    Location
    Wah Cantt, Pakistan.
    Posts
    8
    thanks for providing information about what happens.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •