当前位置:网站首页>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;
}
边栏推荐
- Tensorflow realizes verification code recognition (II)
- Jvm-08-garbage collector
- Using notepad++ to build an arbitrary language development environment
- Second kill system 3 - list of items and item details
- Popular understanding of linear regression (I)
- 视觉上位系统设计开发(halcon-winform)-2.全局变量设计
- 基础SQL教程
- The state does not change after the assignment of El switch
- Mysql报错:[ERROR] mysqld: File ‘./mysql-bin.010228‘ not found (Errcode: 2 “No such file or directory“)
- 开启 Chrome 和 Edge 浏览器多线程下载
猜你喜欢
函数栈帧的创建和销毁
[Yu Yue education] scientific computing and MATLAB language reference materials of Central South University
redis缓存穿透,缓存击穿,缓存雪崩解决方案
Detailed pointer advanced 2
[set theory] inclusion exclusion principle (complex example)
Redis cache penetration, cache breakdown, cache avalanche solution
Final review points of human-computer interaction
Jvm-06-execution engine
mysql innodb 存储引擎的特性—行锁剖析
视觉上位系统设计开发(halcon-winform)-4.通信管理
随机推荐
Characteristics of MySQL InnoDB storage engine -- Analysis of row lock
官网MapReduce实例代码详细批注
socket.io搭建分布式Web推送服务器
视觉上位系统设计开发(halcon-winform)
High quality workplace human beings must use software to recommend, and you certainly don't know the last one
Atlas atlas torque gun USB communication tutorial based on mtcom
【云原生训练营】模块八 Kubernetes 生命周期管理和服务发现
需要知道的字符串函数
Basic SQL tutorial
【云原生训练营】模块七 Kubernetes 控制平面组件:调度器与控制器
运维体系的构建
Reentrantlock usage and source code analysis
Calibre LVL
Stress test WebService with JMeter
Qt常用语句备忘
C语言刷题~Leetcode与牛客网简单题
阿特拉斯atlas扭矩枪 USB通讯教程基于MTCOM
Unity hierarchical bounding box AABB tree
Nppexec get process return code
Idea does not specify an output path for the module