当前位置:网站首页>do{}while()的妙用
do{}while()的妙用
2022-07-03 15:20:00 【宇龍_】
有时候在代码中经常看到do{}while(0),do{}while(false)这种看起来有点奇怪的形式,实际上在项目中使用这种方式是非常妙的,可以减少函数逻辑中很多return或者多层嵌套if...else...的形式,可以很简单的实现在函数中只有一个return,见如下代码:
#include <stdlib.h>
#include <stdio.h>
//权限校验函数
bool DoCheck(int n)
{
bool bRet = false;
do
{
if (n & 0x1)
{
bRet = true;
}
if (n & 0x2)
{
bRet = false;
break;
}
if (n & 0x4)
{
bRet = false;
break;
}
if (n & 0x10)
{
//如果没有do...while的形式,这里可能需要直接return或者外层嵌套多个if...else...
bRet = true;
break;
}
if (n & 0x20)
{
bRet = true;
}
if (n & 0x40)
{
bRet = true;
}
} while (false);
return bRet;
}
int main()
{
//权限校验
if (DoCheck(0x11))
{
printf("yes!\n");
}
else
{
printf("no\n");
}
system("pause");
return 0;
}
边栏推荐
- Solve the problem that pushgateway data will be overwritten by multiple push
- [probably the most complete in Chinese] pushgateway entry notes
- win32创建窗口及按钮(轻量级)
- Seckill system 2 redis solves the problem of distributed session
- socket.io搭建分布式Web推送服务器
- 需要知道的字符串函数
- Using multipleoutputs to output multiple files in MapReduce
- Using TCL (tool command language) to manage Tornado (for VxWorks) can start the project
- What is machine reading comprehension? What are the applications? Finally someone made it clear
- 在MapReduce中利用MultipleOutputs输出多个文件
猜你喜欢
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
[cloud native training camp] module VIII kubernetes life cycle management and service discovery
Visual upper system design and development (Halcon WinForm) -1 Process node design
Halcon and WinForm study section 1
[probably the most complete in Chinese] pushgateway entry notes
视觉上位系统设计开发(halcon-winform)-1.流程节点设计
Finally, someone explained the financial risk management clearly
解决pushgateway数据多次推送会覆盖的问题
Popular understanding of ovo and ovr
Introduction to redis master-slave, sentinel and cluster mode
随机推荐
Atlas atlas torque gun USB communication tutorial based on mtcom
视觉上位系统设计开发(halcon-winform)-6.节点与宫格
Popular understanding of linear regression (I)
qt使用QZxing生成二维码
从 flask 服务端代码自动生成客户端代码 -- flask-native-stubs 库介绍
基础SQL教程
MySQL reports an error: [error] mysqld: file '/ mysql-bin. 010228‘ not found (Errcode: 2 “No such file or directory“)
Solve the problem that pushgateway data will be overwritten by multiple push
GCC cannot find the library file after specifying the link library path
Jvm-09 byte code introduction
Besides lying flat, what else can a 27 year old do in life?
Visual upper system design and development (Halcon WinForm) -4 Communication management
【可能是全中文网最全】pushgateway入门笔记
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
The first character of leetcode sword offer that only appears once (12)
Kubernetes - yaml file interpretation
socket. IO build distributed web push server
Popular understanding of gradient descent
XWiki安装使用技巧
Using Tengine to solve the session problem of load balancing