C/C++学习笔记 基础入门系列(八)

C/C++基础入门系列

写在前面的话

C++学了一段时间了,现在在整理笔记,作为备份和分享,会一点点的连载上来,如果此时的你在看这篇文章,而且想学C/C++,本篇系列文章或许能帮助你打基础,为了方便大家,代码我也直接码在上面了,如果复制粘贴都懒得做的话,过段时间我会把整理好的代码打包传到下载里面。

目录

第一部分 HelloWorld的起点
第二部分 初探数据类型(一)
第三部分 初探数据类型(二)
第四部分 认识运算符(一)
第五部分 认识运算符(二)
第六部分 理清思路,写好逻辑结构(一)
第七部分 理清思路,写好逻辑结构(二)
第八部分 理清思路,写好逻辑结构(三)
第九部分 数据处理神器之数组(一)
第十部分 数据处理神器之数组(二)
第十一部分 写好函数,学会功能复用(一)
第十二部分 写好函数,学会功能复用(二)

第八部分 理清思路,写好逻辑结构(三)

4.2.2 do…while循环语句

作用: man许循环脑健,执行循环语句

语法: do{ 循环语句 }while(循环条件)

注意: 与while区别在于do…while会先执行一次循环语句,再判断循环条件

Created with Raphaël 2.2.0 开始 执行语句 循环条件 结束 yes no

示例:

#include<iostream>
using namespace std;

// 显示数字0~9

int main() {

	int num = 0;

	do {
		cout << num << endl;
	} while (++num <= 9);

	system("pause");
	return 0;
}

练习案例: 水仙花数

水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身

例如:13+53+33=153

请利用do…while语句求出所有3位数中的水仙花数

#include<iostream>
using namespace std;

int main() {

	int num = 100;

	do {
		int a = num / 100;
		int b = num % 100 / 10;
		int c = num % 10;
		if (a*a*a + b*b*b + c*c*c == num) {
			cout << num << endl;
		}
	} while (++num < 1000);

	system("pause");
	return 0;
}
4.2.3 for循环语句

作用: 满足循环条件,执行循环语句

语法: for(起始表达式;条件表达式; 末尾循环体;){ 循环语句; }

Created with Raphaël 2.2.0 开始 起始表达式 条件表达式 循环语句 末尾循环体 结束 yes no

示例:

#include<iostream>
using namespace std;

int main() {

	for (int i = 0; i <= 9; i++) {
		cout << i << endl;
	}

	system("pause");
	return 0;
}

练习案例:敲桌子

从1开始到100,如果数字个位含有7忙活着十位含有7,或者该数字是7的整数倍,我们打印敲桌子,其余数字直接输出。

在这里插入图片描述

#include<iostream>
using namespace std;

int main() {

	for (int n = 1; n <= 100; n++) {
		if ((n % 10 == 7) || (n / 10 == 7) || (n % 7 == 0)) {
			cout << "敲桌子" << endl;
		}
		else {
			cout << n << endl;
		}
	}

	system("pause");
	return 0;
}
4.2.4 嵌套循环

作用: 再循环体中再嵌套一层循环,解决一些实际问题

示例: 打印10*10的 * 图案

#include<iostream>
using namespace std;

int main() {

	for (int i = 0; i <= 9; i++) { // 外层循环
		
		for (int j = 0; j <= 9; j++) { // 内层循环
			
			cout << "* ";	// 每行打印10个星号

		}
		cout << endl;
	}

	cout << endl;

	system("pause");
	return 0;
}

练习案例: 九九乘法口诀表

#include<iostream>
using namespace std;

int main() {

	for (int i = 1; i <= 9; i++) {

		for (int j = 1; j <= 9; j++) {
			cout << j << " x " << i << " = " << i * j << "\t";
			if (i == j) {
				cout << endl;
				break;	// 跳转语句,下一节会讲到
			}
		}
	}

	system("pause");
	return 0;
}

4.3 跳转语句

4.3.1 break语句

作用: 用于跳出选择结构循环结构

break使用的时机:

  • 出现在switch条件语句中,作用是终止case并跳出switch
  • 出现在循环语句中,作用是跳出当前循环语句
  • 出现在嵌套循环中,跳出最近的内层循环语句

示例1:

#include<iostream>
using namespace std;
int main() {
	cout << "请选择副本难度:" << endl;
	cout << "1、普通" << endl;
	cout << "2、中等" << endl;
	cout << "3、困难" << endl;

	int level = 0;
	cin >> level;
	

	cout << "你选择的难度为:" << endl;
	switch (level) {
	case 1:
		cout << "普通" << endl; break;
	case 2:
		cout << "中等" << endl; break;
	case 3:
		cout << "困难" << endl; break;
	default:
		cout << "输入有误!" << endl; break;
	}

	system("pause");
	return 0;
}

示例2:

#include<iostream>
using namespace std;
int main() {
	for (int i = 0; i < 10; i++) {
		if (i == 5) {
			break;
		}
		cout << i << endl;
	}

	system("pause");
	return 0;
}

示例3:

#include<iostream>
using namespace std;

int main() {

	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			if (j == 5) {
				break;	// 退出内层循环
			}
			cout << "* ";
		}
		cout << endl;
	}

	system("pause");
	return 0;
}
4.3.2 continue语句

作用: 在循环语句中,跳过本次循环中余下尚未执行的语句,继续执行下一次循环

示例:

# include<iostream>
using namespace std;

int main() {
	for (int i = 0; i < 100; i++) {
		if (i % 2 == 0) {	// 只输出奇数
			continue;
		}
		cout << i << endl;
	}
	system("pause");
	return 0;
}
4.3.3 goto语句

作用: 可以无条件跳转语句

语法: goto 标记;

解释: 如果标记名称存在,执行到goto语句时,会跳转到标记位置

示例:

#include<iostream>
using namespace std;

int main() {

	cout << "1" << endl;

	goto FLAG;

	cout << "2" << endl;
	cout << "3" << endl;
	cout << "4" << endl;

FLAG:

	cout << "5" << endl;

	system("pause");
	return 0;
}

注意:在程序中不建议使用goto语句,以免造成流程混乱

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值