开发者

do while loop and others

when I modify string or another variable inside the loop it's condition is rec开发者_开发知识库alculated each time? or once before the loop start


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


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


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).


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