# sentence

# If statement

In WXS, you can useifstatements in the following format:

  • if (expression) statement: Whenexpressionis truthy, executestatement.

  • if (expression) statement1 else statement2: Whenexpressionis truthy, executestatement1. Otherwise, executestatement2

  • if ... else if ... else statementN With this pattern, you can choose betweenstatement1~statementN.

Example syntax:

// if ...

if (表达式) 语句;

if (表达式)
  语句;

if (表达式) {
  代码块;
}


// if ... else

if (表达式) 语句;
else 语句;

if (表达式)
  语句;
else
  语句;

if (表达式) {
  代码块;
} else {
  代码块;
}

// if ... else if ... else ...

if (表达式) {
  代码块;
} else if (表达式) {
  代码块;
} else if (表达式) {
  代码块;
} else {
  代码块;
}

# Switch statement

Example syntax:

switch (表达式) {
  case 变量:
    语句;
  case 数字:
    语句;
    break;
  case 字符串:
    语句;
  default:
    语句;
}
  • DefaultBranches can be omitted without writing.
  • The casekeyword can only be followed by:variable [, number, character string [.

Example 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 statement

Example syntax:

for (语句; 语句; 语句)
  语句;

for (语句; 语句; 语句) {
  代码块;
}
  • Support usingbreak,continuekeyword.

Example code:

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

Output:

0
1

# While statement

Example syntax:

while (表达式)
  语句;

while (表达式){
  代码块;
}

do {
  代码块;
} while (表达式)
  • When theexpressionis true, the loop executes thestatementor thecode block.
  • Support usingbreak,continuekeyword.