p37:嵌套循环
在循环中再嵌套一层循环,解决一些实际问题
例如我们想在屏幕中打印如下图片,就需要利用嵌套循环
打印星图:
#include<iostream> using namespace std; int main() { for (int i = 0; i < 10; i++) //外圈执行一次 { for (int j = 0; j < 10; j++) //内圈执行一周 { cout << "* "; } cout << endl; } system("pause"); return 0; }
**外层执行一次,内层执行一周
p38: 案例:乘法口诀表
利用嵌套循环:实现九九乘法表
分析打印的东西是上面: 列数*行数 = 计算结果 列数要≤当前行数
#include<iostream> using namespace std; int main() { int sum = 0; for (int i = 1; i < 10; i++) //*后面的数 { for (int j = 1; j < 10; j++) //*前面的数 { if ( i >= j) { sum = j * i; cout << j << "×" << i << "=" << sum << " "; } } cout << endl; } system("pause"); return 0; }
//分析过程特别重要:由图片中 行数和列数的关系来找出 :输出只有行数大于列数时才输出
p39: 跳转语句:break
作用:用于跳出选择结构或者循环结构
使用break的时机:
* 出现在switch条件语句中,作用是终止case并跳出switch
*出现在循环语句中,作用是跳出当前的循环语句
*出现再嵌套循环中,跳出醉经的内层循环语句(就近原则)
*补充()
p40: 跳转语句 continue
continue作用:
1.在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环。
#include<iostream> using namespace std; int main() { for(int i=0 ; i<100;i++) { if(i%2 ==0) {continue;} //这里如果满足条件将不再执行cout 操作,继续下一次循环 cont << i <<endl; } system("pause"); return 0; }
p41: goto 语句
1.语法:goto 标记: 标记用的时候; 标记:
2.如果标记的名称存在,执行到goto语句时,会跳转到标记位置
#include<iostream> using namespace std; int main() { cout << "1.xxxxxxxxx" << endl; cout << "2.xxxxxxxxx" << endl; cout << "3.xxxxxxxxx" << endl; goto FLAG; cout << "4.xxxxxxxxx" << endl; cout << "5.xxxxxxxxx" << endl; cout << "6.xxxxxxxxx" << endl; cout << "7.xxxxxxxxx" << endl; FLAG: cout << "8.xxxxxxxxx" << endl; cout << "9.xxxxxxxxx" << endl; system("pause"); return 0; } /*输出结果: 1.xxxxxxxxx 2.xxxxxxxxx 3.xxxxxxxxx 8.xxxxxxxxx 9.xxxxxxxxx 请按任意键继续. . .*/