<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>计算1到100的和</title>
</head>
<script type="text/javascript">
function sum1(){
var a=document.getElementById("m1");
a=parseInt(a.value);
var b=document.getElementById("n1");
b=parseInt(b.value);
var s=0;
for(var i=a;i<=b;i++){
s+=i;
}
alert(s);
}
function sum2(){
var a=document.getElementById("m1");
a=parseInt(a.value);
var b=document.getElementById("n1");
b=parseInt(b.value);
var s=0;
var i=a;
do{
s+=i;
i++;
}while(i<=b)
alert(s);
}
function sum3(){
var a=document.getElementById("m1");
a=parseInt(a.value);
var b=document.getElementById("n1");
b=parseInt(b.value);
var s=0;
var i=a;
while(i<=b){
s+=i;
i++;
}
alert(s);
}
</script>
<body>
计算从<input id="m1" type="text">到<input id="n1" type="text">的值
<button onclick="sum1()">用for循环计算</button>
<button onclick="sum2()">用do...while循环计算</button>
<button onclick="sum3()">用while循环计算</button>
</body>
</html>