是否可以执行以下代码中显示的内容(使用任何语言)?
while(Condition)
// can I do anything here (like initializations) between the condition and the body of the loop?
{
// while Loop body
}
答案 0 :(得分:4)
由于您尚未指定语言......
在Common Lisp和Emacs Lisp中,loop宏支持initially
子句,可以在这里做你想做的事。
(loop while (my-predicate)
initially (perform-setup)
do (my-function))
此子句在循环外执行(即只执行一次)。
答案 1 :(得分:3)
不,不可能,至少用我所知的任何语言(并且涵盖了很多语言)
模拟相同的效果:
bool firstTime = true;
while (condition)
{
if (firstTime)
{
// do initialization here
firstTime = false
}
// the rest of your loop stuff here
}
这会做你想要的,但它在性能方面并不完全相同,因为在循环体中会发生另外的比较。
答案 2 :(得分:0)
不,不是我见过的任何语言。
答案 3 :(得分:0)
至少在java
中,这是不可能的while(Condition)
// You can't do anything here
{
// This is the While Loop body
}
答案 4 :(得分:0)
不能在while循环之后和while循环之前初始化变量或任何东西。如果你喜欢
boolean a;
while(condition)
a=false;
{
//body of loop
}
然后a=false
将成为while循环体和
{
//body of loop
}
不会成为while循环的一部分。
答案 5 :(得分:0)
你可以通过
实现它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
}
<强>输出:强> print(i,x,c)将执行一次
这适用于C#