Loops 是否可以在while循环的条件和主体之间插入代码?

Loops 是否可以在while循环的条件和主体之间插入代码?,loops,syntax,while-loop,Loops,Syntax,While Loop,是否可以执行以下代码中所示的操作(任何语言) 不,不可能,至少在我所知道的任何语言中是这样(这涵盖了很多语言) 要模拟相同的效果,请执行以下操作: bool firstTime = true; while (condition) { if (firstTime) { // do initialization here firstTime = false } // the rest of your loop stuff here }

是否可以执行以下代码中所示的操作(任何语言)


不,不可能,至少在我所知道的任何语言中是这样(这涵盖了很多语言)

要模拟相同的效果,请执行以下操作:

bool firstTime = true;
while (condition)
{
    if (firstTime)
    {
        // do initialization here
        firstTime = false
    }
    //  the rest of your loop stuff here
}

这将满足您的需要,但在性能方面并不完全相同,因为循环体中还存在其他比较。

不,我从未见过任何语言

不,这至少在java中是不可能的

while(Condition)
// You can't do anything here
{
// This is the While Loop body
}

不,不可能在while循环之后和while循环主体之前初始化变量或任何东西。如果您愿意的话

boolean a;
while(condition)
   a=false;
{
//body of loop
}
然后
a=false
将成为while循环的主体,并且

{
//body of loop
}

不会成为while循环的一部分。

因为您尚未指定语言

在Common Lisp和Emacs Lisp中,宏最初支持一个
子句,该子句可以执行此处所需的操作

(loop while (my-predicate)
      initially (perform-setup)
      do (my-function))
此子句在循环外执行(即仅执行一次)。

您可以通过

for(int i=0,x=1,c=3;print(i,x,c),i<15;i++)
{
//use i,x,c here ,where they are initialized once
}

for(inti=0,x=1,c=3;print(i,x,c),你可以在那里写,但是后面的块不会被认为是循环体。它是有效的,除了它不是while循环,这是(当前)问题所在。
for(int i=0,x=1,c=3;print(i,x,c),i<15;i++)
{
//use i,x,c here ,where they are initialized once
}