4. JavaScript 控制语句

1. If...Else 语句

1.1 条件语句if

/*  格式:
1. if ...
2. if ... else ...
3. if ... else if .... else
4. switch
*/
var d = new Date().getDay(); 
var x;
switch (d) 
{
    case 1:
        x = "今天是星期一"; 
        break; 
    case 2:
        x = "今天是星期二"; 
        break; 
    case 3:
        x = "今天是星期三"; 
        break; 
    case 4:
        x = "今天是星期四"; 
        break; 
    case 5:
        x = "今天是星期五"; 
        break; 
    case 6:
        x = "今天是星期六"; 
        break; 
    default:
        x = '今天是星期日'
        break
}
console.log(x)

2. 循环语句

2.1 for 循环

var sum = 0;
for(var i = 1; i < 50; ++i)
{
    sum += i;
}
console.log(sum)

2.2 for/in 循环

var person = {
    fname : "John",
    lname : "Doe",
    age : 25
}; 

var txt;
for (x in person)
{
    console.log(x, person[x])
}

2.3 while 循环

var index = 0;
var sum = 0;
while(index <= 50)
{
    sum += index;
    ++index;
}
console.log(sum);

2.4 do/while 循环

var index = 0;
var sum = 0;
do
{
    sum += index;
    ++index;
}while(index <= 50)
console.log(sum);
posted @ 2016-09-18 16:31  zyh_think  阅读(134)  评论(0编辑  收藏  举报