# Confused

Printable View

• 04-05-2002
Clyde
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.
• 05-09-2004
indigo0086
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.