2-1 ����ԭz

¦b±ø¥ó±Ô­z¤¤¡A³Ì±`¨£ªº´N¬O if ±Ô­z¡A¨ä¤@¯ë®æ¦¡¦p¤U¡G If (condition) { ¡K } ¦b¤W­z®æ¦¡¤¤¡A­Y condition ªº­È¬O true ©Î«D¹s¡A«h°õ¦æ¤j¬A¸¹¤¤ªºµ{¦¡½X¡F¤Ï¤§¡A«h¤£°õ¦æ¡C­Y­n¦b§PÂ_±ø¥ó¤£¦¨¥ß®É¡A¯à°÷°õ¦æ¥t¤@¬qµ{¦¡½X¡A«h¥i¥Î if-else ±Ô­z¡G If (condition) { statement 1 } else { statement 2 } ¦b¤W­z®æ¦¡¤¤¡A­Y condition ªº­È¬O true ©Î«D¹s¡A«h°õ¦æ statement 1¡F¤Ï¤§¡A«h°õ¦æ statement 2¡C¦pªG¦b±ø¥ó¦¨¥ß¡]©Î¤£¦¨¥ß¡^®É¡A¥u»Ý°õ¦æ¤@­Ó±Ô­z¡A¨º´N¥i¥H¬Ù²¤¹ïÀ³ªº¤j¬A¸¹¡C¨Ò¦p¡G

Example¡]ifElse01.htm¡^¡G

¤W­z½d¨Ò¥i®Ú¾Ú¨Ï¥ÎªÌ¿é¤Jªº¦~ÄÖ¡A¦Ó¦^À³¨âºØ¤£¦P½Ô´Aªº¸Ü¡A§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]ifElse01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<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("½Ð¿é¤J±zªº¦~ÄÖ¡G", 30);
	if (age<30)
		alert("±z¥u¦³ " + age + " ·³¡A¯u¬O«C¦~¤~«T°Ú¡I");
	else
		alert("±z¦~¹L30¡A·Q¥²¬O¨Æ·~¦³¦¨¤F¡I");
}
</script>
<a href="javascript:flatter()">½Ô´Aªº¸Ü</>

<hr>
</body>
</html>

­Y­n¶i¦æ¦hºØ±ø¥óªº¤ñ¹ï¡A«h¥i¥H¤ÏÂШϥΠif-else ±Ô­z¡A¦ý¬O³o¼Ë·|³y¦¨µ{¦¡½XªºÂø¶Ã¡A¥t¤@­Ó¤ñ¸û¦nªº¤èªk¡A«h¬O¨Ï¥Î switch ±Ô­z¡A¨Ò¦p­Y­n§PÂ_¤µ¤Ñ¬O¬P´Á´X¡A¨Ã¦L¥X¬ÛÃöªº°T®§¡A¥i¨£¤U¦C½d¨Ò¡G

Example¡]switch01.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]switch01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<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();	// ¨ú±o¡u¤µ¤Ñ¡vªºª«¥ó
day = today.getDay();	// ¨ú±o¤µ¤Ñ¬O¬P´Á´X
switch (day) {
	case 0:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤Ñ­C¡A¥i¥HºÎ¨ì12ÂI¹Æ¡I");
		break;
	case 1:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤@...GDIM...");
		break;
	case 2:
	case 3:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤G©Î¤T¡AÂ÷¶g¥½ÁÙ«Ü»·©O¡IÄ~Äò¤u§@¤¤...");
		break;
	case 4:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¥|...¬P´Á¤­¬°¤°»òÁÙ¨S¨ì¡H");
		break;
	case 5:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤­³á¡ATGIF!");
		break;
	case 6:
		document.write("<p>¤µ¤Ñ¬P´Á¤»³á¡A½Ö­n¸ò§Ú¥h¦å«÷¡H");
		break;
	default:
		document.write("<p>Error!");
}
</script>

<hr>
</body>
</html>

¦b¤W­±ªº½d¨Ò¤¤¡Aday ªº­È¬O±q 0 ¨ì 6¡A¤À§O¥Nªí¬P´Á¤é¡B¬P´Á¤@¡B¬P´Á¤G¡B...¡B¬P´Á¤»¡A¦]¦¹§Ú­Ì¥i¥H¨Ï¥Î day ªº­È¡A¨Ó¦L¥X¤£¦Pªº°T®§¡C»Ý­nª`·Nªº¬O¡Aswitch ·|¨Ì§Ç¤ñ¹ï¨C¤@­Ó case «ü¥Oªº±ø¥ó¡A¨Ã¦b±ø¥óº¡¨¬«á¡A°õ¦æ¬ÛÃöªº±Ô­z¡C­Y»Ý­n¦b²Å¦X¬Y­Ó¯S©w±ø¥ó«á´N¤£¦A¤ñ¹ï¡A¦¹®É´N­n¦b¬ÛÃö±Ô­z³Ì«á­±¥[¤W break ±Ô­z¡C¦¹¥~¡Adefault ¤§«áªº±Ô­z¡A¥u·|¦b©Ò¦³±ø¥ó§¡¤£²Å¦X®É¡A¤~·|³Q°õ¦æ¡C¦pªG¤£¥[¤W break¡A«h¨t²Î·|¦b²Å¦X¬Y¤@­Ó¯S©w±ø¥ó«á¡A´N°õ¦æ²Å¦X¤U¦C¨ä¥L±ø¥óªº±Ô­z¡A²£¥Í«Ü©_©Çªºµ²ªG¡C¤U¦C½d¨Ò©M¤W¤@­Ó½d¨Ò§¹¥þ¬Û¦P¡A°ß¤@®t§O¬O¨S¦³ break¡A¦p¤U¡G

Example¡]switch02.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]switch02.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<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();	// ¨ú±o¡u¤µ¤Ñ¡vªºª«¥ó
day = today.getDay();	// ¨ú±o¤µ¤Ñ¬O¬P´Á´X
switch (day) {
	case 0:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤Ñ­C¡A¥i¥HºÎ¨ì12ÂI¹Æ¡I");
		//break;
	case 1:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤@...GDIM (God damned it's Monday)...");
		//break;
	case 2:
	case 3:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤G©Î¤T¡AÂ÷¶g¥½ÁÙ«Ü»·©O¡IÄ~Äò¤u§@¤¤...");
		//break;
	case 4:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¥|...¬P´Á¤­¬°¤°»òÁÙ¨S¨ì¡H");
		//break;
	case 5:
		document.write("<p>¤µ¤Ñ¬O¬P´Á¤­³á¡ATGIF (Thank God it's Friday) !");
		//break;
	case 6:
		document.write("<p>¤µ¤Ñ¬P´Á¤»³á¡A½Ö­n¸ò§Ú¥h¦å«÷¡H");
		//break;
	default:
		document.write("<p>Error!");
}
</script>

<hr>
</body>
</html>

Hint
¥H¤W switch ªº¦æ¬°¡A©M C/C++ ¤¤ªº switch ¬O§¹¥þ¬Û¦Pªº¡C

­Y¬O§PÂ_±ø¥ó¸û½ÆÂø¡A§Ú­Ì¤]¥i¥H¨Ï¥Î¡u¥B¡v¡B¡u©Î¡v¡B¡u§_©w¡vµ¥¤è¦¡¨Ó²£¥Í½Æ¦X±ø¥ó¡A½Ð¨£¤Uªí¡G

