您的位置:首页 > Web前端 > JavaScript

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

2015-05-22 15:35 831 查看


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

JS:控制语句
If
循环

JS:控制语句-If返回顶部
If
If-else
If嵌套

JS:控制语句-if返回顶部
<html xmlns="http://www.w3.org/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>


JS:控制语句-if_else返回顶部
<html xmlns="http://www.w3.org/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>


JS:控制语句-if嵌套返回顶部
<html xmlns="http://www.w3.org/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>


JS:控制语句-循环返回顶部
for
switch
while
do-while

JS:控制语句-循环-for返回顶部
<html xmlns="http://www.w3.org/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>


JS:控制语句-循环-switch返回顶部
<html xmlns="http://www.w3.org/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>


JS:控制语句-循环-while返回顶部
<html xmlns="http://www.w3.org/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>


JS:控制语句-循环-do_while返回顶部
<html xmlns="http://www.w3.org/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>


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