I'm trying to understand the while loop more deeply. I understad the basics which is that a while repeats the statement as long as the test condition is true.
In the book Accelerated C++ the authors state the following:
//the number of blanks surrounds the greeting
const int pad = 1;
//total number of rows to write
const int rows = pad * 2 + 3;
//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
I don't get it, why does writing a row of output the invariant false, and then true again at the increment. Did the authors of the book made an mistake?
Edit - The variant is just an intellectual tool to understand a while loop easier. In this example the variant is "r = number of rows in output".
-----------------------what's an invariant---------------