1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Loop through every number between 1 and 50
int iii = 1;
while (iii <= 50)
{
// print the number
cout << iii << " ";
// if the loop variable is divisible by 10, print a newline
if (iii % 10 == 0)
cout << endl;
// increment the loop counter
iii++;
}
它也可以嵌套循环内循环的。在下面的例子中,内环和外环的每一个都有自己的专柜。然而,请注意,内回路的表达,利用外环的计数器和!
1
2
3
4
5
6
7
8
9
10
11
12
13
// Loop between 1 and 5
int iii=1;
while (iii<=5)
{
// loop between 1 and iii
int jjj = 1;
while (jjj <= iii)
cout << jjj++;
// print a newline at the end of each row
cout << endl;
iii++;
}
This program prints: