1.if...else的学习:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>if...else </title> <script type="text/javascript"> var ticket = true; if (ticket) { document.write("有票,看电影") } else{ document.write("没有票,看不了电影") } </script> </head> <body> </body> </html>
2.多种选择(Switch语句):
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>switch</title> <script type="text/JavaScript"> var myweek =3;//myweek表示星期几变量 switch(myweek) { case 1: case 2: document.write("学习理念知识"); break; case 3: case 4: document.write("到企业实践"); break; case 5: document.write("总结经验"); break; default: document.write("周六、日休息和娱乐"); } </script> </head> <body> </body> </html>
3.for循环语句:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>if...else</title> <script type="text/JavaScript"> var mymoney,sum=0;//mymoney变量存放不同面值,sum总计 for(mymoney=1;mymoney<=10;mymoney++) { sum= sum + mymoney; } document.write("sum合计:"+sum); </script> </head> <body> </body> </html>
4.While语句:
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>while</title> <script type="text/javascript"> var mynum =1;//mynum初值化数值为1 while (mynum<=5) { document.write("数字:"+mynum); mynum=mynum+1; } </script> </head> <body> </body> </html>
5.Do...While语句:
do while结构的基本原理和while结构是基本相同的,但是它保证循环体至少被执行一次。因为它是先执行代码,后判断条件,如果条件为真,继续循环。
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>do...while</title> <script type="text/javascript"> var mynum =6;//mynum初值化数值为6 do { document.write("数字:"+mynum+"<br/>"); mynum=mynum-1; } while( mynum <= 0 ) </script> </head> <body> </body> </html>
6.退出循环Break;
7.退出本次循环Continue;