1. If structure
grammar:
if (condition) { JavaScript statement
}
This statement requires that the condition must be true before the JavaScript statement inside {} will be executed.
The condition here will only return boolean type is either true or false
<script>
var result1 = 61;
var result2 = 55;
if (result1>60){
alert('恭喜你,过关啦~') //页面弹出语句
}
if(result2>60){
alert('恭喜你,过关啦~') //页面没有显示
}
</script>
2. Multiple if structures
grammar:
if (condition 1) { JavaScript statement 1
} else if (condition 2) { JavaScript statement 2
}else{ JavaScript statement 3
}
Here it is executed from top to bottom, if condition 1 is satisfied, execute JavaScript statement 1, and then the program stops; if condition 1 is not satisfied, then continue to compare condition 2, if it is satisfied, execute JavaScript statement 2, if not, continue to search; if the previous If none of the conditions are met, the JavaScript statement in the last else is output.
//示例1:
<script>
// 多重if结构嵌套,按照顺序来判断,只要有一个为true,其他都不执行
var result = prompt('请输入你的考试成绩')
if (result>90){
alert('优秀!')
}else if(result>=70){
alert('良好!')
}
else if(result>=60){
alert('及格!')
}else{
alert('希望你下次再努力!')
}
</script>
//示例2:
<script>
var year = prompt('请输入年份' )
if (year % 4==0 && year % 100 !=0 || year % 400==0){
alert(year + '年是闰年')
}
else {
alert(year + '年是平年')
}
</script>
Two, switch statement
The value in the switch (expression) must be consistent with the value and type of the case to compare
grammar:
switch(expression) {
case n:
code block
break;
case n:
code block
break;
default:
default code block
}
Case and default must be followed by a break to terminate the subsequent comparison
<script>
// switch(表达式)里面的值,要和 case 的值和类型保持一致才能比较
switch(8){
case 1 :
document.write('结果为1');
break;
case 2 :
document.write('结果为9');
break;
default:
document.write('都不对!');
break
}
</script>