2-1 條件?述

bԭzA̱`NO if ԭzA@榡pUG If (condition) { K } bWz榡AY condition ȬO true ΫDsAhjA{XFϤAhCYnbP_󤣦߮ɡAt@q{XAhi if-else ԭzG If (condition) { statement 1 } else { statement 2 } bWz榡AY condition ȬO true ΫDsAh statement 1FϤAh statement 2CpGb󦨥ߡ]Τߡ^ɡAuݰ@ӱԭzANiHٲjACҦpG

Example]ifElse01.htm^G

WzdҥiھڨϥΪ̿J~֡AӦ^ؤPԴAܡAlɮצpUG

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

<body>
<h2 align=center>if-else ԭz</h2>
<hr>

<script>
function flatter() {
	var age = prompt("пJz~֡G", 30);
	if (age<30)
		alert("zu " + age + " AuOC~~TڡI");
	else
		alert("z~L30AQOƷ~FI");
}
</script>
<a href="javascript:flatter()">ԴA</>

<hr>
</body>
</html>

Ynihر󪺤AhiHШϥ if-else ԭzAOo˷|y{XáAt@ӤnkAhOϥ switch ԭzAҦpYnP_ѬOPXAæLXTAiUCdҡG

Example]switch01.htm^G

WzdҪlɮצpUG

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

<body>
<h2 align=center>switch ԭz</h2>
<hr>

<script>
today = new Date();	// ouѡv
day = today.getDay();	// oѬOPX
switch (day) {
	case 0:
		document.write("<p>ѬOPѭCAiHΨ12IơI");
		break;
	case 1:
		document.write("<p>ѬOP@...GDIM...");
		break;
	case 2:
	case 3:
		document.write("<p>ѬOPGΤTAg٫ܻOI~u@...");
		break;
	case 4:
		document.write("<p>ѬOP|...P٨SH");
		break;
	case 5:
		document.write("<p>ѬOPATGIF!");
		break;
	case 6:
		document.write("<p>ѬPA֭nڥhH");
		break;
	default:
		document.write("<p>Error!");
}
</script>

<hr>
</body>
</html>

bWdҤAday ȬOq 0 6AONPBP@BPGB...BPA]ڭ̥iHϥ day ȡAӦLXPTCݭn`NOAswitch |̧ǤC@ case OAæb󺡨AԭzCYݭnbŦXYӯSwNAAɴNnbԭz̫᭱[W break ԭzC~Adefault ᪺ԭzAu|bҦ󧡤ŦXɡA~|QCpG[W breakAhtη|bŦXY@ӯSwANŦXUCL󪺱ԭzAͫܩ_ǪGCUCdҩMW@ӽdҧۦPAߤ@tOOS breakApUG

Example]switch02.htm^G

WzdҪlɮצpUG

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

<body>
<h2 align=center>switch ԭz</h2>
<hr>

<script>
today = new Date();	// ouѡv
day = today.getDay();	// oѬOPX
switch (day) {
	case 0:
		document.write("<p>ѬOPѭCAiHΨ12IơI");
		//break;
	case 1:
		document.write("<p>ѬOP@...GDIM (God damned it's Monday)...");
		//break;
	case 2:
	case 3:
		document.write("<p>ѬOPGΤTAg٫ܻOI~u@...");
		//break;
	case 4:
		document.write("<p>ѬOP|...P٨SH");
		//break;
	case 5:
		document.write("<p>ѬOPATGIF (Thank God it's Friday) !");
		//break;
	case 6:
		document.write("<p>ѬPA֭nڥhH");
		//break;
	default:
		document.write("<p>Error!");
}
</script>

<hr>
</body>
</html>

Hint
HW switch 欰AM C/C++ switch OۦPC

YOP_Aڭ̤]iHϥΡuBvBuΡvBu_wv覡ӲͽƦXAШUG

Ÿ
B&&
||
_w!

ҦpAYnP_O_ua jsA b M c psvAiΤUC{XG

If ((a>0) || (!(b<0) && !(c<0))) { ... }

JScript ]䴩tΦA٬BlCڭ̥Nnժ᭱[W@ӰݸAPɤ]wӿﶵAĤ@ӥΦb󦨥߮ɡAt@ӫhΦb󤣦߮ɡAoӿﶵH@ӫ_j}ӡAҦpAUCdҥiHLX{bɶOuWȡv٬OuUȡvG

Example]implicitIf01.htm^G

WzdҪlɮצpUG

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

<body>
<h2 align=center>Bl</h2>
<hr>

<script>
today = new Date();	// ouѡv
hour = today.getHours();	// oɼ
minute = today.getMinutes();	// o
second = today.getSeconds();	// o
prepand = (hour>=12)? "U":"W";	// QαBlӨMwOuWȡvΡuUȡv
hour = (hour>=12)? hour-12:hour;	// QαBlӧ令12pɨ
document.write("{bɶO"+prepand+hour+"I"+minute+""+second+"");
</script>

<hr>
</body>
</html>

bԭzAJavaScript pP_@ӹB⦡O true ]u^ false ]^OHھڪhpUG

  1. B⵲GO@ӼƭȮɡAYƭȵ 0AhO falseA_hNO trueC
  2. B⵲GO@ӦrɡAYr굥Ŧr]""^AhO falseA_hNO trueC
HUoǽdŪ̦ۦѦҤαQG

ԭzPwG
0false
5true
-3true
""false
"0"true
"00"true
"0.0"true

UCdҦLXWzP_󪺵GG

Example]testIf.htm^G

WzdҪlɮצpUG

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

<body>
<h2 align=center>P_󪺯u</h2>
<hr>

<script>
condition=0;
document.write("<br>Condition=0 ===> "+(condition?true:false));
condition=5;
document.write("<br>Condition=5 ===> "+(condition?true:false));
condition=-3;
document.write("<br>Condition=-3 ===> "+(condition?true:false));
condition="";
document.write("<br>Condition=\"\" ===> "+(condition?true:false));
condition="0";
document.write("<br>Condition=\"0\" ===> "+(condition?true:false));
condition="00";
document.write("<br>Condition=\"00\" ===> "+(condition?true:false));
condition="0.0";
document.write("<br>Condition=\"0.0\" ===> "+(condition?true:false));
</script>

<hr>
</body>
</html>


JavaScript {]pPΡGΩΤ