# Statements
# if Statements
In WXS, you can use if
statements in the following formats:
if (expression) statement
: Executestatement
whenexpression
is truthy.if (expression) statement1 else statement2
: Executestatement1
whenexpression
is truthy. Otherwise, executestatement2
.if ... else if ... else statementN
Using this statement, you can choose one of the executions fromstatement1
tostatementN
.
Example Syntax:
// if ...
if (Expression) Statement;
if (Expression)
Statement;
if (Expression) {
Code block;
}
// if ... else
if (Expression) Statement;
else Statement;
if (Expression)
Statement;
else
Statement;
if (Expression) {
Code block;
} else {
Code block;
}
// if ... else if ... else ...
if (Expression) {
Code block;
} else if (Expression) {
Code block;
} else if (Expression) {
Code block;
} else {
Code block;
}
# switch Statements
Example Syntax:
switch (Expression) {
case Variable:
Statement;
case Number:
Statement;
break;
case String:
Statement;
default:
Statement;
}
default
branches can be omitted.- After the
case
keyword, you can only use avariable
,number
, orstring
.
Sample code:
var exp = 10;
switch ( exp ) {
case "10":
console.log("string 10");
break;
case 10:
console.log("number 10");
break;
case exp:
console.log("var exp");
break;
default:
console.log("default");
}
Output:
number 10
# for Statements
Example Syntax:
for (Statement; Statement; Statement)
Statement;
for (Statement; Statement; Statement) {
Code block;
}
- Supports the use of
break
andcontinue
keywords.
Sample code:
for (var i = 0; i < 3; ++i) {
console.log(i);
if( i >= 1) break;
}
Output:
0
1
# while Statements
Example Syntax:
while (Expression)
Statement;
while (Expression){
Code block;
}
do {
Code block;
} while (Expression)
- When
expression
is true, loop executestatement
orcode block
. - Supports the use of
break
andcontinue
keywords.