Skip to content

Statement

If statement

In WXS, you can use the following format of if statement:

  • When expression is truthy, execute statement;
  • When expression is truthy, execute statement1. Otherwise, execute statement2;
  • if ... else if ... else statementN Through this sentence pattern, you can choose one of statement1 ~ statementN to execute.

Sample code:

js
// 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 statement

Sample code:

js
switch (Expression) {
  case Variable:
    Statement;
  case Number:
    Statement;
    break;
  case String:
    Statement;
  default:
    Statement;
}
  • Default branch can be omitted

  • The case keyword can only be followed by: variables, numbers, strings

Sample code:

js
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

js
number 10

for statement

Example syntax:

js
for (Statement; Statement; Statement)
  Statement;

for (Statement; Statement; Statement) {
  Code block;
}

Supports the use of break and continue keywords.

Sample code:

js
for (var i = 0; i < 3; ++i) {
  console.log(i);
  if( i >= 1) break;
}

Output:

js
0
1

while statement

Sample code:

js
while (Expression)
  Statement;

while (Expression){
  Code block;
}

do {
  Code block;
} while (Expression)
  • When the expression is true, loop and execute statements or code blocks;
  • Supports the use of break and continue keywords.