do..while
があるとwhile..
があります:なしdo..while..
声明はありません。
JavaScriptでは、block statementsは他のフロー制御/定義構造から独立しています。不足しているステートメントのセミコロンのために、これは構文エラー(Javaではそうです)を引き起こしません。
の構文に関するいくつかの追加の説明があります。他の答えは論理エラーをカバーします。一方
do {
console.log("I'm the do loop");
} while(condition === true) // semicolons optional in JS (see ASI):
// 'do..while' statement ENDS HERE
{ // starts a block statement which has naught to do with 'do..while' above
// THERE IS NO WHILE LOOP HERE
console.log("I'm the while loop");
condition = false;
}; // useless semicolon which further leads to confusion
do..
が省略された場合、それが終了しているだろう「だけ」while
文として解析されていたであろう。
// Basic WHILE statement - no 'do..' code, so NOT parsed as a 'do..while'!
while(condition === true)
{ // this block is now part of the 'while' statement loop
console.log("I'm the while loop");
condition = false;
};
ループは 'do'で始まり、' while'で終わり、間には 'condition'がありません。後のブロックは単なるブロックです。 –