Thread: Confused

  1. #1
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403

    Confused

    I just bought "Accelerated C++ -Practical programming by example"

    It seems like a great book... but i'm really confused about something:

    On page 21 (for those of you that have the book)

    They explain how a "while loop" works (which btw. i already know) but they seem to have said something really weird. Here is the code in the book:

    Code:
    //  invariant: we have written r rows so far
    
    int r = 0;
    //  setting r to 0 makes the invariant true
    
    while (r != rows) {
           //  we can assume that the invariant is true here
    
           //  writing a row of output makes the invariant false
           std::cout <<  std::endl;
    
           //  incrementing r makes the invariant true again
           ++r;
    }
    
    //  we can conclude that the invariant is true here
    Now I'm really confused; as I understand it when they refer to "the invariant" they are reffering to r not equalling rows, they previously declared rows to be an integer equal to 5 (on page 18):

    Code:
           //  the number of blanks surrounding the greeting
           const int pad = 1;
    
           //  total number of rows to write
           const int rows = pad * 2 + 3;
    So my question is why is the invariant made false by outputting a newline?? Surely its not, surely if i didn't include the "++r;" rather than the while loop stopping as seem's to be implied it would go on for ever......... what am I missing?

    EDIT: I get it, i was misunderstanding what they meant by invariant.... still fail to see its uses, loops seem pretty easy to understand without these invariant things.
    Last edited by Clyde; 04-05-2002 at 05:27 PM.

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by Clyde
    I just bought "Accelerated C++ -Practical programming by example"

    It seems like a great book... but i'm really confused about something:

    On page 21 (for those of you that have the book)

    They explain how a "while loop" works (which btw. i already know) but they seem to have said something really weird. Here is the code in the book:

    Code:
    //  invariant: we have written r rows so far
    
    int r = 0;
    //  setting r to 0 makes the invariant true
    
    while (r != rows) {
           //  we can assume that the invariant is true here
    
           //  writing a row of output makes the invariant false
           std::cout <<  std::endl;
    
           //  incrementing r makes the invariant true again
           ++r;
    }
    
    //  we can conclude that the invariant is true here
    Now I'm really confused; as I understand it when they refer to "the invariant" they are reffering to r not equalling rows, they previously declared rows to be an integer equal to 5 (on page 18):

    Code:
           //  the number of blanks surrounding the greeting
           const int pad = 1;
    
           //  total number of rows to write
           const int rows = pad * 2 + 3;
    So my question is why is the invariant made false by outputting a newline?? Surely its not, surely if i didn't include the "++r;" rather than the while loop stopping as seem's to be implied it would go on for ever......... what am I missing?

    EDIT: I get it, i was misunderstanding what they meant by invariant.... still fail to see its uses, loops seem pretty easy to understand without these invariant things.

    hey, reading the same book and I was a little confused myself. I know this is an old topic but maybe you still need info on it. Basically, before the while statement, r was set equal to 0. The invariant tells us that we have 0 rows so far. Now, when you write the row, you have written one row while the r tells us that there are 0 rows, which is false. By setting ++r, you increase r to 1, which corresponds to the row you just created, making the invariant true. I noticed that the invariant is just a tool to help you understand thet loop better. IN my case it doesn't really, but for this book, when they go in to nexted loops (which isn't far from where you are, in fact, the same chapter) than it helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 05:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 03:29 AM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 01:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM