if...else语句:
格式1:
if (<表达式>)
<语句1>;
else
<语句2>;
if (<表达式>) <语句1>;
else <语句2>;
例:
<Script>
var now = new Date();
var hour = now.getHours();
if (6 < hour && hour < 18)
document.write ("辛勤工作才能快乐收割!");
else
document.write ("休息一下,充电后再出发。");
</Script>
while语句:
格式1:
while (<表达式>)
语句;
格式2:
while (<表达式>){
<语句组>
}
do...while语句:
格式:
do {
<语句组>
} while (<表达式>)
for语句:
格式:
for (<初始表达式>;<条件表达式>;<变动量表达式>){
<语句组>
}
for...in语句:重复执行指定对象的所有属性
格式:
for ( 变量 in 对象 ){
<语句组>
}
例:
<Script>
function member(name, sex) {//构造函数member
this.name = name;
this.sex = sex;
}
function showProperty(obj, objString) {
var str = "";
for (var i in obj)
str += objString + "." + i + " = " + obj[i] + "<BR>";
return str;
}
papa = new member("杨宏文", "男生");//建立对象实例papa
document.write(showProperty(papa, "papa"))
</Script>
break语句:
格式:break
例:
<Script>
var i = 5;
while ( i > 0 ) {
if ( i == 3 ) break;
document.write("i = " ,i ,"<BR>");
i--;
}
</Script>
continue语句:
格式:continue
例:
<Script>
var i = 5;
while ( i > 0 ) {
i--;
if ( i == 3 ) continue;
document.write("i = " ,i ,"<BR>");
}
</Script>