while语句:
计数控制的循环
#include
using namespace std;
int main() {int loopcount &#61; 0;while (loopcount <&#61; 10) {cout << "hello world&#xff01;" << endl;loopcount&#43;&#43;;}system("pause");return 0;
}
事件控制的循环
#include
#include
using namespace std;
int main() {int month, day;cin >> month >> day;while (!(month &#61;&#61; 2 && day &#61;&#61; 31)) {cout << "这个月有30天" << endl;cin >> month >> day;}ifstream indata;indata.open("test.txt");int value;indata >> value;while (indata) {cout << value << endl;indata >> value;}indata.close();int sum &#61; 0, i &#61; 1;bool flag &#61; true;while (flag) {sum &#43;&#61; i;if (sum >&#61; 100)flag &#61; false;i&#43;&#43;;}cout << "sum &#61; " << sum << endl;system("pause");return 0;
}
嵌套循环
#include
#include
using namespace std;
int main() {int i &#61; 1, j &#61; 1, sum &#61; 0;while (i <&#61; 9) {while (j <&#61; 9) {sum &#61; i * j;cout << i << "x" << j << "&#61;" << sum << " ";j&#43;&#43;;}i&#43;&#43;;j &#61; i;cout << "\n";}system("pause");return 0;
}