之前在博文中说过do while(0)的用法, 不过实际项目中用得不多。 最近遇到麻烦了, 感觉到不用do while(0)就很蛋疼。

先来看一段比较常规的丑代码:

#include<iostream>
using namespace std;int fun(int a, int b, int c, int d)
{int errCode = 0;if(a < 0){errCode = -1;printf("error1\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r1 = 0;if(b < 0){errCode = -2;printf("error2\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r2 = 0;if(c < 0){errCode = -3;printf("error3\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r3 = 0;if(d < 0){errCode = -4;printf("error4\n");printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;}int r4 = 0;// add normal business logic hereprintf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;
}int main()
{fun(-1, -1, -1, -1);return 0;
}

我们可以看到, 代码存在极大的冗余, 一坨一坨的, 像什么样!  于是, 有的人说用goto可以搞定啊。 且不说goto为人诟病已久, 先来看看goto的效果:

#include<iostream>
using namespace std;int fun(int a, int b, int c, int d)
{int errCode = 0;if(a < 0){errCode = -1;printf("error1\n");goto END;}int r1 = 0;if(b < 0){errCode = -2;printf("error2\n");goto END;}int r2 = 0;if(c < 0){errCode = -3;printf("error3\n");goto END;}int r3 = 0;if(d < 0){errCode = -4;printf("error4\n");goto END;}int r4 = 0;// add normal business logic hereEND:printf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;
}int main()
{fun(-1, -1, -1, -1);return 0;
}

经验比较丰富的同学一眼就可以看出问题, 这里存在编译问题。 在linux GCC下提示:crosses initialization of xxx,  在Windows VC++下提示:initialization of 'r4' is skipped by 'goto END'

那怎么办呢? 也不是完全么有办法, 方法1是把所有被跳过的变量放在最前面定义, 方法2是直接用大括号把被掉过的变量括起来。 在实际中, 这两种方法我都玩过。 第一种方法非常繁琐(当时搞一个安全问题, 写代码的时候用了这个方式), 要重新移动变量的位置, 而第二种方法显得很猥琐(当时是搞一个节日活动的问题, 写代码的时候用了这种方式), 谁用谁知道。

还是来看看do while(0)的实现方式吧, 我最近就是这么搞的, 相对较爽:

#include<iostream>
using namespace std;int fun(int a, int b, int c, int d)
{int errCode = 0;do {if(a < 0){errCode = -1;printf("error1\n");break;}int r1 = 0;if(b < 0){errCode = -2;printf("error2\n");break;}int r2 = 0;if(c < 0){errCode = -3;printf("error3\n");break;}int r3 = 0;if(d < 0){errCode = -4;printf("error4\n");break;}int r4 = 0;}while(0);// add normal business logic hereprintf("code xxx, err code is %d\n", errCode);printf("code yyy, err code is %d\n", errCode);printf("code zzz, err code is %d\n", errCode);return errCode;
}int main()
{fun(-1, -1, -1, -1);return 0;
}

搞定!

连goto都解决不了的问题, 那就用do while(0)来解决吧!相关推荐

  1. 解决git下载出现:Failed to connect to 127.0.0.1 port 1080: Connection refused拒绝连接错误

    解决git下载出现:Failed to connect to 127.0.0.1 port 1080: Connection refused拒绝连接错误 文章目录: 一.git拒绝连接原因分析 二.错 ...

  2. oracle忽略除数为0,ora-01476除数为0的解决办法,oracle中decode()的使用

    执行报表查询,固定的一个时间点没有数据,应该是有数据的,把前一个时间节点往前调是有的,往后调也有 bebug报错 把sql放到数据库中,发现提示如截图 ====================== 解 ...

  3. Spring-bean的循环依赖以及解决方式___Spring源码初探--Bean的初始化-循环依赖的解决

    本文主要是分析Spring bean的循环依赖,以及Spring的解决方式. 通过这种解决方式,我们可以应用在我们实际开发项目中. 什么是循环依赖? 怎么检测循环依赖 Spring怎么解决循环依赖 S ...

  4. 有关阿里云服务器ping不通解决办法(云服务器搭建完环境访问不了ip解决办法)

    阿里云服务器ping不通解决办法(云服务器搭建完环境访问不了ip解决办法) 问题:这里的服务器我以阿里云为例,学生套餐嘛,便宜,最近搞服务器,然后Ubuntu16.04下搭建LAMP环境后发现输入ip ...

  5. 蓝牙耳机芯片,苹果弹窗电量显示为0的解决方法

    之前的蓝牙耳机重新都是默认电量为100%,但是后来添加了充电仓模块的程序之后,在选择使用一般充电仓的时候,手机弹窗就会0%,解决方法如下: 更新之后,我们能够发现原厂在之前的基础上进行了更深一步的封装 ...

  6. 计算机无效d盘的错误怎么解决,在电脑中安装软件时提示无效驱动器D的解决方法...

    电脑一打开IE浏览器就弹出广告怎么办?解决电脑打开网页总是弹出广告的方法 随着互联网技术的应用与普及,我们实现了不出门即可知天下事,但与此同时,网络上充斥着越来越多的广告.用户反映一打开IE浏览器便会 ...

  7. 万维考试系统检测不到VC++ 6.0环境解决办法

    万维考试系统检测不到VC++ 6.0环境解决办法 在大家使用万维考试系统的时候会发现提示检测不到VC++ 6.0环境 此时就需要这样做了 下载一个企业版VC++6.0 (下载链接https://dow ...

  8. aspose-words-15.8.0 完美解决word转pdf

    aspose-words-15.8.0 完美解决word转pdf 1.写在前面 2.集成aspose-words 3.核心的一步,是时候让word转pdf了! 4.打包会遇到的一点小坑 1.写在前面 ...

  9. 奇迹mu游戏服务端提高灵魂和生命的成功率解决GS不掉祝福宝石/灵魂宝石/玛雅之石/洛克之羽解决GS不掉生命宝石修改方法

    奇迹mu游戏服务端提高灵魂和生命的成功率解决GS不掉祝福宝石/灵魂宝石/玛雅之石/洛克之羽解决GS不掉生命宝石修改方法 我是艾西,今天的内容对于懂技术的朋友可以作为参考 提高灵魂和生命的成功率 均在v ...

  10. mysql中除数为0怎么解决_ora-01476除数为0的解决办法,oracle中decode()的使用

    执行报表查询,固定的一个时间点没有数据,应该是有数据的,把前一个时间节点往前调是有的,往后调也有 bebug报错 把sql放到数据库中,发现提示如截图 ====================== 解 ...

最新文章

  1. 第62天:手风琴效果
  2. css实现自适应背景图及背景图透明度处理
  3. php程序内存优化之数组操作优化
  4. SVM-支持向量机原理详解与实践之四
  5. java处理excel(java使用Apache POI处理Excel)
  6. 详细易懂的二叉树遍历(先中后)
  7. 【面试必备】通俗易懂的XGBoost、Transformer、BERT、水波网络原理解析 | 学习小组...
  8. 一步步编写操作系统 23 重写主引导记录mbr
  9. ironpython最新版本_Microsoft IronPython2.7.5 最新版
  10. linux下tomcat启动后出现多个java进程
  11. 计算机软件实习项目四 —— 校园一卡通管理系统 (实验准备) 12-27
  12. 后台UI框架bootstrap、JQuery——Hplus-v.4.1.0
  13. pdf转换html乱码怎么办,pdf转word后乱码怎么办?
  14. 交换机端口镜像配置大全【汇集22个各种品牌交换机】
  15. 四核网络机顶盒芯片局势分析(开放市场):rk3128将会成为四核主流
  16. 数据中台,什么是数据中台?
  17. vlan的tagged、untagged
  18. 医保卡和社保卡的区别
  19. open-falcon原理以及使用(不断更新)
  20. 澤山咸 (易經大意 韓長庚)

热门文章

  1. C++中for循环语句简析
  2. 【python】有名元祖
  3. 滴答滴答滴答滴答滴答滴答滴答滴答滴答
  4. Prometheus+Grafana+钉钉部署一个单机的MySQL监控告警系统
  5. luogu 1909
  6. noip2017普及 兔纸游玩记
  7. 无线ldap认证服务器,结合LDAP服务器进行portal认证配置案例
  8. shell脚本实战:
  9. 关于CSDN网站打不开,全是文字没有图片,其他网站却是很正常的解决方案
  10. 2022流动式起重机司机试题及答案