tencent cloud

Feedback

Statements

Last updated: 2024-11-21 18:34:18

    if Statements

    In WXS, you can use if statements in the following formats:
    if (expression) statement : Execute statement when expression is truthy.
    if (expression) statement1 else statement2 : Execute statement1 when expression is truthy. Otherwise, execute statement2.
    if ... else if ... else statementN Using this statement, you can choose one of the executions from statement1 to statementN.
    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 a variable, number, or string.
    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 and continue 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 execute statement or code block.
    Supports the use of break and continue keywords.
    
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support