C++第3~4章:运算\程序流程结构

3 运算符

作用:用于执行代码的运算

本章我们主要讲解以下几类运算符:

在这里插入图片描述

3.1算术运算符:

作用:用于处理四则运算

算术运算符包括以下符号:

在这里插入图片描述

  • 举例:

    举例1:

    //加减乘除
    int main() {
    
    	int a1 = 10;
    	int b1 = 3;
    
    	cout << a1 + b1 << endl;
    	cout << a1 - b1 << endl;
    	cout << a1 * b1 << endl;
    	cout << a1 / b1 << endl;  //两个整数相除结果依然是整数
    
    	int a2 = 10;
    	int b2 = 20;
    	cout << a2 / b2 << endl; 
    
    	int a3 = 10;
    	int b3 = 0;
    	//cout << a3 / b3 << endl; //报错,除数不可以为0
    
    	//两个小数可以相除
    	double d1 = 0.5;
    	double d2 = 0.25;
    	cout << d1 / d2 << endl;
    
    	system("pause");
    
    	return 0;
    }
    

    举例2:

    //取模
    int main() {
    
    	int a1 = 10;
    	int b1 = 3;
    
    	cout << 10 % 3 << endl;
    
    	int a2 = 10;
    	int b2 = 20;
    
    	cout << a2 % b2 << endl;
    
    	int a3 = 10;
    	int b3 = 0;
    
    	//cout << a3 % b3 << endl; //取模运算时,除数也不能为0
    
    	//两个小数不可以取模
    	double d1 = 3.14;
    	double d2 = 1.1;
    
    	//cout << d1 % d2 << endl;
    
    	system("pause");
    
    	return 0;
    }
    

    举例3:

    //递增
    int main() {
    
    	//后置递增
    	int a = 10;
    	a++; //等价于a = a + 1
    	cout << a << endl; // 11
    
    	//前置递增
    	int b = 10;
    	++b;
    	cout << b << endl; // 11
    
    	//区别
    	//前置递增先对变量进行++,再计算表达式
    	int a2 = 10;
    	int b2 = ++a2 * 10;
    	cout << b2 << endl;
    
    	//后置递增先计算表达式,后对变量进行++
    	int a3 = 10;
    	int b3 = a3++ * 10;
    	cout << b3 << endl;
    
    	system("pause");
    
    	return 0;
    }
    

3.2 赋值运算符:

作用:用于将表达式的值赋给变量

赋值运算符包括以下几个符号:

在这里插入图片描述

  • code:

    int main() {
    
    	int a = 10;
    	int b = 20;
    
    	cout << (a == b) << endl; // 0 
    
    	cout << (a != b) << endl; // 1
    
    	cout << (a > b) << endl; // 0
    
    	cout << (a < b) << endl; // 1
    
    	cout << (a >= b) << endl; // 0
    
    	cout << (a <= b) << endl; // 1
    	
    	system("pause");
    
    	return 0;
    }
    

比较运算符:

作用:用于表达式的比较,并返回一个真值或假值

比较运算符有以下符号:

在这里插入图片描述

  • code

    int main() {
    
    	int a = 10;
    	int b = 20;
    
    	cout << (a == b) << endl; // 0 
    
    	cout << (a != b) << endl; // 1
    
    	cout << (a > b) << endl; // 0
    
    	cout << (a < b) << endl; // 1
    
    	cout << (a >= b) << endl; // 0
    
    	cout << (a <= b) << endl; // 1
    	
    	system("pause");
    
    	return 0;
    }
    

3.4 逻辑运算符:
作用:用于根据表达式的值返回真值或假值

逻辑运算符有以下符号:

在这里插入图片描述

  • code

    示例1:逻辑非

    //逻辑运算符  --- 非
    int main() {
    
    	int a = 10;
    
    	cout << !a << endl; // 0
    
    	cout << !!a << endl; // 1
    
    	system("pause");
    
    	return 0;
    }
    

    逻辑与:

    //逻辑运算符  --- 与
    int main() {
    
    	int a = 10;
    	int b = 10;
    
    	cout << (a && b) << endl;// 1
    
    	a = 10;
    	b = 0;
    
    	cout << (a && b) << endl;// 0 
    
    	a = 0;
    	b = 0;
    
    	cout << (a && b) << endl;// 0
    
    	system("pause");
    
    	return 0;
    }
    

    逻辑或:

    //逻辑运算符  --- 或
    int main() {
    
    	int a = 10;
    	int b = 10;
    
    	cout << (a || b) << endl;// 1
    
    	a = 10;
    	b = 0;
    
    	cout << (a || b) << endl;// 1 
    
    	a = 0;
    	b = 0;
    
    	cout << (a || b) << endl;// 0
    
    	system("pause");
    
    	return 0;
    }
    

