function writeDocument(variable) { document.write(variable + "<br/>"); } //三种循环来计算1+2+3+...+100 //第一种循环 while var n1 = 1; var sum1 = 0; while (n1 <= 100) { sum1 += n1; n1++; } writeDocument("while循环: " + sum1); //第二种循环 for var n = 1; var sum = 0; for (; n <= 100; n++) { sum += n; } writeDocument("for循环: " + sum); //第三种循环 do while var n2 = 1; var sum2 = 0; do { sum2 += n2; n2++; } while (n2 <= 100); writeDocument("do while循环: " +sum2);