ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage syntax / Structured statements
  • Exiting from a SWITCH statement
  • SWITCH with test on a condition
  • OR keyword
  • OTHER CASE keyword
WINDEV
WindowsLinuxUniversal Windows 10 AppJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac CatalystUniversal Windows 10 App
Others
Stored procedures
The SWITCH statement is used to choose the action that will be run according to the value of an expression.
Two syntaxes are available for this statement:
  • Full syntax.
  • Simplified syntax for an integer value of consecutive values.
Example
SWITCH Quantity
CASE 1: Comment = "Take advantage of our promotions: buy one, get one free"
CASE 2: Comment = "Buy two products: get one free"
OTHER CASE: Comment = ""
END
SWITCH Value
CASE 10: Trace(10)
CASE 10<*<20: Trace("Included between 10 and 20")
CASE >30: Trace("Greater than 30")
END
SWITCH Quantity
CASE TO 8: // Value less than or equal to 8
Comment = "A gift offered for each order"
CASE 9,10: // Value equal to 9 or 10
Comment = "Take advantage of our promotions: buy one, get one free"
CASE 11 TO 20 // Value included between 11 and 20 (inclusive)
Comment = "Buy two products: get one free"
CASE > 20: // Value greater than 20
Comment = ""
END
// Simplified syntax: Combo_Sel contains numbers from 1 to 5
// A first name is displayed according to the selected number
Value is int = COMBO_sel[COMBO_sel]
Result1 is string = ""
Result1 = SWITCH (Value, "Vince", "Emma", "Liz", "Freddy", ELSE "Not found")
Syntax

Full syntax Hide the details

SWITCH <Comparison variable>
  CASE <Expression 1>: <Action 1>

  CASE <Expression 2>:
      <Action 2.1>
      <Action 2.2>

  CASE <Expression 3>
      <Action 3.1>
      <Action 3.2>
      <Action 3.3>

  CASE <Expression 4>, <Expression 5>: <Action 4>

  CASE <Expression 6>, <Expression 7>:
      <Action 5>

  CASE <Expression 8>, <Expression 9>
      <Action 6.1>
      <Action 6.2>

  CASE <Minimum expression> TO: <Action 7>

  CASE TO <Maximum expression>: <Action 8>

  CASE <Minimum expression> TO <Maximum expression>: <Action 9>

  CASE [= / ~= / ~~ / [= / > />= / < / <= / [~ / [~~ ] <Expression 10>: <Action 10>

  CASE <Minimum expression> [<= * <=] [< * <=] [<= * <] [< * <]  <Maximum expression>: <Action 11>

  ...
  [
  OTHER CASE: <Action other case>
  OR
  OTHER CASE:
  <Action other case>
  OR
  OTHER CASE
  <Action other case>
  ]
END
<SWITCH>:
Marks the beginning of the statement block.
<Comparison variable>:
Comparison variable (any type).
<CASE>:
Different comparison cases.
<Expression N>:
Value that must be compared to the comparison variable (same type as the comparison variable).
<Action N>:
Action to perform if the corresponding expression is equal to the comparison variable. Once this action has been performed, the process continues after the END keyword.
<OTHER CASE>:
Action to perform if the comparison variable is equal to no expression (optional).
<END>:
Marks the end of the statement block.

Simplified syntax for an integer value of consecutive values Hide the details

<Result> = SWITCH (<Value>, <Result 1>, <Result 2>, ... <Result 3> [ELSE <Other Result>])
<Result> = SWITCH (<Value>
      <Result 1>,
      <Result 2>,
      ...
      )
<Result>:
Value returned by SWITCH
<SWITCH>:
Marks the beginning of the statement block.
<Value>:
Comparison variable (any type).
<Result N>:
Returned value. Returns <Result 1> if <Value> is set to 1, <Result 2> if <Value> is set to 2, ...
<ELSE>:
Used to process the case when no result corresponds to the value. A WLanguage error occurs if this case is not processed.
<Other Result>:
Value returned if no result corresponds to the value.
Remarks

Exiting from a SWITCH statement

The BREAK SWITCH statement is used to exit from a SWITCH statement and to run the rest of the process.
The syntax is as follows:
SWITCH <Expression>
CASE <VALUE>
...
IF <Condition> THEN
BREAK SWITCH
END
END
For example:
SWITCH Quantity
CASE 1
 IF User = "Admin" THEN
BREAK SWITCH
 END
 Comment = "Take advantage of our promotions: buy one, get one free"
 
CASE 2:
 IF User = "Admin" THEN
BREAK SWITCH
 END
 Comment = "Buy two products: get one free"
 
OTHER CASE: Comment = ""
END

SWITCH with test on a condition

The result of a condition performed in a CASE statement can be checked in a SWITCH statement. This condition can be a complex condition containing calls to functions, ...
For example:
SWITCH True
// Check a complex condition
CASE Value1 = 10 AND Value2 = 20: Trace(10)
// Check a condition on a function
CASE CheckCondition()
Remark: to run a test on a simple condition, we recommend that you use the following syntax:
SWITCH Value
CASE 10: Trace(10)
CASE 10<*<20: Trace("Included between 10 and 20")
CASE >30: Trace("Greater than 30")
END

OR keyword

The OR keyword cannot be used to compare the value to an expression or to another one. The comma must be used. Example not to do:
Sub is int = 2
SWITCH Sub
CASE 1 OR 2: Info("case 1 or 2")
CASE 3: Info("case 3")
OTHER CASE: Info("other case")
END
to do:
Sub is int = 2
SWITCH Sub
CASE 1,2: Info("case 1 or 2")
CASE 3: Info("case 3")
OTHER CASE: Info("other case")
END

OTHER CASE keyword

OTHER CASE must be the last statement used in the conditional SWITCH statement.
Radio Button control: automatic SWITCH
When typing the SWITCH statement relative to a Radio Button control, the code editor automatically adds the CASE corresponding to the different options of the Radio Button control. The caption of each option is automatically added in line comment.
For example:
  • Code for a Radio Button control that returns an integer: typing "SWITCH RADIO_Title" in the code editor displays:
    Code for a Radio Button control that returns an integer
  • Code for a Radio Button control that returns a value: typing "SWITCH RADIO_TodaysMenu" in the code editor displays:
    Code for a Radio Button control that returns a value
Minimum version required
  • Version 9
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 06/07/2022

Send a report | Local help