4 程序流程结构

C/C++支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构

  • 顺序结构:程序按顺序执行,不发生跳转
  • 选择结构:依据条件是否满足,有选择的执行相应功能
  • 循环结构:依据条件是否满足,循环多次执行某段代码

4.1 选择结构:

4.1.1 if语句

**作用:**执行满足条件的语句

if语句的三种形式:

  • 单行格式if语句
  • 多行格式if语句
  • 多条件的if语句

1.单行格式if语句:if(条件){ 条件满足执行的语句 }

2.多行格式if语句:if(条件){ 条件满足执行的语句 }else{ 条件不满足执行的语句 };

3.多条件的if语句:if(条件1){ 条件1满足执行的语句 }else if(条件2){条件2满足执行的语句}… else{ 都不满足执行的语句}

  • code:

    int main() {
    
    	int score = 0;
    
    	cout << "请输入考试分数:" << endl;
    
    	cin >> score;
    
    	if (score > 600)
    	{
    		cout << "我考上了一本大学" << endl;
    	}
    	else if (score > 500)
    	{
    		cout << "我考上了二本大学" << endl;
    	}
    	else if (score > 400)
    	{
    		cout << "我考上了三本大学" << endl;
    	}
    	else
    	{
    		cout << "我未考上本科" << endl;
    	}
    
    	system("pause");
    
    	return 0;
    }
    

4.1.2 三目运算符

作用: 通过三目运算符实现简单的判断

语法:表达式1 ? 表达式2 :表达式3

解释:

如果表达式1的值为真,执行表达式2,并返回表达式2的结果;

如果表达式1的值为假,执行表达式3,并返回表达式3的结果。

总结:和if语句比较,三目运算符优点是短小整洁,
缺点是如果用嵌套,结构不清晰。

  • code:

    int a = 10;
    int b = 20;
    int c = 0;
    
    c = a > b ? a : b;
    
    //C++中三目运算符返回的是变量,可以继续赋值
    

    在这里插入图片描述

    三目运算符可以转换成如下的if-else的形式:

    在这里插入图片描述

4.1.3 switch语句

作用:执行多条件分支语句

语法

switch(表达式)

{

	case 结果1:执行语句;break;

	case 结果2:执行语句;break;

	...

	default:执行语句;break;

}
  • code:

    int main() {
    
    	//请给电影评分 
    	//10 ~ 9   经典   
    	// 8 ~ 7   非常好
    	// 6 ~ 5   一般
    	// 5分以下 烂片
    
    	int score = 0;
    	cout << "请给电影打分" << endl;
    	cin >> score;
    
    	switch (score)
    	{
    	case 10:
    	case 9:
    		cout << "经典" << endl;
    		break;
    	case 8:
    		cout << "非常好" << endl;
    		break;
    	case 7:
    	case 6:
    		cout << "一般" << endl;
    		break;
    	default:
    		cout << "烂片" << endl;
    		break;
    	}
    
    	system("pause");
    
    	return 0;
    }
    

! :switch语句中表达式类型只能是整形或字符型

! :case里如果没有break,那么程序会一直向下执行

! :switch便于多条件判断,而缺点在于其不可以判断区间

4.2 循环结构

4.2.1 while 循环语句(功能:猜数字)

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

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

**解释:**只要循环条件的结果为真,就执行循环语句

  • code :

    int main() {
    
    	int num = 0;
    	
    	while (num < 10)
    	{
    		cout << "num = " << num << endl;
    		num++;
    	}
    	
    	system("pause");
    
    	return 0;
    }
    
  • 猜数字:

    #include<ctime>
    #include <stdlib.h>
    #include <iostream>
    int main()
    {
    	srand((unsigned int)time(NULL));
    	int num = rand() % 100 + 1;
    
    	int val;
    	while (1)
    	{
    		std::cin >> val;
    		if (val > num)
    		{
    			std::cout << "猜测过大" << std::endl;
    		}
    		else if (val < num)
    		{
    			std::cout << "猜测过小" << std::endl;
    		}
    		else
    		{
    			std::cout << "恭喜您猜正确了,奖励狗屎一份" << std::endl;
    			break;
    		}
    	}
    	system("paise");
    	return 0;
    }
    

