码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 58.【c/c++优先级详解】


    文章目录

    • (一)、什么是优先级?
      • (二)、优先级表格都有什么?
        • (三)、经典优先级举列.
          • 1.1 前置++和后置++是什么?
          • 1.2前置++后置++在判断语句中.
          • 1.3前置++后置++在数据类型中.
          • 1.4前置++后置++在输出语句中.
          • 1.5前置++和前置++同在赋值语句中
          • 1.6后置++和后置++同在赋值语句中
          • 1.7后置++和后置++同在赋值语句中
          • 前置++和后置++,单个赋值
        • (四)总结.

    (一)、什么是优先级?

    优先级是指计算机分时操作系统在处理多个作业程序的时候,决定各个作业程序接收系统调用的先后。优先级越高、先调用。

    (二)、优先级表格都有什么?

    	![在这里插入图片描述](https://img-blog.csdnimg.cn/349a3421ff8243dea1bd9e7b27eb6a72.jpeg#pic_center)
    
    • 1

    (三)、经典优先级举列.

    本次我们以前置++和后置++进行详细讲说:

    1.1 前置++和后置++是什么?

    前置++:是先自增,然后再进行表达式.
    后置++:是先进性表达式,再进行自增.

    1.2前置++后置++在判断语句中.

    后置++在判断语句中:在if语句中a++的值仍然为5,因为先进行表达式里面的判断语句得出来不满足,所以进入else语句,此时a=6,又+1所以结果为7.

    在这里插入代码片#include <iostream>
    #include 
    using namespace std;
    int main()
    {
    	int a = 5;
    	if (a++ > 5)
    		cout << a << endl;
    	else
    	{
    		cout << a+1;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述
    前置++在判断语句中: 先进性自增,然后再判断的出来if为真输出a

    在这里插入代码片#include <iostream>
    #include 
    using namespace std;
    int main()
    {
    	int a = 5;
    	if (++a > 5)
    		cout << a << endl;
    	else
    	{
    		cout << a+1;
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在这里插入图片描述

    1.3前置++后置++在数据类型中.

    后置++在数据类型中:直接运行.

    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a = 5;
    	a++;
    	cout << a << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    前置++在数据类型中:依然直接输出.

    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a = 5;
    	++a;
    	cout << a << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    1.4前置++后置++在输出语句中.

    后置++在输出语句中:因为a默认优先级为+,+的优先级小于++,所以我们先运行后面的后置++,又因为后置++先进性表达式,所以第二个a仍然为5,其余的为6.

    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a = 5;
    	cout << a << " " << a++ << endl;
    	cout << a << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述
    输出语句中的前置++: 因为先进性前置++,所以结果都为6

    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a = 5;
    	cout << a << " " << ++a << endl;
    	cout << a << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在这里插入图片描述

    1.5前置++和前置++同在赋值语句中

    ()的优先级高于+、先执行两个小括号内的++,第一次++x的时候为5,放在储存区,第二次为6,所以两个结果都为6;

    #include 
    using namespace std;
    int main()
    {
    	int a = 4,b;
    	b =  (++a)+(++a);
    	cout << b << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    1.6后置++和后置++同在赋值语句中

    先存值、后输出

    #include 
    using namespace std;
    int main()
    {
    	int a = 4,b;
    	b =  (a++)+(a++);
    	cout << b << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    1.7后置++和后置++同在赋值语句中

    后置++优先级高于前置++优先级,所以后置++先运行为4,前置++在5的基础上再+1为6.

    #include 
    using namespace std;
    int main()
    {
    	int a = 4,b;
    	b =  (a++)+(++a);
    	cout << b << endl;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在这里插入图片描述

    前置++和后置++,单个赋值

    后置++,先运算再++

    #include 
    #include 
    using namespace std;
    int main()
    {
    	int a=5,b;
    	b = a++;
    	cout << b << endl;
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    (四)总结.

    一定要看好优先级再进行数据判断!

  • 相关阅读:
    Day06-filebeat,logstash多实例,pipline,ElasticStack项目架构梳理及实战案例
    中国光刻胶行业现状动态与投资前景展望报告2022-2028年
    vue3 setup展示数据
    用Nginx搭建一个具备缓存功能的反向代理服务
    不知道文字转语音软件哪个好?推荐几款实用软件
    【Linux】进程
    【软件测试】自动化测试selenium(一)
    OpenWRT设置自动获取IP,作为二级路由器
    参数优化文档介绍
    Unity 使用宏
  • 原文地址:https://blog.csdn.net/qq_69683957/article/details/126133619
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号