for语句
for循环的遍历
document.write("CC========for循化==============================","<br/>"); for (var i = 0; i < 20; i++) { document.write(i); }
for in遍历对象属性循环
document.write("========for in遍历对象属性循化==============================","<br/>"); obj={ name:"我笑", age:45, gender:"maile" }; for(var x in obj) { document.write(obj[x]); };
for 遍历数组
document.write("========for in循化===遍历数组===========================","<br/>"); var a=['A7','B8','C9','D5','E7','F5','G6']; for(var i in a){ document.write(i,a[i],"<br>")
for in循环 对象继承属性
document.write("========for in循化==对象继承属性============================","<br/>"); for(var key in obj){ if(obj.hasOwnProperty(key)){ document.write(key); } }
跳转语句
continue语句
<script> for (var index1 = 1; index1 <= 12; index1++) { if (index1 == 7) break; // 跳出循环 document.write(index1 + "\n"); document.write("<br>") } document.write("++++++++++++++++++++++++++++++++++++++++++++++++") for (var index = 1; index <= 12; index++) { if (index == 6) continue; // 跳出循环 document.write(index + "\n"); } </script>
简单的数字娱乐:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>猜数游戏</title> <script type="text/javascript"> var num = Math.floor(Math.random() * 50 + 1); var num = Math.floor(Math.random() * 50 + 1); do { var guess = parseInt(prompt("下面进行猜数游戏\n请输入1-50之间的整数:", "")); var he = parseInt(prompt("下面进行猜数游戏\n请输入1-50之间的整数:", "")); if (guess == num, he == num) { alert("^_^ ,恭喜你,猜对了,幸运数字是:" + num); break; } else { if (guess > num, he > num) { alert("^_^ ,你猜的数字大了"); go_on = confirm("是否继续游戏?"); } else { alert("^_^ ,你猜的数字小了"); go_on = confirm("是否继续游戏?"); } } } while (go_on); alert("谢谢参与游戏!"); </script> </head> <body> </body> </html>
异常语句
<!-- 异常一 --> <script> var str ="I LIKE Javascript"; try { document.write(str.ghtu(5)); } catch (exception) { alert("运行出现异常") }finally{ alert("结束的try ...... catch.......... finally") } </script>
<!-- 异常二 --> <script> var str1 ="I LIKE Javascript"; try { document.write(str.charat(5)); } catch (exception) { alert("错误的原因在"+exception.message+"错误的字符型是"+exception.name) } </script>
<!-- 异常三 --> <script> try { var num=1/0; if(num==Infinity){ throw new Error("除数步能为零") } } catch (exception) { alert(exception.message); document.write(exception.message) } </script>
流程控制语句的基本语法到这里了。后期会有大量的练习。
https://blog.csdn.net/qq_56248592/article/details/127341920