Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alf P. Steinbach

    Correct C++ tutorial part 3 "Intro to loops" available (Windows, mingw/msvc/std)

    The third part of my attempted Correct C++ tutorial is now available,
    although for now only in Word format (use free Open Office if no Word),
    and also, it's not yet been extensively reviewed -- comments welcome!

    "Intro to loops"
    <url: http://home.no.net/dubjai/win32cpptut/w32cpptut_01_03 .doc>

    As before I expect that here's mental food and technical points that
    we can strongly disagree about, for both newbie and the experienced.

    And as before the point of inviting comments is to _fix_ anything
    incorrect, or the presentation, or whatever; your input is valuable.

    The first two parts, "Hello, world!" and "Variables" , are available
    both as Word docs and (now reasonably structured! ;-)) HTML at

    <url: http://home.no.net/dubjai/win32cpptut/html/>

    Again, re part 2, thanks to Stephan Brönniman for helping with the
    conversion from Word to HTML, and helpful comments & corrections.


    [I inadvertently posted this message to comp.std.c++: darn that mouse!
    If any of the moderators read this and can cancel that, please.]

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
  • Bogdan Sintoma

    #2
    Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

    Hi Alf,

    In the chapter 8 maybe: "Always use an integral type such as int for a
    loop control variable" is 'to strong'. I think to something like:
    "chose the desired precision when dealing with floats and avoid 'equal'
    operators".

    Another pitfall could be: for( unsigned int i = 5; i >= 0; --i ) { ...
    }

    Best regards,
    Bogdan Sintoma

    Comment

    • Alf P. Steinbach

      #3
      Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

      * Alf P. Steinbach:[color=blue]
      > The third part of my attempted Correct C++ tutorial is now available,
      > although for now only in Word format (use free Open Office if no Word),
      > and also, it's not yet been extensively reviewed -- comments welcome!
      >
      > "Intro to loops"
      > <url: http://home.no.net/dubjai/win32cpptut/w32cpptut_01_03 .doc>
      >
      > As before I expect that here's mental food and technical points that
      > we can strongly disagree about, for both newbie and the experienced.
      >
      > And as before the point of inviting comments is to _fix_ anything
      > incorrect, or the presentation, or whatever; your input is valuable.[/color]

      Well, I discovered one Big Blunder myself! :-o

      In section 4, "[Pitfall:] Using assignment instead of comparision",
      I described an if-condition as accepting non-bool values because it
      converted bool to numerical zero / non-zero. That was correct as an
      effective belief, and may be the way it is in C, but it's not how it
      actually works in C++. In C++ the same _effect_ is achieved by having
      implicit conversion of non-bool values (zero, non-zero) to bool.

      Now corrected...

      --
      A: Because it messes up the order in which people normally read text.
      Q: Why is it such a bad thing?
      A: Top-posting.
      Q: What is the most annoying thing on usenet and in e-mail?

      Comment

      • Alf P. Steinbach

        #4
        Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

        * Bogdan Sintoma:[color=blue]
        > Hi Alf,
        >
        > In the chapter 8 maybe: "Always use an integral type such as int for a
        > loop control variable" is 'to strong'. I think to something like:
        > "chose the desired precision when dealing with floats and avoid 'equal'
        > operators".[/color]

        Well, here's my reasoning:

        * If you use e.g. 'double' as a loop control variable (counter), you
        either use integer update (e.g. increment) or non-integer update.

        * If you use integer update you should use integer type for the
        variable.

        * If you use non-integer update you're in trouble, and/or are
        doing things in a needlessly complicated way; either way the
        solution is to use integer type for the loop control variable.

        Additionally, it would take much text to explain how to compare
        floating point values "safely", and what degree of safety can be
        achieved (it's an advanced topic one can write a whole book about...).

        Does this make sense?


        [color=blue]
        > Another pitfall could be: for( unsigned int i = 5; i >= 0; --i ) { ...
        > }[/color]

        Yes, thanks.

        My thinking here has been to introduce only the bare minimum of
        _general_ functionality, not things that are really only optimization or
        only useful in special cases, and so no 'unsigned' yet, only 'int'.

        But perhaps that pitfall is so common that it deserves to be mentioned
        even at this early stage?

        --
        A: Because it messes up the order in which people normally read text.
        Q: Why is it such a bad thing?
        A: Top-posting.
        Q: What is the most annoying thing on usenet and in e-mail?

        Comment

        • Bogdan Sintoma

          #5
          Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

          Alf P. Steinbach wrote:[color=blue]
          > * Bogdan Sintoma:[color=green]
          > > Hi Alf,
          > >
          > > In the chapter 8 maybe: "Always use an integral type such as int[/color][/color]
          for a[color=blue][color=green]
          > > loop control variable" is 'to strong'. I think to something like:
          > > "chose the desired precision when dealing with floats and avoid[/color][/color]
          'equal'[color=blue][color=green]
          > > operators".[/color]
          >
          > Well, here's my reasoning:
          >
          > * If you use e.g. 'double' as a loop control variable (counter),[/color]
          you[color=blue]
          > either use integer update (e.g. increment) or non-integer update.[/color]
          Well, it could be situations when a loop control variable isn't really
          a counter. Double as control variable and increment (or decrement)
          isn't a smart choice :) (GotW #67 ;) )
          [color=blue]
          >
          > * If you use integer update you should use integer type for the
          > variable.[/color]
          Agree.
          [color=blue]
          >
          > * If you use non-integer update you're in trouble, and/or are
          > doing things in a needlessly complicated way; either way the
          > solution is to use integer type for the loop control variable.[/color]
          Let's say that you have to simulate some process. And this process must
          run until some variable (e.g some cumulated errors) exceed some level.
          Using this variable as a loop control variable could be more
          appropriate than converting (rounding) it and the limit to some integer
          value.

          Anyway, I just wand to say something like "never say never" :)
          [color=blue]
          >
          > Additionally, it would take much text to explain how to compare
          > floating point values "safely", and what degree of safety can be
          > achieved (it's an advanced topic one can write a whole book[/color]
          about...).[color=blue]
          >[/color]
          That's true.
          [color=blue]
          > Does this make sense?
          >
          >[color=green]
          > > Another pitfall could be: for( unsigned int i = 5; i >= 0; --i ) {[/color][/color]
          ....[color=blue][color=green]
          > > }[/color]
          >
          > Yes, thanks.
          >
          > My thinking here has been to introduce only the bare minimum of
          > _general_ functionality, not things that are really only optimization[/color]
          or[color=blue]
          > only useful in special cases, and so no 'unsigned' yet, only 'int'.
          >
          > But perhaps that pitfall is so common that it deserves to be[/color]
          mentioned[color=blue]
          > even at this early stage?
          >[/color]

          Not if it is just me that say so :).
          But, size_t is unsigned, isn't it? Should I write the pitfall as:
          for( vector<int>::si ze_type i = (v.size()-1); i >= 0; --i ){...} ? :)

          Comment

          • Computer Whizz

            #6
            Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)


            "Alf P. Steinbach" <alfps@start.no > wrote in message
            news:41ee103b.9 59845921@news.i ndividual.net.. .[color=blue]
            > The third part of my attempted Correct C++ tutorial is now available,
            > although for now only in Word format (use free Open Office if no Word),
            > and also, it's not yet been extensively reviewed -- comments welcome!
            >
            > "Intro to loops"
            > <url: http://home.no.net/dubjai/win32cpptut/w32cpptut_01_03 .doc>
            >
            > As before I expect that here's mental food and technical points that
            > we can strongly disagree about, for both newbie and the experienced.
            >
            > And as before the point of inviting comments is to _fix_ anything
            > incorrect, or the presentation, or whatever; your input is valuable.
            >
            > The first two parts, "Hello, world!" and "Variables" , are available
            > both as Word docs and (now reasonably structured! ;-)) HTML at
            >
            > <url: http://home.no.net/dubjai/win32cpptut/html/>
            >
            > Again, re part 2, thanks to Stephan Brvnniman for helping with the
            > conversion from Word to HTML, and helpful comments & corrections.
            >
            >
            > [I inadvertently posted this message to comp.std.c++: darn that mouse!
            > If any of the moderators read this and can cancel that, please.]
            >
            > --
            > A: Because it messes up the order in which people normally read text.
            > Q: Why is it such a bad thing?
            > A: Top-posting.
            > Q: What is the most annoying thing on usenet and in e-mail?[/color]

            May I ask why you chose to do loops and then the IF statement, instead of
            the other way around?

            IMO it would have been more clear to explain IF, the go onto a loop, then
            briefly do IF and then go into more detail later on (in your next part I
            presume).

            I find these tutorials very nice, and reading through them has been very
            pleasant - thank you for writing these!

            --
            =========
            Comp Whizz
            =========
            (The C++ beginner)


            Comment

            • Alf P. Steinbach

              #7
              Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

              * Bogdan Sintoma:[color=blue]
              > Alf P. Steinbach wrote:[color=green]
              > > * Bogdan Sintoma:[color=darkred]
              > > > Hi Alf,
              > > >
              > > > In the chapter 8 maybe: "Always use an integral type such as int
              > > > for a loop control variable" is 'to strong'. I think to something
              > > > like: "chose the desired precision when dealing with floats and
              > > > avoid 'equal' operators".[/color]
              > >
              > > Well, here's my reasoning:
              > >
              > > * If you use e.g. 'double' as a loop control variable (counter),
              > > you either use integer update (e.g. increment) or non-integer
              > > update.[/color]
              >
              > Well, it could be situations when a loop control variable isn't really
              > a counter.[/color]

              Does it matter whether it counts or not?

              [color=blue]
              > Double as control variable and increment (or decrement)
              > isn't a smart choice :) (GotW #67 ;) )[/color]

              At first reading that seems like we're in agreement, but on second
              reading I'm not sure I grok that statement -- and what's
              <url: http://www.gotw.ca/gotw/067.htm> got to do with this?


              [snip][color=blue][color=green]
              > > * If you use non-integer update you're in trouble, and/or are
              > > doing things in a needlessly complicated way; either way the
              > > solution is to use integer type for the loop control variable.[/color]
              >
              > Let's say that you have to simulate some process. And this process must
              > run until some variable (e.g some cumulated errors) exceed some level.
              > Using this variable as a loop control variable could be more
              > appropriate than converting (rounding) it and the limit to some integer
              > value.[/color]

              One example could be to iteratively determine the square root of n,
              using a 'double' variable 'root' and binary search. But now I don't
              think of that variable as a loop control variable. Trying to pin down
              what I mean, which I think is the general accepted meaning: in each
              iteration except the first the value of a loop control variable depends
              (only) on the previous value of that variable, the values in different
              iterations except possibly the last one are different, and the loop
              condition depends on that variable and possibly more.

              The WikiPedia definition of 'loop counter' is somewhat similar, at
              <url: http://en.wikipedia.or g/wiki/Loop_counter>, but I don't like using
              the word "counter" which implies a more restricted meaning.

              On the other hand, using the Newton/Raphson algorithm 'root' would
              qualify as a loop control variable, with both the above definitions.

              So I think you're right that my advice excludes a whole general class
              of useful 'double' loop control variables.

              What could be a good way to include this information in the text,
              keeping it reasonably short and novice-level?


              [snip][color=blue]
              > But, size_t is unsigned, isn't it? Should I write the pitfall as:
              > for( vector<int>::si ze_type i = (v.size()-1); i >= 0; --i ){...} ? :)[/color]

              Thinking about that pitfall it's part of a more general pitfall, namely
              assuming integer values exist outside the defined range.

              I think it will have to wait till a general discussion of that.

              If, when, I get to that... :-)

              --
              A: Because it messes up the order in which people normally read text.
              Q: Why is it such a bad thing?
              A: Top-posting.
              Q: What is the most annoying thing on usenet and in e-mail?

              Comment

              • Alf P. Steinbach

                #8
                Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

                * Computer Whizz:[color=blue]
                >
                > May I ask why you chose to do loops and then the IF statement, instead of
                > the other way around?[/color]

                Well I didn't... ;-) It's like the resolution of a circular class
                dependency A <-> B by introducing abstract base classes A' and B', and
                then A <- (A' + B'), B <- (A' + B'), where "x <- y" means x depends on
                y. What I did was to first introduce a raw infinite loop (base class
                A'), which serves to introduce an unconditional jump which I think is
                the most basic and easiest-to-comprehend form, and then a simple 'if'
                (base class B'), which serves to introduce a conditional jump.

                After that a more full discussion of loops (concrete class A), done
                first because loops allow you to do interesting things.

                The next part tackles decisions in general (concrete class B).


                [snip][color=blue]
                > I find these tutorials very nice, and reading through them has been very
                > pleasant - thank you for writing these![/color]

                Thank _you_.

                --
                A: Because it messes up the order in which people normally read text.
                Q: Why is it such a bad thing?
                A: Top-posting.
                Q: What is the most annoying thing on usenet and in e-mail?

                Comment

                • Bogdan Sintoma

                  #9
                  Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

                  Alf P. Steinbach wrote:[color=blue][color=green][color=darkred]
                  > > > In the chapter 8 maybe: "Always use an integral type such as int
                  > > > > for a loop control variable" is 'to strong'. I think to[/color][/color][/color]
                  something[color=blue][color=green][color=darkred]
                  > > > > like: "chose the desired precision when dealing with floats and
                  > > > > avoid 'equal' operators".
                  > > >
                  > > > Well, here's my reasoning:
                  > > >
                  > > > * If you use e.g. 'double' as a loop control variable[/color][/color][/color]
                  (counter),[color=blue][color=green][color=darkred]
                  > > > you either use integer update (e.g. increment) or non-integer
                  > > > update.[/color]
                  > >
                  > > Well, it could be situations when a loop control variable isn't[/color][/color]
                  really[color=blue][color=green]
                  > > a counter.[/color]
                  >
                  > Does it matter whether it counts or not?[/color]
                  Maybe, please see below.
                  [color=blue]
                  >[color=green]
                  > > Double as control variable and increment (or decrement)
                  > > isn't a smart choice :) (GotW #67 ;) )[/color]
                  >
                  > At first reading that seems like we're in agreement, but on second
                  > reading I'm not sure I grok that statement -- and what's
                  > <url: http://www.gotw.ca/gotw/067.htm> got to do with this?[/color]

                  You mention increment and 'doubles'. I just want to point out that
                  there are float implementation that cannot exactly represent all the
                  integer values from certain interval.
                  And, yes, we are in agreement for 'count-controlled loops'.

                  [snip]
                  [color=blue]
                  > One example could be to iteratively determine the square root of n,
                  > using a 'double' variable 'root' and binary search. But now I don't
                  > think of that variable as a loop control variable. Trying to pin[/color]
                  down[color=blue]
                  > what I mean, which I think is the general accepted meaning: in each
                  > iteration except the first the value of a loop control variable[/color]
                  depends[color=blue]
                  > (only) on the previous value of that variable, the values in[/color]
                  different[color=blue]
                  > iterations except possibly the last one are different, and the loop
                  > condition depends on that variable and possibly more.
                  >
                  > The WikiPedia definition of 'loop counter' is somewhat similar, at
                  > <url: http://en.wikipedia.or g/wiki/Loop_counter>, but I don't like[/color]
                  using[color=blue]
                  > the word "counter" which implies a more restricted meaning.
                  >[/color]
                  Yes, that's true, but let me cite the same source:

                  "A loop is a sequence of statements which is specified once but which
                  may be carried out several times in succession. The code "inside" the
                  loop (the body of the loop, shown below as xxx) is obeyed a specified
                  number of times, or once for each of a collection of items, or until
                  some condition is met."
                  And further 'count-controlled' and 'condition-controlled' loops are
                  defined.
                  The c++ 'for' statement can be used in both cases.
                  (Actually "the standard" - 6.5.3.1 - define the 'for' statement in
                  equivalence with the 'while' statement)
                  [color=blue]
                  > On the other hand, using the Newton/Raphson algorithm 'root' would
                  > qualify as a loop control variable, with both the above definitions.
                  >
                  > So I think you're right that my advice excludes a whole general class
                  > of useful 'double' loop control variables.
                  >[/color]
                  Thanks :)
                  [color=blue]
                  > What could be a good way to include this information in the text,
                  > keeping it reasonably short and novice-level?
                  >[/color]
                  Well, that's not so easy :).
                  What about: "Don't use floating point types as loops control variable,
                  unless you really know what you are doing." :)
                  [color=blue]
                  >
                  > [snip][color=green]
                  > > But, size_t is unsigned, isn't it? Should I write the pitfall as:
                  > > for( vector<int>::si ze_type i = (v.size()-1); i >= 0; --i ){...} ?[/color][/color]
                  :)[color=blue]
                  >
                  > Thinking about that pitfall it's part of a more general pitfall,[/color]
                  namely[color=blue]
                  > assuming integer values exist outside the defined range.
                  >[/color]
                  Agree.
                  [color=blue]
                  > I think it will have to wait till a general discussion of that.
                  >
                  > If, when, I get to that... :-)[/color]
                  Just erase the "if" ;).

                  Best regards,
                  Bogdan

                  Comment

                  • Alf P. Steinbach

                    #10
                    Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

                    * Bogdan Sintoma:[color=blue]
                    > What about: "Don't use floating point types as loops control variable,
                    > unless you really know what you are doing." :)[/color]

                    Done.

                    Thanks,

                    - Alf

                    --
                    A: Because it messes up the order in which people normally read text.
                    Q: Why is it such a bad thing?
                    A: Top-posting.
                    Q: What is the most annoying thing on usenet and in e-mail?

                    Comment

                    • Attila Feher

                      #11
                      Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

                      Alf P. Steinbach wrote:[color=blue]
                      > * Bogdan Sintoma:[color=green]
                      >> Hi Alf,
                      >>
                      >> In the chapter 8 maybe: "Always use an integral type such as int for
                      >> a loop control variable" is 'to strong'. I think to something like:
                      >> "chose the desired precision when dealing with floats and avoid
                      >> 'equal' operators".[/color]
                      >
                      > Well, here's my reasoning:
                      >
                      > * If you use e.g. 'double' as a loop control variable (counter), you
                      > either use integer update (e.g. increment) or non-integer update.[/color]

                      Yeah. And you use either C++ or not to write it. ;-)
                      [color=blue]
                      > * If you use integer update you should use integer type for the
                      > variable.[/color]

                      Why? You have introduced your lines with the word reasoning, but I see no
                      reason told for this statement.
                      [color=blue]
                      > * If you use non-integer update you're in trouble, and/or are
                      > doing things in a needlessly complicated way; either way the
                      > solution is to use integer type for the loop control variable.[/color]

                      Again, no reasoning. Using floating point numbers where floating point
                      operations are needed is (IMHO) not needlessly complicated for two reasons:
                      1.) Read the beginning of the sentence, 2.) using floating point is only
                      complicated until you learn it.
                      [color=blue]
                      > Additionally, it would take much text to explain how to compare
                      > floating point values "safely", and what degree of safety can be
                      > achieved (it's an advanced topic one can write a whole book about...).[/color]

                      That is the only reasoning in your text. Which suggests that instead of
                      making up a rule which is not supported by any real arguments (original
                      says: "Always use an integral type such as int for a loop control variable",
                      so it effectively rules out iterators as well as a billion other things) why
                      not tell simply what you have said above: floating point arithmetics is a
                      topic of its own, not something which belongs to a beginner's tutorial?
                      [color=blue]
                      > Does this make sense?[/color]

                      Only the last sentence. The rest makjes sense too, as an opinion, but not
                      as reasoning. ;-)
                      [color=blue][color=green]
                      >> Another pitfall could be: for( unsigned int i = 5; i >= 0; --i ) {
                      >> ... }[/color]
                      >
                      > Yes, thanks.
                      >
                      > My thinking here has been to introduce only the bare minimum of
                      > _general_ functionality, not things that are really only optimization
                      > or only useful in special cases, and so no 'unsigned' yet, only 'int'.
                      >
                      > But perhaps that pitfall is so common that it deserves to be mentioned
                      > even at this early stage?[/color]

                      There is somewhere on the net (was it Stroustrups pages?) a paper on why not
                      ot use unsigned variables for most of the things. So probably you want to
                      introduce that opportunity later, so people don't fly off course on a
                      tangent and start playing around with unsigned values.

                      --
                      Attila aka WW


                      Comment

                      • Alf P. Steinbach

                        #12
                        Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

                        * Attila Feher:[color=blue]
                        > Alf P. Steinbach wrote:[color=green]
                        > > * Bogdan Sintoma:[color=darkred]
                        > >> Hi Alf,
                        > >>
                        > >> In the chapter 8 maybe: "Always use an integral type such as int for
                        > >> a loop control variable" is 'to strong'. I think to something like:
                        > >> "chose the desired precision when dealing with floats and avoid
                        > >> 'equal' operators".[/color]
                        > >
                        > > Well, here's my reasoning:
                        > >
                        > > * If you use e.g. 'double' as a loop control variable (counter), you
                        > > either use integer update (e.g. increment) or non-integer update.[/color]
                        >
                        > Yeah. And you use either C++ or not to write it. ;-)[/color]

                        That's the context: a numeric loop control variable, with two mutually
                        excluding and exhaustive possibilities then discussed.

                        [color=blue][color=green]
                        > > * If you use integer update you should use integer type for the
                        > > variable.[/color]
                        >
                        > Why? You have introduced your lines with the word reasoning, but I see no
                        > reason told for this statement.[/color]

                        It seems to boil down to two issues: why it's not a good idea to use a
                        misleading type (maintainabilit y), and whether a floating point type can
                        always represent all values of an integral type exactly (technical).

                        [color=blue][color=green]
                        > > * If you use non-integer update you're in trouble, and/or are
                        > > doing things in a needlessly complicated way; either way the
                        > > solution is to use integer type for the loop control variable.[/color]
                        >
                        > Again, no reasoning. Using floating point numbers where floating point
                        > operations are needed is (IMHO) not needlessly complicated for two reasons:
                        > 1.) Read the beginning of the sentence, 2.) using floating point is only
                        > complicated until you learn it.[/color]

                        Yes, to the latter two sentences. I wonder whether you are happy with
                        the resolution of that issue as it turned out in the rest of the thread?
                        That is, adding the parenthetical expression "unless you really know
                        what you're doing"?

                        [color=blue][color=green]
                        > > Additionally, it would take much text to explain how to compare
                        > > floating point values "safely", and what degree of safety can be
                        > > achieved (it's an advanced topic one can write a whole book about...).[/color]
                        >
                        > That is the only reasoning in your text. Which suggests that instead of
                        > making up a rule which is not supported by any real arguments (original
                        > says: "Always use an integral type such as int for a loop control variable",
                        > so it effectively rules out iterators as well as a billion other things) why
                        > not tell simply what you have said above: floating point arithmetics is a
                        > topic of its own, not something which belongs to a beginner's tutorial?[/color]

                        Iterators and pointers as loop control variables is a good point,
                        thanks.

                        The discussion above was trapped in the implicit context of numeric loop
                        control variables.

                        Possibly I should add the word "numeric" in the advice. But I think it
                        would then hang in thin air, not related to the rest of the discussion.
                        Suggestions?

                        --
                        A: Because it messes up the order in which people normally read text.
                        Q: Why is it such a bad thing?
                        A: Top-posting.
                        Q: What is the most annoying thing on usenet and in e-mail?

                        Comment

                        • Attila Feher

                          #13
                          Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

                          Alf P. Steinbach wrote:
                          [SNIP][color=blue][color=green][color=darkred]
                          >>> * If you use integer update you should use integer type for the
                          >>> variable.[/color]
                          >>
                          >> Why? You have introduced your lines with the word reasoning, but I
                          >> see no reason told for this statement.[/color]
                          >
                          > It seems to boil down to two issues: why it's not a good idea to use a
                          > misleading type (maintainabilit y), and whether a floating point type
                          > can always represent all values of an integral type exactly
                          > (technical).[/color]

                          Why would a floating point type misleading if I need a floating point value?
                          Furthermore - on the same note - why would I care about it representing or
                          not integer values, if I needed a floating point one in the first place?

                          [SNIP][color=blue]
                          > Yes, to the latter two sentences. I wonder whether you are happy with
                          > the resolution of that issue as it turned out in the rest of the
                          > thread? That is, adding the parenthetical expression "unless you
                          > really know what you're doing"?[/color]

                          That is one way to put it. :-) But I guess there is another and probably
                          more important reason/cause: do you need a floating point iteration or not?
                          :-) And then comes the know what you do, do what you know. ;-)

                          [SNIP][color=blue]
                          > Iterators and pointers as loop control variables is a good point,
                          > thanks.[/color]

                          No problemo. :-)
                          [color=blue]
                          > The discussion above was trapped in the implicit context of numeric
                          > loop control variables.[/color]

                          And it possibly should for that very-beginner part, but IMHO the rule itself
                          may need to be worded differently. So that it suggests to the reader that
                          the rules is supposed to say: do not use floating point unless you really
                          need it (IMHO this goes not only for loops), so basically tell what is
                          wrong, instead of identifying what is right. Because limiting the possible
                          types (in this case/stage) will rule out valid cases, valid cases you did
                          not introduce yet for a reason.
                          [color=blue]
                          > Possibly I should add the word "numeric" in the advice. But I think
                          > it would then hang in thin air, not related to the rest of the
                          > discussion. Suggestions?[/color]

                          What I wrote above could probably be worded.

                          Loops can do (at least) counting and iterating, not taking into account the
                          "abuses" of them, like when a do {} while(0); loop (with breaks inside) is
                          used instead of a local try-catch...

                          So the loop variable in those non-abuse cases can mean a numeric value
                          (counting), and index value (addressing into containers using the array
                          access) or an iterator. A loop walking a linked list (with the loop
                          variable being a pointer to one list element) is (IMHO) a special case of
                          iteration.

                          My point is: there can be numerous other (legal and "good design") uses of
                          loops, which the current rule rules out. If the rule could be worded to
                          only say what not to do, the "unknown number of other legal uses" would not
                          be affected by it.

                          --
                          Attila aka WW


                          Comment

                          • Alf P. Steinbach

                            #14
                            Re: Correct C++ tutorial part 3 &quot;Intro to loops&quot; available (Windows, mingw/msvc/std)

                            * Attila Feher:[color=blue]
                            >
                            > If the rule could be worded to only say what not to do, the
                            > "unknown number of other legal uses" would not be affected by it.[/color]

                            Done.

                            New wording:

                            <quote>
                            That's partly why the program uses the integer type int for the counter,
                            instead of double (thanks to Bogdan Sintoma and Attila Feher for good advice
                            concerning the wording here):

                            * Don't use a floating point type such as double for a loop control variable
                            (unless you really know what you're doing).

                            If you do use e.g. double you'll get accumulated rounding errors, and
                            probably end up with a program that loops one less time than it "should"
                            and results that are very wildly off the mark.
                            </quote>

                            Thanks,

                            - Alf

                            --
                            A: Because it messes up the order in which people normally read text.
                            Q: Why is it such a bad thing?
                            A: Top-posting.
                            Q: What is the most annoying thing on usenet and in e-mail?

                            Comment

                            Working...