[JS] 迴圈與條件式

while loop:只有當條件式為true時,才會執行大括號 { } 裡的程式碼。

var number = 1;
while (number <= 5) {
  console.log(number);
  number++;
};

for loop

for ( var number = 5; number > 0; number-- ) {
  console.log(number);
};

if...else...if...else if...else...

var value1 = 4;
var value2 = 9;
if( value1 < value2 ){
  console.log(value1 + " is less than " + value2);
}else{
  console.log(value1 + " is greater than or equal to " + value2);
}
var value1 = 4;
var value2 = 9;
if( value1 < value2 ){
  console.log(value1 + " is less than " + value2);
}else if(value1 == value2){
  console.log(value1 + " is greater than " + value2);
}else{
  console.log(value1 + " is equal to " + value2);
}

您可在此處留言

avatar

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料

  Subscribe  
Notify of