第一句子网 - 唯美句子、句子迷、好句子大全
第一句子网 > JS:1.2 控制语句(if if else for switch while do while)

JS:1.2 控制语句(if if else for switch while do while)

时间:2020-03-25 03:43:34

相关推荐

JS:1.2 控制语句(if if else for switch while do while)

If循环IfIf-elseIf嵌套

<html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">var d=new Date();var time= d.getHours();if(time<10){document.write("早上好!");}</script></head><body><h1>if的示例(1_1)</h1>1,if(条件1){语句1:}</pre><p>如果我们的浏览器的时间在10点之前,我们将得到一个"早上好!" 的问候。</p></body></html>

<html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">var d=new Date();var time= d.getHours();if(time<=10){document.write("早上好!");}else if(time<=12){document.write("上午好!");}else if(time<=18){document.write("下午好!");}else{document.write("晚上好!");}</script></head><body><h1>if的示例(1_2)</h1><pre>2,if(条件1){语句1:}esle{语句2:}</pre><p>根据浏览器不同的时间段:提示相应的问候语。</p></body></html>

<html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">var i=j=1;var k=2;if(i==j){if(j==k){document.write("j等于k");}else{document.write("j不等于k");}}</script></head><body><h1>嵌套if语句(1_3)</h1><h2><pre>公式:3,if(条件1){if(条件2){语句1:}}</pre></h2></body></html>

forswitchwhiledo-while

<html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">for(var i=1;i<=5;i=i+1){document.write(i);document.write("<br>");}</script></head><body><h1>for循环示例</h1><pre>利用for循环输出1-5之间的数字。for(初始值;条件; 增值){语句块1;}</pre></body></html>

<html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">var d=new Date();theDay=d.getDay();document.write("今天是周",theDay,",");switch(theDay){case 5:document.write("<b>今天是周五</b>");break;case 6:document.write("<b>今天是周六</b>");break;default:document.write("<b>工作日,我们期待周末的到来!嘿嘿</b>");}</script></head><body><h1>switch循环示例</h1><h2><pre>基本格式:switch(条件){case label1:语句段1;break;case label2:语句段2;break;case label3:语句段3;break;...... default:语句段 4;}</pre></h2></body></html>

<html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">var count=0;while(count<10){document.write(count+"<br>");count++;}</script></head><body><h1>while循环示例</h1><h2>当条件为真是时,重复循环,否则退出循环体。</h2><pre>while(判断条件1){语句块1;}</pre></body></html>

<html xmlns="/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=gb2312" /><title>无标题文档</title><script language="javascript">var count =0;do{document.write(count+"<br>");count++;}while(count<0)</script></head><body><h1>do while循环的示例</h1><h2><pre>基本格式:do{语句段;}while(条件)例如:即使条件永远不为真,循环中的语句至少也会执行一次。</pre></h2></body></html>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。