JavaScript中的流程控制—条件语句
if(exp)执行一句代码;
if(exp){执行代码段};
if(exp){exp为true执行代码段}else{exp为false执行代码段}
if…elseif… switch…case
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
//条件语句
if(3>1)
document.write('hello ');
document.write('world');
document.write('<hr color="red"/>');
if(true){
document.write('hello ');
document.write('world');
var x= 1,y= 2,username='king';
}
document.write(x+'--'+y+'--'+username);
//if...else
document.write('<hr color="darkred"/>');
var username='king';
if(username=='king')
{
document.write('hello king');
}else{
document.write('hello maizi');
}
document.write('<hr color="red"/>');
//if...else if
username='admin';
if(username=='king');{
document.write('hello king');
}else if(username='maizi'){
document.write('hello maizi');
}else if(username='admin'){
document.write('hello admin');
}
document.write('<hr color="red"/>');
if(true==false){
document.write('a');
}else if(3>11){
document.write('b');
}
document.write('<hr color="red"/>');
var x=11;
if(x==1){
document.write('aaa');
}else if(x==2){
document.write('bbb');
}else if(x==3){
document.write('ccc');
}else {
document.write('以上表达式都为false执行的代码段');
}
document.write('<hr color="red"/>');
//switch...case语句
/*
switch(exp){
case 值1;
执行的代码段;
break;
case 值2;
执行的代码段;
break;
...
default:
执行的代码;
break;
}
//严格类型比较
*/
var i=1;
switch(i){
case 1:
document.write('aa');
break;
case 2:
document.write('bb');
break;
default:
document.write('以上case都没有匹配到的时候执行的代码段');
break;
case 3:
document.write('cc');
break;
case 4:
document.write('dd');
break;
}
document.write('<br/>continue');
document.write('<hr color="red"/>');
i=14;
switch(i){
case 1:
document.write('a');
case 2:
document.write('b');
default:
document.write('e');
case 3:
document.write('c');
break;
case 4:
document.write('d');
}
document.write('<hr color="red"/>');
var i=1;
switch(i){
case 1;
case 2;
case 3;
document.write('hello world');
break;
case 4;
case 5;
case 6;
document.write('hello maizi');
break;
default;
document.write('hello everybody');
break;
}
</script>
</body>
</html>
if嵌套
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<script type="text/javascript">
var username='king',age='12',sex='男';
if(3>1){
document.write('aa<br/>');
if(username=='king'){
document.write('hello king<br/>')
if(age>=18){
document.write('成年人<br/>');
if(sex='男'){
document.write('帅哥');
}else{
document.write('美女');
}
}else{
document.write('成年人');
}
}else{
document.write('hello others<br/>')
}
}else{
document.write('bb<br/>')
}
</script>
</body>
</html>