[英]While loop in C, with non-executed condition
I keep coming across the following pattern in my C code:我在 C 代码中不断遇到以下模式:
_Bool executed = 0;
while (condition) {
executed = 1;
...
}
if (!executed) {
...
}
Is there a better way to construct this?有没有更好的方法来构建它?
Ideally:理想情况下:
while (condition) {
executed = 1;
...
} else {
...
}
(A while
/ else
loop, but not with Python's semantics. The else
should be only executed if the while condition was immediately false.) (一个
while
/ else
循环,但不符合 Python 的语义。只有当 while 条件立即为假时才应该执行else
。)
It seems它似乎
_Bool executed = 0;
while (condition) {
executed = 1;
...
}
if (!executed) {
...
}
If condition
has side effects, it can be changed with如果
condition
有副作用,可以用
if (condition) {
do
{
} while(condition);
} else {
}
But if you insist only using a while
, and not a do... while
then your penalty is evaluating condition
again.但是如果你坚持只使用
while
,而不是do... while
那么你的惩罚就是再次评估condition
。
if (condition) {
while(condition)
{
}
} else {
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.