刚刚偶然看到其他博客上写的这个问题,感觉例子都不够精炼,而且还有说的不对的。
while先判断后执行;
do-while先执行一次再判断。
直接上例子:
#include <iostream>
int main()
{
//declare & init
int a = 10,b = 10;
//while
while (a < 10)
a += 1;
//do-while
do {
b += 1;
} while (b < 10);
//cout
std::cout << "while:"<<a <<"\ndo-while:"<<b<< std::endl;
}
运行结果