»¡©ú²Å¸¹
¥B&&
©Î||
§_©w!

¨Ò¦p¡A­Y­n§PÂ_¬O§_¡ua ¤j©ó¹s¡A©Î b ©M c §¡¤£¤p©ó¹s¡v¡A¥i¥Î¤U¦Cµ{¦¡½X¡G

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

JScript ¤]¤ä´©Áô§tªº±ø¥ó§Î¦¡¡AºÙ¬°±ø¥ó¹Bºâ¤l¡C­º¥ý§Ú­Ì¥²¶·±N­n´ú¸Õªº±ø¥ó«á­±¥[¤W¤@­Ó°Ý¸¹¡A¦P®É¤]«ü©w¨â­Ó¿ï¶µ¡A²Ä¤@­Ó¥Î¦b±ø¥ó¦¨¥ß®É¡A¥t¤@­Ó«h¥Î¦b±ø¥ó¤£¦¨¥ß®É¡A³o¨â­Ó¿ï¶µ¤§¶¡¥²¶·¥H¤@­Ó«_¸¹¤À¹j¶}¨Ó¡A¨Ò¦p¡A¤U¦C½d¨Ò¥i¥H¦L¥X²{¦b®É¶¡¬O¡u¤W¤È¡vÁÙ¬O¡u¤U¤È¡v¡G

Example¡]implicitIf01.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]implicitIf01.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=big5">
</head>

<body>
<h2 align=center>±ø¥ó¹Bºâ¤l</h2>
<hr>

<script>
today = new Date();	// ¨ú±o¡u¤µ¤Ñ¡vªºª«¥ó
hour = today.getHours();	// ¨ú±o®É¼Æ
minute = today.getMinutes();	// ¨ú±o¤À¼Æ
second = today.getSeconds();	// ¨ú±o¬í¼Æ
prepand = (hour>=12)? "¤U¤È":"¤W¤È";	// §Q¥Î±ø¥ó¹Bºâ¤l¨Ó¨M©w¬O¡u¤W¤È¡v©Î¡u¤U¤È¡v
hour = (hour>=12)? hour-12:hour;	// §Q¥Î±ø¥ó¹Bºâ¤l¨Ó§ï¦¨12¤p®É¨î
document.write("²{¦b®É¶¡¬O"+prepand+hour+"ÂI"+minute+"¤À"+second+"¬í");
</script>

<hr>
</body>
</html>

¦b±ø¥ó±Ô­z¤¤¡AJavaScript ¦p¦ó§PÂ_¤@­Ó¹Bºâ¦¡¬O true ¡]¯u¡^©Î false ¡]°°¡^©O¡H¨ä®Ú¾Úªº­ì«h¦p¤U¡G

  1. ·í¹Bºâµ²ªG¬O¤@­Ó¼Æ­È®É¡A­Y¦¹¼Æ­Èµ¥©ó 0¡A«h¬O false¡A§_«h´N¬O true¡C
  2. ·í¹Bºâµ²ªG¬O¤@­Ó¦r¦ê®É¡A­Y¦¹¦r¦êµ¥©óªÅ¦r¦ê¡]""¡^¡A«h¬O false¡A§_«h´N¬O true¡C
¥H¤U³o¨Ç½d¨ÒÅýŪªÌ¦Û¦æ°Ñ¦Ò¤Î±À·Q¡G

±ø¥ó±Ô­z§P©wµ²ªG
0false
5true
-3true
""false
"0"true
"00"true
"0.0"true

¤U¦C½d¨Ò¦L¥X¤W­z§PÂ_±ø¥óªºµ²ªG¡G

Example¡]testIf.htm¡^¡G

¤W­z½d¨Òªº§¹¾ã­ì©lÀɮצp¤U¡G

­ì©lÀÉ¡]testIf.htm¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
<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 µ{¦¡³]­p»PÀ³¥Î¡G¥Î©óºô­¶¥Î¤áºÝ