var understand = true;
while(/* ... */) {
console.log("I'm learning while loops!");
understand = false;
}
我想打印“我正在循环学习!”那么什么条件需要在循环中写?
答案 0 :(得分:3)
试试这个:
while(understand){
console.log("I'm learning while loops!");
understand = false;
}
<强> EDIT1:强>
如果您希望循环运行多次:
var i=0;
while(i<10){ //suppose you want to run your loop for 10 times.
console.log("I'm learning while loops!");
i++;
}
编辑2:(回复评论中的代码)
您正在使用loop
作为函数名称,并在while循环中检查错误
试试这个:
var myFunctionName = function()
{
var myVariableName = 0;
while(myVariableName<3)
{
console.log("In loop" + myVariableName);
myVariableName++;
}
};
myFunctionName();
答案 1 :(得分:2)
尝试执行此操作一次。
var understand = false; // not yet
while(understand !== true){
console.log("I'm learning while loops!");
understand = true; // I do now!
}
答案 2 :(得分:0)
当条件评估为true时,while循环将继续执行。所以它真的取决于你想要的条件。如果您只想要一个循环,并根据您的代码判断,您可能需要以下内容:
var understand = true;
while(understand) {
console.log("I'm learning while loops!");
understand = false;
}
这就像说:“虽然理解等于真,但执行循环”
值得一提的是,变量名understand
从true
开始并没有多大意义,并且当您想要打破循环时设置为false
(假设您想要当做明白时)打破循环。所以以下更符合逻辑:
var understand = false;//don't yet understand, so enter loop
while(!understand) {
console.log("I'm learning while loops!");
understand = true;//now I understand, so break loop
}
这就像说:“虽然理解等于假,然后执行循环”
答案 3 :(得分:0)
var understand = true;
while( understand == true){
console.log("I'm learning while loops!");
understand = false;
}