JavaScriptで書いてみよう! (ステートメント)
ここではステートメントについて説明します。
条件ステートメント
構文:
if (condition) {
statements1
}
[else {
statements2
} ]
conditionがtrueのときstatements1を実行し、そうでなければstatements2を実行します。ifまたはelseの後に複数行のステートメントを使用したい場合はステートメントを括弧({})で囲んでください。
例:
function checkData () {
if (document.form1.threeChar.value.length == 3) {
return true
} else {
alert("Enter exactly three characters. " +
document.form1.threeChar.value + " is not valid.")
return false
}
}
switchステートメント
構文:
switch (expression){
case label :
statement;
break;
case label :
statement;
break;
...
default : statement;
}
expressionがlabelと一致するcaseにあるstatementを実行します。一致するcaseがない場合は、defaultにあるstatementを実行し、defaultが省略された場合は何もしません。breakが省略された場合は一致したcaseにあるstatementに続くすべてのstatementが実行されます。
例:
switch (expr) {
case "Oranges" :
document.write("Oranges are $0.59 a pound.<BR>");
break;
case "Apples" :
document.write("Apples are $0.32 a pound.<BR>");
break;
case "Bananas" :
document.write("Bananas are $0.48 a pound.<BR>");
break;
case "Cherries" :
document.write("Cherries are $3.00 a pound.<BR>");
break;
default :
document.write("Sorry, we are out of " + i + ".<BR>");
}
document.write("Is there anything else you'd like?<BR>");
繰り返しステートメント
構文:
for ([initialExpression]; [condition]; [incrementExpression]) {
statements
}
まずinitialExpressionが実行され、conditionがtrueである間、statementsを実行し、incrementExpressionを実行し、conditionを再評価します。
例:
<SCRIPT>
function howMany(selectObject) {
var numberSelected=0
for (var i=0; i < selectObject.options.length; i++) {
if (selectObject.options[i].selected==true)
numberSelected++
}
return numberSelected
}
</SCRIPT>
<FORM NAME="selectForm">
<P><B>Choose some music types, then click the button below:</B>
<BR><SELECT NAME="musicTypes" MULTIPLE>
<OPTION SELECTED> R&B
<OPTION> Jazz
<OPTION> Blues
<OPTION> New Age
<OPTION> Classical
<OPTION> Opera
</SELECT>
<P><INPUT TYPE="button" VALUE="How many are selected?"
onClick="alert ('Number of options selected: '
+ howMany(document.selectForm.musicTypes))">
</FORM>
do...whileステートメント
構文:
do {
statement
} while (condition)
まずstatementを実行し、conditionがtrueある間、statementを実行し、conditionを再評価します。
例:
do {
i+=1;
document.write(i);
} while (i<5);
whileステートメント
構文:
while (condition) {
statements
}
conditionがtrueである間、statementsを実行し、conditionを再評価します。
例1:
n = 0
x = 0
while( n < 3 ) {
n ++
x += n
}
labelステートメント
構文:
label :
statement
labelは識別子付きのステートメントを提供します。例えば、ループの識別にラベルを使えば、braekでループを抜ける、continueでループをやり直すなどの指示ができます。
例:
markLoop:
while (theMark == true) {
doSomething();
}
breakステートメント
構文:
1. break
2. break [label]
1.は最も内側のwhile、do-while、for、switchのブロックを抜けます。2.は指定されたlabelのブロックを抜けます。
例:
for (i = 0; i < a.length; i++) {
if (a[i] = theValue);
break;
}
continueステートメント
構文:
1. continue
2. continue [label]
continueはwhile、forの現在のループをやめ、次のループを始めます。whileではconditionの再評価に戻り、forではincrementExpressionに飛びます。labelはループを指示するlabelでなければなりません。
例1:
i = 0
n = 0
while (i < 5) {
i++
if (i == 3)
continue
n += i
}
例2:
checkiandj :
while (i<4) {
document.write(i + "<BR>");
i+=1;
checkj :
while (j>4) {
document.write(j + "<BR>");
j-=1;
if ((j%2)==0);
continue checkj;
document.write(j + " is odd.<BR>");
}
document.write("i = " + i + "<br>");
document.write("j = " + j + "<br>");
}
オブジェクト操作ステートメント
構文:
for (variable in object) {
statements }
objectのすべてのプロパティについてvariableをプロパティに変えながら、statementsを実行します。
例:
function dump_props(obj, obj_name) {
var result = ""
for (var i in obj) {
result += obj_name + "." + i + " = " + obj[i] + "<BR>"
}
result += "<HR>"
return result
}
makeとmodelのプロパティを持つオブジェクトcarの場合、結果は以下のようになります。
car.make = Honda
car.model = Integura
withステートメント
構文:
with (object){
statements
}
statementsの中で使用される基底のオブジェクトを指定します。
例:
var a, x, y
var r=10
with (Math) {
a = PI * r * r
x = r * cos(PI) //withなしの場合はMath.cos()
y = r * sin(PI/2) //withなしの場合はMath.sin()
}
コメント
JavaScriptは以下のコメントをサポートします。
・単数行のコメントは//に続きます。
・複数行のコメントは/*と*/で囲まれた範囲です。
例:
// これは単数行のコメントです。
/* これは複数行のコメントです。どれだけの長さになっても
構いませんし、ここに何でも記述できます。*/
ホームページへ戻る
|