Announcement

Collapse
No announcement yet.

code teaser

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #46
    Originally posted by cjolley
    The word may be "for", but c "for" loops are realy "while" constructs.
    Ok, they DO accomplish much the same thing. The for loop is more compact, though.

    In fact, unless I'm not remembering correctly*,
    for(i=x; i<j;--i)
    would decrement i even if j<i , at least on old compilers*Like that never happens.
    You're remembering incorrectly.

    - Gurm
    The Internet - where men are men, women are men, and teenage girls are FBI agents!

    I'm the least you could do
    If only life were as easy as you
    I'm the least you could do, oh yeah
    If only life were as easy as you
    I would still get screwed

    Comment


    • #47
      Originally posted by cjolley
      The word may be "for", but c "for" loops are realy "while" constructs.
      Basically for and while loops are the same logic construct. One is logically aimed at a known iterations while the other is logically aimed at an unknown number of iterations. But as a construct they are basically the same.

      Sorry if I have some syntax errors I haven't touched c in 10 years.

      Example 1: Know iterations...

      int i, n = -20;
      for (i = 0; i + n; i--)
      printf("-");

      or

      int i = 0 , n = -20;
      while( i + n)
      {
      printf("-");
      i--;
      }

      Example 2: unknown iterations

      Assumption that there is an int arry of [n] positions.


      int x = 0;
      while (intNumbers[x]!=null)
      {
      printf(intNumbers[x]);
      x++;
      }

      int x;
      for (x = 0 ; intNumbers[x]!=null ; x++)
      {
      printf(intNumbers[x]);
      }


      Example 3: unknows interations (ie listining to a port)...

      while (true)
      {
      ...code...
      }

      or

      for ( ; true ; )
      {
      ...code...
      }

      Switching between for and while loops was one of my standard lectures.


      Jeff
      Last edited by Duty; 18 October 2003, 12:12.
      -We stop learning when We die, and some
      people just don't know They're dead yet!

      Member of the COC!
      Minister of Confused Knightly Defence (MCKD)

      Food for thought...
      - Remember when naps were a bad thing?
      - Remember 3 is the magic number....

      Comment


      • #48
        Originally posted by cjolley
        In fact, unless I'm not remembering correctly*,
        for(i=x; i<j;--i)
        would decrement i even if j<i , at least on old compilers
        ++x = Pre-increment - this happens before the variable is interpreted (or used)

        x++ = Post-increment - this happens after the variable is interpreted (or used)

        So for(x=0 ; x < y ; x++) is the same as for(x=0 ; x < y ; ++x) because the variable is only incremented. Pre and post increment only become important when the variable is going to be interpreted and incremented (ie. int z=someFunction(x++) or int z=someFunction(++x)).

        I think this has been the standard since the beginning of ANSI C.

        Remember in C you can ++x--

        Have fun.

        Jeff
        Last edited by Duty; 18 October 2003, 13:17.
        -We stop learning when We die, and some
        people just don't know They're dead yet!

        Member of the COC!
        Minister of Confused Knightly Defence (MCKD)

        Food for thought...
        - Remember when naps were a bad thing?
        - Remember 3 is the magic number....

        Comment


        • #49
          Originally posted by Gurm
          Ok, they DO accomplish much the same thing. The for loop is more compact, though.
          - Gurm
          Well, I would say the difference between a "real" for loop and a c for loop is this.

          In PL/SQL, the Oracle programming language based on ada, a for loop is more like this:
          Code:
          SQL> @connect chajol@tax_test
          Enter password:
          Connected.
          [email]chajol@tax.expe[/email]rior> --a real for loop
          [email]chajol@tax.expe[/email]rior> declare
            2     i number := -1;
            3  begin
            4     for i in 1..3 loop
            5        dbms_output.put_line('i='||to_char(i));
            6     end loop;
            7     dbms_output.put_line('i='||to_char(i));
            8  end;
            9  /
          i=1
          i=2
          i=3
          i=-1
          
          PL/SQL procedure successfully completed.
          
          Elapsed: 00:00:00.16
          [email]chajol@tax.expe[/email]rior>
          i still = -1 because the i in the loop is in it's own name space and has nothing to do with the declared i

          This property makes it realy different from while.

          c's for is just a convenient construct for incriment driven whiles.

          chuck
          Chuck
          秋音的爸爸

          Comment


          • #50
            Originally posted by cjolley
            Well, I would say the difference between a "real" for loop and a c for loop is this.

            In PL/SQL, the Oracle programming language based on ada, a for loop is more like this:
            Code:
            SQL> @connect chajol@tax_test
            Enter password:
            Connected.
            [email]chajol@tax.expe[/email]rior> --a real for loop
            [email]chajol@tax.expe[/email]rior> declare
              2     i number := -1;
              3  begin
              4     for i in 1..3 loop
              5        dbms_output.put_line('i='||to_char(i));
              6     end loop;
              7     dbms_output.put_line('i='||to_char(i));
              8  end;
              9  /
            i=1
            i=2
            i=3
            i=-1
            
            PL/SQL procedure successfully completed.
            
            Elapsed: 00:00:00.16
            [email]chajol@tax.expe[/email]rior>
            i still = -1 because the i in the loop is in it's own name space and has nothing to do with the declared i

            This property makes it realy different from while.

            c's for is just a convenient construct for incriment driven whiles.

            chuck
            chuck,

            In Gurms defense, you’re wrong (sorry). You’re comparing syntax and variable scope, not logic. PL/SQL is not the right way, it is just a “BASIC syntax” derived way of building a FOR loop. The ADA syntax is a “BASIC derivative” just like VB, and COBOL. Where as C, C++, Smalltalk and Java are all derived from FORTRAN syntax. As to the variable, C has stronger variable naming constraints then PL/SQL, thats all.

            Have fun,
            Jeff
            -We stop learning when We die, and some
            people just don't know They're dead yet!

            Member of the COC!
            Minister of Confused Knightly Defence (MCKD)

            Food for thought...
            - Remember when naps were a bad thing?
            - Remember 3 is the magic number....

            Comment


            • #51
              Originally posted by Duty
              chuck,

              In Gurms defense, you’re wrong (sorry). You’re comparing syntax and variable scope, not logic. PL/SQL is not the right way, it is just a “BASIC syntax” derived way of building a FOR loop. The ADA syntax is a “BASIC derivative” just like VB, and COBOL. Where as C, C++, Smalltalk and Java are all derived from FORTRAN syntax. As to the variable, C has stronger variable naming constraints then PL/SQL, thats all.

              Have fun,
              Jeff
              Apparently I didn't make myself clear.
              I don't disagree with GURM.
              ALL for and while constructs are logicaly interchangeable.
              And I am not saying the PL/SQL for loop is "right".
              I think the code I gave should produce a duplicate declaration compile time error. That would save a lot of newbie heartach in the Oracle world.
              What I'm talking about is that the for in c has so few constraints on it that I would call it just a convenient syntax for while to a much larger extent than most other languages.
              I like the way the c for loop works and think it is great for clarifying while logic when the loop is controlled by a counter.
              But it is deadly if placed in the wrong hands. (or, I suppose, the right hands if you are a fan of obfuscated c like I am )
              The fact that you can do things like:
              for(printf("hello world/n"); random(1, 10000) > 9000; printf(""))
              and/or things like that means that the counter aspect that makes for different from while is simply not enforced at all in c.

              chuck

              PS, I am shocked to find that the closest thing I have at home now to an ansi c compiler is VS6
              My quick and turbo c disks are gone!?
              So much for including cute examples....
              Chuck
              秋音的爸爸

              Comment


              • #52
                Originally posted by cjolley
                (or, I suppose, the right hands if you are a fan of obfuscated c like I am )
                The fact that you can do things like:
                for(printf("hello world/n"); random(1, 10000) > 9000; printf(""))
                and/or things like that means that the counter aspect that makes for different from while is simply not enforced at all in c.
                Yeah, C really expects that you know what you are doing. You can do some serious wacked code with pointers.


                Jeff
                -We stop learning when We die, and some
                people just don't know They're dead yet!

                Member of the COC!
                Minister of Confused Knightly Defence (MCKD)

                Food for thought...
                - Remember when naps were a bad thing?
                - Remember 3 is the magic number....

                Comment

                Working...
                X