-
Notifications
You must be signed in to change notification settings - Fork 35
Statements
Jing Lu edited this page May 15, 2013
·
13 revisions
See Full Language Specification.
syntax:
if (boolean-expression) expression-statement | block
[else expression-statement | block]
e.g.:
if (user_name == 'guest') quit();
or
if (user_name == 'admin') {
var user = new User();
user.role = 'admin';
}
Since the expression-statement in else could contains it...else self, so a if...else statement could be repeatedly used:
if (user_role == 'guest') {
...
} else if (user_role == 'admin') {
...
}
But an ambiguous statement as below is not recommended:
if (a < 5)
return 20;
else if (a < 10) // not good sample
return 30;
else
return 40;
Always use block instead of expression-statement is strongly recommended:
if (a < 5) {
return 20;
} else if (a < 10) { // good sample
return 30;
} else {
return 40;
}
for statement used to loop until condition is met. (boolean-expression to be true)
syntax:
for ( [local-]variable-declaration; boolean-expression; expression-statement )
expression-statement | block
e.g.:
var total = 0;
for (var i = 0; i < 10; i++) {
total += i;
}
console.log(total);
The result is:
45
for...in used to iterate over an object which could be enumerable. (supported by implements IEnumerable in .Net)
By default the following built-in types in ReoScript could be enumerable:
- Array
- Object
- String
var arr = [1,5,'ok',false];
for (element in arr) {
console.log(element);
}
The result is:
1
5
ok
false
var obj = {name: 'apple', color: 'red', amount: 5};
for (key in obj) {
console.log(key + ': ' + obj[key]);
}
The result is:
name: apple
color: red
amount: 5
var str = 'abc';
for (char in str) {
console.log(char);
}
The result is:
a
b
c