首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

do-while loop

反复执行语句,直到表达式的值变为false。测试在每次迭代之后进行。

句法

attr(optional) do statement while ( expression ) ;

?

?

attr(C++11)

-

any number of attributes

expression

-

any expression which is contextually convertible to bool. This expression is evaluated after each iteration, and if it yields false, the loop is exited.

statement

-

any statement, typically a compound statement, which is the body of the loop

解释

语句总是至少执行一次,即使表达式总是产生false。如果在这种情况下不应该执行,当或为可以使用循环。

如果需要在某个点终止循环的执行,断续语句可用作终止语句。

如果循环的执行需要在循环主体的末尾继续,继续语句可以用作快捷方式。

注记

作为C++的一部分前进保障,行为是未定如果循环没有可观察行为%28不调用I/O函数、访问易失性对象或执行原子或同步操作%29不终止。编译器可以删除这些循环。

关键词

do,,,while...

二次

代码语言:javascript
复制
#include <iostream>
#include <algorithm>
#include <string>
 
int main()
{
    int j = 2;
    do { // compound statement is the loop body
        j += 2;
        std::cout << j << " ";
    } while (j < 9);
    std::cout << '\n';
    // common situation where do-while loop is used
    std::string s = "aba";
    std::sort(s.begin(), s.end());
    do std::cout << s << '\n'; // expression statement is the loop body
    while(std::next_permutation(s.begin(), s.end()));
}

二次

产出:

二次

代码语言:javascript
复制
4 6 8 10
aab
aba
baa

二次

另见

c同时提供文件

*。

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com