当前位置:网站首页>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;
}
边栏推荐
- Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe
- Seckill system 2 redis solves the problem of distributed session
- Popular understanding of random forest
- Jvm-06-execution engine
- [cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
- redis单线程问题强制梳理门外汉扫盲
- Besides lying flat, what else can a 27 year old do in life?
- High quality workplace human beings must use software to recommend, and you certainly don't know the last one
- [daily training] 395 Longest substring with at least k repeated characters
- Influxdb2 sources add data sources
猜你喜欢
Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
Popular understanding of decision tree ID3
GCC cannot find the library file after specifying the link library path
Besides lying flat, what else can a 27 year old do in life?
Halcon and WinForm study section 1
Second kill system 3 - list of items and item details
Concurrency-02-visibility, atomicity, orderliness, volatile, CAS, atomic class, unsafe
Functional modules and application scenarios covered by the productization of user portraits
Finally, someone explained the financial risk management clearly
详解指针进阶2
随机推荐
Jvm-06-execution engine
Redis lock Optimization Practice issued by gaobingfa
The method of parameter estimation of user-defined function in MATLAB
互斥对象与临界区的区别
Jvm-05-object, direct memory, string constant pool
Kubernetes - YAML文件解读
Seckill system 3- product list and product details
秒杀系统3-商品列表和商品详情
基于SVN分支开发模式流程浅析
Popular understanding of random forest
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
[set theory] inclusion exclusion principle (complex example)
[transform] [practice] use pytoch's torch nn. Multiheadattention to realize self attention
Seckill system 2 redis solves the problem of distributed session
Construction of operation and maintenance system
Kubernetes - yaml file interpretation
视觉上位系统设计开发(halcon-winform)
What is label encoding? How to distinguish and use one hot encoding and label encoding?
Popular understanding of gradient descent
[combinatorics] permutation and combination (set permutation, step-by-step processing example)