4.2.2 do…while 循环语句

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

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

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

在这里插入图片描述

  • code:

    int main() {
    
    	int num = 0;
    
    	do
    	{
    		cout << num << endl;
    		num++;
    
    	} while (num < 10);
    	
    	
    	system("pause");
    
    	return 0;
    }
    

    水仙花数:

    int main() {
    	int num = 100;
    	do
    	{
    		int a = 0;   //个位数字
    		int b = 0;   //十位数字
    		int c = 0;   //百位数字
    		a = num % 10; //获取各位数字
    		b = num / 10 % 10;  //获取十位数字
    		c = num / 100;      //获取百位数字
    		if (a*a*a+b*b*b+c*c*c==num)
    		{
    			cout << num << endl;
    		}
    		
    
    		num++;
    	} while (num < 1000);
    	system("pause");
    	return 0;
    
    }
    

4.2.3 for循环语句

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

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

int main() {

	for (int i = 0; i < 10; i++)
	{
		cout << i << endl;
	}
	
	system("pause");

	return 0;
}

在这里插入图片描述

执行顺序:0123123123…

  • code:

    敲桌子:

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

    int main()
    {
    	int i, a,b;
    	for (i = 1; i <= 100; i++)
    	{
    		a = i % 10;        //求出个位数
    		b = i / 10 % 10;   //求出十位数
    
    		if (a == 7 || b == 7 || i % 7 == 0)
    		{
    			cout << "敲桌子" << endl;
    		}
    		else
    		{
    			cout << i << endl;
    		}
    	}
    
    	system("pause");
    	return 0;
    
    }
    

4.2.4 嵌套循环:(乘法口诀表)

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

例如我们想在屏幕中打印如下图片,就需要利用嵌套循环

  • 案例:

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QMaCo2Tr-1642679660843)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/ffa303da-b2a2-4c33-8d4b-3cb80df3b05a/Untitled.png)]

    int main() {
    
    	//外层循环执行1次,内层循环执行1轮
    	for (int i = 0; i < 10; i++)
    	{
    		for (int j = 0; j < 10; j++)
    		{
    			cout << "*" << " ";
    		}
    		cout << endl;
    	}
    
    	system("pause");
    
    	return 0;
    }
    

    2.乘法口诀表:

    int main()
    {
    	for (int i = 1; i <= 9; i++)       //行数
    	{
    		for (int j = 1; j <= i; j++)     //列数,列数<=行数
    		{
    			cout <<   j  << "*" << i << " = "  << j*i <<"  "   ;    //列数*行数=结果
    		}
    		cout<< endl;
    	}
    	system("pause");
    	return 0;
    }
    

4.3 跳转语句

4.3.1 break语句

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

break使用的时机:

  • 出现在switch条件语句中,作用是终止case并跳出switch

  • 出现在循环语句中,作用是跳出当前的循环语句

  • 出现在嵌套循环中,跳出最近的内层循环语句

  • 案例:

    int main() {
    	//1、在switch 语句中使用break
    	cout << "请选择您挑战副本的难度:" << endl;
    	cout << "1、普通" << endl;
    	cout << "2、中等" << endl;
    	cout << "3、困难" << endl;
    
    	int num = 0;
    
    	cin >> num;
    
    	switch (num)
    	{
    	case 1:
    		cout << "您选择的是普通难度" << endl;
    		break;
    	case 2:
    		cout << "您选择的是中等难度" << endl;
    		break;
    	case 3:
    		cout << "您选择的是困难难度" << endl;
    		break;
    	}
    
    	system("pause");
    
    	return 0;
    }
    
    int main() {
    	//2、在循环语句中用break
    	for (int i = 0; i < 10; i++)
    	{
    		if (i == 5)
    		{
    			break; //跳出循环语句
    		}
    		cout << i << endl;
    	}
    
    	system("pause");
    
    	return 0;
    }
    
    int main() {
    	//在嵌套循环语句中使用break,退出内层循环
    	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语句

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

  • 案例:

    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语句时,会跳转到标记 的位置

  • 案例:

    int main() {
    
    	cout << "1" << endl;
    
    	goto FLAG;
    
    	cout << "2" << endl;
    	cout << "3" << endl;
    	cout << "4" << endl;
    
    	FLAG:
    
    	cout << "5" << endl;
    	
    	system("pause");
    
    	return 0;
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值