1.switch语句
语法:
switch (expression){
case value:
//statement
break;
case value:
//statement
break;
default:
//statement
}
break 语句可以阻止后面语句的执行;
例1
var i="莫名";
switch(i){
case"莫名":
document.write("嗨,美女");
break;
case"李四":
document.write("才高八斗,英俊潇洒");
break;
default:
document.write("潇洒哥")
}
例2
var a = 5;
switch(a){
case 5:
document.write(a+"是5");
break;
case 15:
break;
document.write(a+"不是15");
default:
document.write("其它");
break;
}
2.while 循环
语法:
while (exp) {
//statements;
}
说明:
while (变量<=结束值) { 需执行的代码 }
例1
var i = 1;
var a = 1;
while(i < 1001){
document.write(i+"<br />");
i++;
}
例2
var a = 1;
while(a < 1001){
if(a%2==0)
document.writeln("<span style='text-decoration:underline'>"+a+"</span>");
a++;
}
例3
var a = 1;
while(a < 1001){
if(a%2!=0)
document.writeln("<span style='text-decoration:underline'>"+a+"</span>");
a++;
}
3.do while
语法:
do {
//statements;
}while (condition);
do { 需执行的代码 } while (变量<=结束值)
例:ar c=88;
do{
document.write("发发");
c++;
}
while(c<100);
4.for循环语句
for(initial; expression; post-loop-expression){
//statement;
}
for (变量=开始值;变量<=结束值;变量=变量+步进值) {
需执行的代码
}
例:
for(var i=1;i<=4;i++){
document.write("<img src='img/"+i+".png' />");
}