2-2 迴??述

` JavaScript Ojԭz]Loop Statement^AoDZԭziHqЦaY@q{XC

O for jԭzA榡M C y for jA򥻮榡pUG

for ([w@ӭpܼƪ]; [զ]; [spܼƪʧ@]) { j餺ԭz } for ԭz|w@ӭpܼƪȡB@ӱ󦡡AHΤ@ӷ|spܼƪʧ@CbC@nj餧eA|P_󦡪ȡCpG󦡬uAj̪{XN|FϤApGSqLAN|Aêj餧᪺Ĥ@{XCbj餧AN|isoӭpܼƪʧ@AMA}lU@ӭNCUCdҨϥ for jӦLX 5 Ө̦ܤj uHello worldIvG

Example]forLoop01.htm^G

WzdҪlɮצpUG

lɡ]forLoop01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center> for jӲ 5 ӥѤpܤj  "Hello World!"</h2>
<hr>

<script>
//  for jӲ 5 ӥѤpܤj  "Hello World!"
for (i=1; i<=5; i++) {
	document.write("Font size = " + i + " ===> ");
	document.write("<font color=green size=" + i + ">Hello World!</font><br>");
}
</script>

<hr>
</body>
</html>

JavaScript ѥt@دS for jA٬ for-in jAiΩBz@Ӫ󪺩ҦݩʡCb for-in j餤pܼƬO@ӦrAӤO@ӼƦrArܼƦbCjN@ӯSwݩʦW١A]ڭ̥iHϥΦjAa|X@Ӫ󪺩ҦʽC|ҨӻACӺ@ document ӥNA]bUCdҤAڭ̨ϥ for-in jӦC| document 󪺩ҦʽG

Example]forInLoop01.htm^G

bWzdҤAiHݥX document ܦhʽAҦp document.fileCreatedDate NɮתͤAdocument.bgColor NICⵥCWzdҪlɮצpUG

lɡ]forInLoop01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>ϥ for-in jӦC| document ݩ</h2>
<hr>

<script>
// C| document 󤤪Ҧݩ
for (prop in document)
	document.write("<br>document." + prop + " = " + document[prop]);
</script>

<hr>
</body>
</html>

ѤWz{X]iHݥXAڭ̥iHϥ document.xyz document["xyz"] Ӧsݩ xyzAo쪺GOۦPC

MAڭ̤]iHإߤ@ӪAwݩʩMݩʭȫAA for-in jNҦݩʭȦCLXӡApUG

Example]forInLoop02.htm^G

WzdҪlɮצpUG

lɡ]forInLoop02.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>for-in j</h2>
<hr>

<script>
// إߨ㦳@ݩʪC
student = new Object();
student.name = "Timmy";
student.age = "25";
student.phone = "575-1114";

// C|󤤪Ҧݩ
for (prop in student)
    document.write("<br>student." + prop + " = " + student[prop]);

</script>

<hr>
</body>
</html>

bWzdҤAprop ܼƷ|QOwr "name"B"age"B"phone"AN󪺩ʽW١AMbg student[prop] oPʽҹȡC

t@ӱ`Ψ쪺jԭzO while jAΪk for jA򥻮榡G

while () { j餺ԭz } un󦡬uAwhile j骺ԭzN|Ф@AQCbUCdҤAڭ̧Q while jӤЦLXHܼƭȡAҹJ쪺HܼƭȤj 0.8 ~G

Example]whileLoop01.htm^G

WzdҪlɮ (whileLoop01.htm) pUG

lɡ]whileLoop01.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>while j</h2>
<hr>

<script>
x=Math.random();	// ͤ@Ӥ 0 M 1 ü
while (x<=0.8) {
	document.write("<br>"+x);
	x=Math.random();
}
document.write("<br>"+x);
</script>

<hr>
</body>
</html>

t~٦@өM while \jOA٬ do-while jA򥻮榡pUG

do { j餺ԭz } while (); while jOP_󦡡AAMwO_j餺ԭzF do-while jOj餺ԭzAAP_󦡡AMwO_~jA] do-while jܤַ|@j餺{XCҦpڭ̧Q do-while jӤФW@ӽdҡA{X|²Ƥ@ǡG

Example]whileLoop02.htm^G

WzdҪlɮצpUG

lɡ]whileLoop02.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>do-while j</h2>
<hr>

<script>
do {
	x=Math.random();	// ͤ@Ӥ 0 M 1 ü
	document.write("<br>"+x);
} while (x<=0.8);
</script>

<hr>
</body>
</html>

Ynqj餤YXAi break ԭzAҦpbezLXüƪdҤAڭ̥iHgpUG

lɡ]whileLoop03.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>while jGbreak ϥ</h2>
<hr>

<script>
while (1) {
	x=Math.random();	// ͤ@Ӥ 0 M 1 ü
	document.write("<br>"+x);
	if (x>0.8)
		break;
}
</script>

<hr>
</body>
</html>

bt@رpUAڭ̥iݭnj骺AåߨܤU@Ӱj骺}lmӰA\i continue ӹFCҦpAbBz 100 ӶüƮɡAYڭ̥uQLXƭȤj 0.95 üơAiѤUC{XF]whileLoop04.htm^G

lɡ]whileLoop04.htm^G]ǦϰUYi^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>for jGcontinue ϥ</h2>
<hr>

<script>
for (i=0; i<100; i++) {
	a=Math.random();	// ͤ@Ӥ 0 M 1 ü
	if (a<=0.95)
		continue;
	document.write("<br>"+a);
}
</script>

<hr>
</body>
</html>


JavaScript {]pPΡGΩΤ