如果 while 循环至少运行一次,执行代码的最有效方法是什么?

What is the most efficient way to execute code if a while loop runs at least once?

我有一个 while 循环,如果 while 循环至少一次 运行,我希望某行代码为 运行。如果while循环不会运行,我想跳过这行代码

while(condition) {
  doSomething();
}
doSomethingElse(); 
/* Only run doSomethingElse() if the while loop ran
at least once */

我可以在 运行s 之前将 bool 设置为 false,并在 运行s 时将其设置为 true,但对我来说感觉有点乱。另外,这个 while 循环在一个最多可以 运行 10^6 次的函数中,它有时间限制,所以我希望它尽可能高效地 运行。有什么办法吗?

*注意:不要将此与 do-while 循环混淆,后者无论如何至少 运行s 一次。我想要一行代码,仅当 while 循环至少 运行 一次时才 运行s。

你的意思好像是

if ( condition )
{
    while(condition) {
       doSomething();
    }
    doSomethingElse(); 
}

或者

if ( bool b = condition )
{
    while( b) {
       doSomething();
       b = condition;
    }
    doSomethingElse(); 
}

if ( condition )
{
    do {
       doSomething();
    } while ( condition );
    doSomethingElse(); 
}
int count=0;
while(condition) {
    doSomething();
    count=count+1;
}if(count >=1) {
    // Enter the block of code that you want to run at least once }