简体   繁体   English

如何在JavaScript的do-while循环中嵌套if语句

[英]How do you nest if statement in do-while loop in javascript

 var temp = 110; for { temp-=1 if (temp >= 90) { console.log("Today's temperature is "+temp+"! "+"Lets go play ball") } else { console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!") } }while (temp > 90) 

Please review my snippet. 请查看我的代码段。 It won't run for some reason stating some bracket error as I already checked the brackets. 由于我已经检查了括号,它由于某些原因而无法运行,并指出了一些括号错误。

Its do not for do不是for

 var temp = 110; do { //its `do` not `for` temp -= 1; if (temp >= 90) { console.log("Today's temperature is " + temp + "! " + "Lets go play ball") } else { console.log("Today's temperature is " + temp + "! " + "It is too hot today to ball!") } } while (temp > 90); 

You may use prompt() for user input: 您可以使用prompt()进行用户输入:

 var temp = prompt('Input temperature', '110'); // (message, default input) console.log('temp', temp); do { //its `do` not `for` temp -= 1; if (temp >= 90) { console.log("Today's temperature is " + temp + "! " + "Lets go play ball") } else { console.log("Today's temperature is " + temp + "! " + "It is too hot today to ball!") } } while (temp > 90); 

Replace for with do. 替换为do。 like this 像这样

var temp = 110;
do{
    temp-=1
    if (temp >= 90) {
        console.log("Today's temperature is "+temp+"! "+"Lets go play ball")
    } else {
        console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!")
    }

}while (temp > 90)

It should be 它应该是

 var temp = 110; do { temp-=1 if (temp >= 90) { console.log("Today's temperature is "+temp+"! "+"Lets go play ball") } else { console.log("Today's temperature is "+temp+"! "+"It is too hot today to ball!") } }while (temp > 90); 

The do while loop syntax is like this: do while循环语法如下所示:

Example

do {

}while(conditions);

So a do-while loop with a nested if/else statement is like this: 因此,带有嵌套if / else语句的do-while循环如下所示:

do-while w/ nested if/else 当/嵌套if / else

do{

if() {
}
else {
}
}while();

Example w/ YOUR CODE 带您的代码的示例

var temp = 110; var temp = 110;

do {
 if(temp >= 90) 
{
    console.log("Today's temperature is "+temp+"! "+"Lets go play ball");
{
else 
{
    console.log("Today's temperature is "+temp+"! "+"It is too hot today to play      ball!");
}
 temp--; 
} while(temp > 90);

Okay now, let me explain what is happening here. 好吧,现在让我解释一下这里发生了什么。 What you are essentially doing is telling the compiler to do something until the while loop returns true. 实际上,您要做的是告诉编译器做一些事情,直到while循环返回true。 So, if you notice I changed temp -= 1; 因此,如果您注意到我将temp -= 1;更改temp -= 1; to temp--; it is exactly the samething it's just much more standard to use the latter. 完全一样,使用后者是更标准的。 You were actually very close with your original code other than it is a do-while loop not a for-while. 实际上,您与原始代码非常接近,只是它是一个do-while循环而不是for-while。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM