>> How does C++ test the expression? Does it increase the value of k then test the expression or does it first test then increase the value of k

in case of operator ++ it depends.

1.) if you use preincrement operator (++k). then C++ first increments k, and then use it in any expression that ++k is in.
2.) if you use postincrement operator (k++). then C++ use k for computing the expression that k++ is in, and afther that increments k.

In your piece of code you use postincrement operator and thats why you have k = 12 value at the end of loop.

value before comparation increment
comparation
k = 0, (0<=10), k = k + 1 = 1;
k = 1, (1<=10), k = k + 1 = 2;
...
k = 9, (9 <=10), k= k + 1 = 10;
k = 10, (10 <= 10), k = k + 1= 11;
k = 11, (11 <= 10); this is the end of loop, but you still have k = k+1 = 12!

If you have used ++k, you wold have k = 11 at the end.

value before increment comparation
comparation
k = 0, k = k + 1 = 1 k = k + 1 = 1;
k = 1, k = k + 1 = 2 k = k + 1 = 2;
...
k = 9, k = k + 1 = 10 k= k + 1 = 10;
k = 10, k = k + 1 = 11 (11 <= 10), false, end of loop.

I guess QuickWatch increments k during expression evaluation - good example of debugger which changes program results. Don't use QuickWatch expressions which change variable values.

Is it inherent to loops that even after exiting the loop, the value of the variable, in this case k, always increases.

0

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Loop can't change variable after exiting from it!
Every loop in C++ has some condition that is neccessary to be true in order to begin next iteration.

example:

while (this_condition) {}

for (...; this_condition; ...) {}

do {} while (this_condition);

note that this_condition is tested before / after(do..while loop) each iteration. So if you have some change on your variable in this_condition than it will be done each time when this_condition is true, even when the loop ends (this_condition is false in this case).

value before comparation increment
comparation
k = 0, (0<=10), k = k + 1 = 1;
k = 1, (1<=10), k = k + 1 = 2;
...
k = 9, (9 <=10), k= k + 1 = 10;
k = 10, (10 <= 10), k = k + 1= 11;
k = 11, (11 <= 10); this is the end of loop, but you still have k = k+1 = 12!

comparation is false but still the value increases!!!

It is not important whether comparation is true or false, k++ is in this comparation and therefore k increases.

in the last iteration you have:

k is 11,
(k++ <= 10) is (11 <= 10), which is false,
but C++ now must increment k (k is 12 now), because k++ was in expression.
As I already said, compailer saw k++ in comparation and therefore he must increment k. That is totally independent of value of comparation and must be done in both (true and false) cases!

>> How does C++ test the expression? Does it increase the value of k then test the expression or does it first test then increase the value of k

in case of operator ++ it depends.

1.) if you use preincrement operator (++k). then C++ first increments k, and then use it in any expression that ++k is in.

2.) if you use postincrement operator (k++). then C++ use k for computing the expression that k++ is in, and afther that increments k.

In your piece of code you use postincrement operator and thats why you have k = 12 value at the end of loop.

value before comparation increment

comparation

k = 0, (0<=10), k = k + 1 = 1;

k = 1, (1<=10), k = k + 1 = 2;

...

k = 9, (9 <=10), k= k + 1 = 10;

k = 10, (10 <= 10), k = k + 1= 11;

k = 11, (11 <= 10); this is the end of loop, but you still have k = k+1 = 12!

If you have used ++k, you wold have k = 11 at the end.

value before increment comparation

comparation

k = 0, k = k + 1 = 1 k = k + 1 = 1;

k = 1, k = k + 1 = 2 k = k + 1 = 2;

...

k = 9, k = k + 1 = 10 k= k + 1 = 10;

k = 10, k = k + 1 = 11 (11 <= 10), false, end of loop.

For second question, I have really no idea :-).