Yevhen
Yevhen

Reputation: 1965

do while loop and others

when I modify string or another variable inside the loop it's condition is recalculated each time? or once before the loop start


std::string a("aa");
do
{
a = "aaaa";
}
while(a.size<10)
and what about for loop

Upvotes: 0

Views: 227

Answers (3)

checker
checker

Reputation: 531

Do ... while loops will check the condition every time AFTER the inside of the loop has been executed.

For loops will check the condition every time BEFORE the inside of the loop has been executed.

Upvotes: 1

DennyRolling
DennyRolling

Reputation: 486

imagine what would happen if condition is not recalculated. then if that was true to begin with it would never change and you will get an infinite loop.

having said that in your case the condition is always true (because string length doesn't change).

Upvotes: 1

Reese Moore
Reese Moore

Reputation: 11640

Every time. Basically it checks every time to see if the statement inside the conditional is true. If it is true, continue to loop, if it is false break the loop. That is why these constructs are called Conditional Loops

Upvotes: 6

Related Questions