定义 :一个或几个循环放在另一个循环体内。
案例:
生成实心直角三角形 :
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int i = 1, n = 1,m; 6 cout << "请输入你所需要生成的直角三角形的高" << endl; 7 cin >> m; 8 cout << endl; 9 while ( n <= m) 10 { 11 for (int i = 1; i <= n; i++) 12 { 13 cout << "*"; 14 } 15 n++; 16 cout << endl; 17 } 18 return 0; 19 }

生成九九乘法表:
1 #include <iostream> 2 using namespace std; 3 int main() 4 { 5 int i = 1; 6 while (i <= 9) 7 { 8 for (int n = 1; n <= i; n++) 9 { 10 cout << n << "*" << i << "=" << n * i << " "; 11 } 12 cout << endl; 13 i++; 14 } 15 }
1 #include<iostream> 2 using namespace std; 3 int main() { 4 for (int a = 1; a <= 9; a++) 5 { 6 for (int b = 1; b <= a; b++) 7 { 8 cout << a << "*" << b << "=" << a * b << " "; 9 } 10 cout << endl; 11 } 12 return 0; 13 }

浙公网安备 33010602011771号