当前位置:网站首页>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;
}
边栏推荐
- The method of parameter estimation of user-defined function in MATLAB
- Popular understanding of decision tree ID3
- Stress test WebService with JMeter
- PyTorch crop images differentiablly
- Kubernetes - yaml file interpretation
- 在MapReduce中利用MultipleOutputs输出多个文件
- Using Tengine to solve the session problem of load balancing
- [cloud native training camp] module 7 kubernetes control plane component: scheduler and controller
- Jvm-05-object, direct memory, string constant pool
- [combinatorics] permutation and combination (set permutation, step-by-step processing example)
猜你喜欢
Jvm-04-runtime data area heap, method area
Jvm-03-runtime data area PC, stack, local method stack
Kubernetes帶你從頭到尾捋一遍
Halcon and WinForm study section 2
视觉上位系统设计开发(halcon-winform)
GCC cannot find the library file after specifying the link library path
Concurrency-01-create thread, sleep, yield, wait, join, interrupt, thread state, synchronized, park, reentrantlock
Visual upper system design and development (Halcon WinForm) -1 Process node design
Can‘t connect to MySQL server on ‘localhost‘
Incluxdb2 buckets create database
随机推荐
Seckill system 3- product list and product details
百度智能云助力石嘴山市升级“互联网+养老服务”智慧康养新模式
How to use annotations such as @notnull to verify and handle global exceptions
秒杀系统2-Redis解决分布式Session问题
使用Tengine解决负载均衡的Session问题
Using Tengine to solve the session problem of load balancing
基于SVN分支开发模式流程浅析
[daily training] 395 Longest substring with at least k repeated characters
SQL server安装位置改不了
qt使用QZxing生成二维码
Kubernetes帶你從頭到尾捋一遍
Redis lock Optimization Practice issued by gaobingfa
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
【可能是全中文网最全】pushgateway入门笔记
视觉上位系统设计开发(halcon-winform)-3.图像控件
Custom annotation
官网MapReduce实例代码详细批注
需要知道的字符串函数
在MapReduce中利用MultipleOutputs输出多个文件
驱动与应用程序通信