Select Case: Difference between revisions

From NSB App Studio
Jump to navigation Jump to search
(Add javascript snippet)
No edit summary
 
Line 14: Line 14:
Select Case evaluates a test expression, to conditionally execute one of several groups of statements. An ''expressionlistN'' component is required for every optional Case clause inside of Select Case. The optional component, ''statementsN'' is the group of statements to be executed when ''testexpression'' matches any expression in an ''expressionlistN''. Inside a Case clause, statements are executed up to the next Case clause or End Select; after all statements in a Case clause are executed, execution continues with the next statement after End Select. If ''testexpression'' doesn't match any expression in any of the expressionlists and Case Else is included, ''elsestatements'' are executed. If Case Else is not included and ''testexpression'' doesn't match any expression in any of the ''expressionlists'', execution continues with the next statement after End Select.
Select Case evaluates a test expression, to conditionally execute one of several groups of statements. An ''expressionlistN'' component is required for every optional Case clause inside of Select Case. The optional component, ''statementsN'' is the group of statements to be executed when ''testexpression'' matches any expression in an ''expressionlistN''. Inside a Case clause, statements are executed up to the next Case clause or End Select; after all statements in a Case clause are executed, execution continues with the next statement after End Select. If ''testexpression'' doesn't match any expression in any of the expressionlists and Case Else is included, ''elsestatements'' are executed. If Case Else is not included and ''testexpression'' doesn't match any expression in any of the ''expressionlists'', execution continues with the next statement after End Select.


== Example (Basic) ==
== Example ==


<pre>
<tabber>
Rem Select Case Example
JavaScript=
'Select Case performs conditional execution
<syntaxhighlight lang="JavaScript">
CheckHat("Blue")
CheckHat("Orange")
Sub CheckHat(Hat)
  Select Case Hat
  Case "Blue"
    Print "Kyle's hat"
  Case "Green"
    Print "Stan's hat"
  Case "Cyan"
    Print "Eric's hat"
  Case "Orange", "Hood"
    Print "Kenny's hat"
  Case "White"
    Print "Chef's hat"
  Case "Striped"
    Print "Mr. Hat"
  Case "Christmas", "Santa"
    Print "Mr. Hankey's hat"
  Case Else
    Print "Unknown Hat"
  End Select
End Sub
</pre>
 
== Example (JavaScript) ==
<pre>
// Select Case Example
// Select Case Example
/* Select Case performs conditional execution */
/* Select Case performs conditional execution */
Line 78: Line 52:
CheckHat("Blue");
CheckHat("Blue");
CheckHat("Orange");
CheckHat("Orange");
</pre>
</syntaxhighlight>
|-|
BASIC=
<syntaxhighlight lang="vb.net">
Rem Select Case Example
'Select Case performs conditional execution
CheckHat("Blue")
CheckHat("Orange")
Sub CheckHat(Hat)
  Select Case Hat
  Case "Blue"
    Print "Kyle's hat"
  Case "Green"
    Print "Stan's hat"
  Case "Cyan"
    Print "Eric's hat"
  Case "Orange", "Hood"
    Print "Kenny's hat"
  Case "White"
    Print "Chef's hat"
  Case "Striped"
    Print "Mr. Hat"
  Case "Christmas", "Santa"
    Print "Mr. Hankey's hat"
  Case Else
    Print "Unknown Hat"
  End Select
End Sub
</syntaxhighlight>
</tabber>


== Output ==
== Output ==

Latest revision as of 23:11, 24 July 2019

Select Case testexpression

[Case expressionlistA
[statementsA]]
[Case expressionlistB
[statementsB]]
[Case expressionlistC
[statementsC]]...
[Case Else
[elsestatements]]

End Select

Description

Select Case evaluates a test expression, to conditionally execute one of several groups of statements. An expressionlistN component is required for every optional Case clause inside of Select Case. The optional component, statementsN is the group of statements to be executed when testexpression matches any expression in an expressionlistN. Inside a Case clause, statements are executed up to the next Case clause or End Select; after all statements in a Case clause are executed, execution continues with the next statement after End Select. If testexpression doesn't match any expression in any of the expressionlists and Case Else is included, elsestatements are executed. If Case Else is not included and testexpression doesn't match any expression in any of the expressionlists, execution continues with the next statement after End Select.

Example

// Select Case Example
/* Select Case performs conditional execution */

function CheckHat(Hat) {
  switch (true) {
    case (Hat == "Blue"):
      NSB.Print("Kyle's hat");
      break;
    case (Hat == "Green"):
      NSB.Print("Stan's hat");
      break;
    case (Hat == "Cyan"):
      NSB.Print("Eric's hat");
      break;
    case (Hat == "Orange"): case (Hat == "Hood"):
      NSB.Print("Kenny's hat");
      break;
    case (Hat == "White"):
      NSB.Print("Chef's hat");
      break;
    case (Hat == "Striped"):
      NSB.Print("Mr. Hat");
      break;
    case (Hat == "Christmas"): case (Hat == "Santa"):
      NSB.Print("Mr. Hankey's hat");
      break;
    default:
      NSB.Print("Unknown Hat");
  }
}

CheckHat("Blue");
CheckHat("Orange");

Rem Select Case Example
'Select Case performs conditional execution
CheckHat("Blue")
CheckHat("Orange")
Sub CheckHat(Hat)
  Select Case Hat
  Case "Blue"
    Print "Kyle's hat"
  Case "Green"
    Print "Stan's hat"
  Case "Cyan"
    Print "Eric's hat"
  Case "Orange", "Hood"
    Print "Kenny's hat"
  Case "White"
    Print "Chef's hat"
  Case "Striped"
    Print "Mr. Hat"
  Case "Christmas", "Santa"
    Print "Mr. Hankey's hat"
  Case Else
    Print "Unknown Hat"
  End Select
End Sub

Output

Kyle's hat
Kenny's hat

Related Items

If...Then...Else