C# 将 goto 语句替换为 if-else

本文关键字:if-else 替换 语句 goto | 更新日期: 2023-09-27 18:33:31

我正在尝试用任何其他循环替换此代码块(我想到了同时并做到了,但由于某种原因,我没有完全理解逻辑。

repeat:
   ... 
   if (condition)
   {
     goto repeat
   }
   else
   {
     ...
   }

有人可以帮助我解决这里的逻辑吗?我看到一些关于替换goto语句的帖子,但它们只依赖于一个if,没有其他的。

乱我的想法的是,if 语句中没有任何内容,只有 goto。如果我尝试将其翻译成 while 语句,它会给我留下这个:

while (condition)
{
   // don't know what goes here since there is nothing but goto in the if statement
}
// else stuff

谢谢

C# 将 goto 语句替换为 if-else

这很简单do - while循环:

do
{
    // code between "repeat:" and the if here
} while (condition);
// else code here