<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>運(yùn)算符</title>
</head>
<body>
<h1>結(jié)果:</h1>
<script>
//if語(yǔ)句
if(2 >= 1){//只有當(dāng)括號(hào)內(nèi)的條件為true時(shí),才會(huì)執(zhí)行
document.write("條件結(jié)果:"+(2 >= 1));
}
//if...else...語(yǔ)句
if(false){
document.write("條件結(jié)果:"+true);
}else{//當(dāng)括號(hào)內(nèi)條件結(jié)果為false時(shí)執(zhí)行
document.write("<br />"+"條件結(jié)果:"+false);
}
//if...else if...else...語(yǔ)句
if(0){//滿足條件1時(shí)執(zhí)行,執(zhí)行條件1后不再執(zhí)行后續(xù)
document.write("<br />"+"條件1結(jié)果:"+true);
}else if(1){//不滿足條件1時(shí),判斷條件2,條件2滿足時(shí)執(zhí)行
document.write("<br />"+"條件2結(jié)果:"+true);
}else{//條件1,條件2,條件...均不滿足時(shí)執(zhí)行else語(yǔ)句
document.write("<br />"+"所有條件結(jié)果:"+false);
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>運(yùn)算符</title>
</head>
<body>
<h1>結(jié)果:</h1>
<script>
//switch...case...數(shù)值判斷
var n = 1;
switch(n){
case 0 : //當(dāng)n的值和case后面的值相同時(shí)執(zhí)行
n = "case0";
break;//執(zhí)行完成后,跳出此方法
case 1 :
n = "case1";
break;
case 2 :
n = "case2";
break;
default ://當(dāng)case的值均不滿足的情況下執(zhí)行
n = "default";
}
document.write(n);
//switch...case...
var m = "m";
switch(m){
case "one" :
m = "one";
break;
case "two" :
m = "two";
break;
default :
m = "default";
}
document.write("<br />"+m);
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>運(yùn)算符</title>
</head>
<body>
<h2>結(jié)果:</h2>
<script>
var nums = [1,2,3,4];//數(shù)組
var numDict = {num01:"one",num02:"two",num03:"three"};//對(duì)象
//數(shù)組的遍歷
//for(1;2;3){};
for(var i=0;i<nums.length;i++){
//1.定義條件變量;2.判斷條件,滿足執(zhí)行{}內(nèi)容;3.{}執(zhí)行結(jié)束后執(zhí)行
document.write(nums[i]+".");
}
document.write("<br />");
for(var i=0,len=nums.length;i<len;i++){//1中定義多個(gè)變量
document.write(nums[i]+",");
}
document.write("<br />");
var i=0;
for(;i<nums.length;i++){//省略條件1,用全局變量作為判斷條件
document.write(nums[i]+";");
}
document.write("<br />");
for(;;i++){//當(dāng)省略條件判斷時(shí),為避免發(fā)生死循環(huán),必須在執(zhí)行方法中,有跳出for循環(huán)的操作
document.write(i+":");
if(i>10){
break;//跳出當(dāng)前循環(huán),避免形成死循環(huán)
}
}
document.write("<br />");
//for in 循環(huán)
//數(shù)組的for in
for(x in nums){//在數(shù)組中,循環(huán)遍歷其下標(biāo)
document.write(x+":"+nums[x]+"<br />");//x即數(shù)組的下標(biāo)
}
//對(duì)象的for in
for(x in numDict){//在對(duì)象中,循環(huán)遍歷對(duì)象的屬性名
document.write(x+":"+numDict[x]+"<br />");//x即對(duì)象的屬性名
}
//嵌套for循環(huán)
function makeStar(){//定義無(wú)參方法
var n = document.getElementById("numStar").value;//獲取輸入框輸入值
var star="";//定義空字符串
if(n==""||n==undefined||isNaN(n)){//判斷輸入框輸入值
alert("請(qǐng)輸入數(shù)字!");
}
for(var i=1;i<=n;i++){//遍歷沒(méi)一行,從第一行開(kāi)始到第n行
for(var j=1;j<=n-i;j++){//每一行的前半部分^個(gè)數(shù),首先打印中間行以前的^數(shù)
star+="^";//尾添加到字符串star中
}
for(var j=1;j<=2*i-1;j++){//每一行的+個(gè)數(shù),打印中間行的+個(gè)數(shù)
star+="+";
}
for(var j=1;j<=n-i;j++){//打印中間行以后的^個(gè)數(shù),形成對(duì)稱(chēng)
star+="^";
}
star+="<br />";//每一行的^和+和^個(gè)數(shù)打印結(jié)束,尾添加換行,開(kāi)始下一行的添加
}
document.getElementById("displayStar").innerHTML=star;//因?qū)懭胛募鲿?huì)清空之前的文件流,故采用此方式
}
</script>
<input type="text" id="numStar /">
<button type="button" onclick="makeStar()">n行星星</button>
<p id="displayStar"></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>運(yùn)算符</title>
</head>
<body>
<h2>結(jié)果:</h2>
<script>
var nums = [1,2,3,4];//數(shù)組
//while循環(huán)
var i=0;
while(i<5){//循環(huán)條件,當(dāng)條件為true時(shí),執(zhí)行;注意避免條件一直為ture,形成死循環(huán)
document.write(i+"*");
i++;
}
document.write("<br />");
//do...while...循環(huán)
var j=0;
do{
document.write(j+"^");
j++;
}while(j<0)//條件在后面,至少會(huì)執(zhí)行一次{}
document.write("<br />");
//遍歷數(shù)組
var k=0;
while(nums[k]){//從數(shù)組第一個(gè)元素到最后一個(gè)元素
document.write("nums["+k+"]:"+nums[k]+"<br />");
k++;
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>運(yùn)算符</title>
</head>
<body>
<h2>結(jié)果:</h2>
<script>
cars=["BMW","Volvo","Saab","Ford"];
//break
for(var i=0;i<4;i++){//for循環(huán)
if(i==2){//判斷條件
break;//跳出整個(gè)循環(huán),不再執(zhí)行后面的代碼已經(jīng)循環(huán)
}
document.write("cars["+i+"]:"+cars[i]+"<br />");
}
document.write("<hr />");
//continue
for(var i=0;i<4;i++){
if(i==2){
continue;//跳出當(dāng)前循環(huán),此次不再執(zhí)行后面的代碼,會(huì)繼續(xù)執(zhí)行下一個(gè)循環(huán)
}
document.write("cars["+i+"]:"+cars[i]+"<br />");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p id="continueResult"></p>
<p id="breakResult"></p>
<p id="go_on"></p>
<script type="text/javascript">
var x = 0;
//定義標(biāo)簽
table01:
for(var i=0;i<5;i++){
for(var j=0;j<2;j++){
x++;
if(i==1){
document.getElementById("continueResult").innerHTML=x;
continue table01;//跳到標(biāo)簽處,若循環(huán)未結(jié)束,會(huì)繼續(xù)執(zhí)行下一個(gè)循環(huán)
}
//因continue語(yǔ)句不結(jié)束循環(huán),故此條件會(huì)被執(zhí)行
if(i==2){
document.getElementById("breakResult").innerHTML=x;
break table01;//跳到標(biāo)簽處,且不再執(zhí)行循環(huán)
}
//因break語(yǔ)句結(jié)束循環(huán),故此條件不會(huì)被執(zhí)行
if(i==3){
document.getElementById("go_on").innerHTML="Go On ?";
}
}
}
</script>
</body>
</html>