批改状态:未批改
老师批语:
循环,函数。
switch分支循环语句
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>javascript2</title>
</head>
<body>
<script>
// switch(n)
// {
// case 1:
// 执行代码块 1
// break;
// case 2:
// 执行代码块 2
// break;
// default:
// n 与 case 1 和 case 2 不同时执行的代码
// }
switch(3){
case 1:
document.write("匹配到n的值为1"+"<br>");
break;
case 2:
document.write("匹配到n的值为2"+"<br>");
break;
case 3:
document.write("匹配到n的值为3"+"<br>");
break;
default:
document.write("匹配不到时,输出的值"+"<br>");
}
// for
for(var i=1;i<=9;i++){
for(var j=1;j<=i;j++){
document.write(i+"*"+j+"="+i*j+" ");
}
document.write('<br/>');
}
// while
var i =1;
while(i<10){
document.write(i)
i++;
}
// do while 至少执行一次
var i=1;
do{
document.write(i+"<br>");
i++;
}while(i<=10);
// for in 循环对象属性
var add={
name:"张三丰",
age:"200岁",
height:"180cm"
}
var x;
var test = "";
for(x in add){
test = test +add[x]+";";
}
document.write(test+"<br/>");
// break continue 跳出循环
for(var i=1;i<=10;i++){
if (i==5) {
// break;
continue
}
document.write(i+"<br/>")
}
</script>
</body>
</html>点击 "运行实例" 按钮查看在线实例
函数
<!DOCTYPE html>
<meta charset="utf-8">
<html>
<head>
<title>function</title>
</head>
<body>
<script>
// 自定义函数
function num(){
for(var i=1;i<=10;i++){
document.write(i+"<br>")
}
}
num(); //不会自己运行,们需要调用
// 函数表达式
var x= function(){
document.write("匿名函数"+"<br>")
}
// 构造函数
// var obj=new Function(document.write("构造函数"))
// 函数的返回值与参数
// 在调用函数时,可以向其传递值,这些值被称为参数
// 形参:函数创建时括号中的参数
// 实参:函数被调用时,函数名括号中的参数
function obj(x,y){
document.write(x*y+"<br>")
}
obj(2,3)
// 创建带由返回值的函数
// 运行到return语句,结束整个函数,后面语句不再执行
function fun(a,b){
return a+b;
}
document.write(fun(5,5))
</script>
</body>
</html>点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号