Package: org.codehaus.groovy.ast.stmt

## [Java] Class SwitchStatement

- org.codehaus.groovy.ast.stmt.SwitchStatement

-     public class SwitchStatement
      extends Statement

  Represents a switch (object) { case value: ... case [1, 2, 3]: ... default: ... } statement in Groovy. A switch statement evaluates the control expression against a sequence of case values and executes the code associated with the matching case, or the default statement if no case matches.

  See Also:
    CaseStatement
    Statement

- - ### Constructor Summary

    Constructors
    Constructor and description
    `SwitchStatement(Expression expression)` Constructs a switch statement with the given control expression.
    `SwitchStatement(Expression expression, Statement defaultStatement)` Constructs a switch statement with the given control expression and default statement.
    `SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement)` Constructs a switch statement with the given control expression, case statements, and default statement.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addCase(CaseStatement caseStatement)` Adds a case statement to this switch.
    `` | `public CaseStatement` | `getCaseStatement(int idx)` Returns the case statement at the given index.
    `` | `public List<CaseStatement>` | `getCaseStatements()` Returns the list of case statements in this switch.
    `` | `public Statement` | `getDefaultStatement()` Returns the statement executed when no case matches the control expression.
    `` | `public Expression` | `getExpression()` Returns the control expression that is evaluated against case values.
    `` | `public void` | `setDefaultStatement(Statement defaultStatement)` Sets the statement executed when no case matches the control expression.
    `` | `public void` | `setExpression(Expression e)` Sets the control expression that is evaluated against case values.
    `` | `public void` | `visit(GroovyCodeVisitor visitor)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Statement` | `addStatementAnnotation, addStatementLabel, copyStatementLabels, getStatementAnnotations, getStatementLabel, getStatementLabels, isEmpty, setStatementLabel`
    `class ASTNode` | `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit`

- - ### Constructor Detail

    - #### public SwitchStatement(Expression expression)

      Constructs a switch statement with the given control expression. The default statement is initialized to EmptyStatement.INSTANCE.

      Parameters:
        `expression` - the expression to evaluate against case values
      See Also:
        SwitchStatement(Expression, Statement)

    - #### public SwitchStatement(Expression expression, Statement defaultStatement)

      Constructs a switch statement with the given control expression and default statement.

      Parameters:
        `expression` - the expression to evaluate against case values
        `defaultStatement` - the statement executed when no case matches; may be EmptyStatement.INSTANCE
      See Also:
        SwitchStatement(Expression, List, Statement)

    - #### public SwitchStatement(Expression expression, List<CaseStatement> caseStatements, Statement defaultStatement)

      Constructs a switch statement with the given control expression, case statements, and default statement.

      Parameters:
        `expression` - the expression to evaluate against case values
        `caseStatements` - the list of CaseStatement objects representing case branches
        `defaultStatement` - the statement executed when no case matches

  - ### Method Detail

    - #### public void addCase(CaseStatement caseStatement)

      Adds a case statement to this switch.

      Parameters:
        `caseStatement` - the CaseStatement to add

    - #### public CaseStatement getCaseStatement(int idx)

      Returns the case statement at the given index.

      Parameters:
        `idx` - the index of the case statement to retrieve
      Returns:
        the CaseStatement at the given index, or null if the index is out of bounds

    - #### public List<CaseStatement> getCaseStatements()

      Returns the list of case statements in this switch.

      Returns:
        a list of CaseStatement objects; never null

    - #### public Statement getDefaultStatement()

      Returns the statement executed when no case matches the control expression.

      Returns:
        the default Statement, or EmptyStatement.INSTANCE if not set

    - #### public Expression getExpression()

      Returns the control expression that is evaluated against case values.

      Returns:
        the control Expression

    - #### public void setDefaultStatement(Statement defaultStatement)

      Sets the statement executed when no case matches the control expression.

      Parameters:
        `defaultStatement` - the default Statement

    - #### public void setExpression(Expression e)

      Sets the control expression that is evaluated against case values.

      Parameters:
        `e` - the control Expression

    - #### public void visit(GroovyCodeVisitor visitor)