1. 循環—while
語法:while(條件){
//循環體-循環操作//更新循環條件++/--;
}
2.continue
作用:終止本次循環的執行,繼續下一次的循環
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" style="text/css" href="">
<style>
</style>
</head>
<body>
<script>
計算打印2——100的偶數和
// 方法1:
function sumOs(){
var os=0;
var i=2;
while(i<=100){
os+=i;
i+=2;
}
console.log(os);
}
sumOs();
//方法2:
function sumOs(){
var os=0;
var i=1;
while(i<=100){
if(i%2==0){
os+=i;
}
i++;
}
console.log(os);
}
sumOs();
//方法3:
function sumOs(){
var os = 0;
var i = 1;
while(i<=100){
if(i%2==1){
i++;
continue;
}
os += i;
i++;
}
console.log(os);
}
sumOs();
//擴展:循環從彈窗中錄入信息,并且打印,直到輸入exit為止
function printMsg(){
while(true){
var msg=prompt("請輸入一個字段啊");
if(msg=="exit"){
break;
}else{
console.log(msg);
}
}
}
printMsg();
</script>
</body>
</html>
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com