Node.js基本逻辑结构
循环结构
for…i
for (var i = 0; i < 5; i++) {
console.log(i)
};
for…in
var foo = {
"hello" : "world",
"node" : "js",
"blahblah" : "bar"
};
for(var key in foo) {
console.log(key + ": " + foo[key]);
}
while
var i = 0
while(i < 5) {
console.log(i)
i ++
}
do-while
var i = 5
do {
console.log(i)
i --
} while(i > 0)
分支结构
if-else
var a = 5
if(a == 5) {
console.log(a)
} else {
console.log(a)
}
if-else if
var a = 3
if(a == 5) {
console.log(a)
} else if (a == 3){
console.log(a + "aaa")
}
switch
var a = 3
switch(a) {
case 1 : console.log(1)
case 2 : console.log(2)
case 3 : console.log(3)
case 4 : console.log(4);break
default: console.log("什么都不是")
}
注意:break要加,这个跟Swift不一样,跟其他语言的语法一致