PC SOFT

WINDEVWEBDEV AND WINDEV MOBILE
ONLINE HELP

Home | Sign in | English UK
  • 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/iPadApple WatchUniversal Windows 10 AppWindows Mobile
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.
Versions 16 and later
Universal Windows 10 App This feature is available for the Windows Phone applications.
New in version 16
Universal Windows 10 App This feature is available for the Windows Phone applications.
Universal Windows 10 App This feature is available for the Windows Phone applications.
Versions 17 and later
iPhone/iPad This feature is available for the iPhone/iPad applications.
New in version 17
iPhone/iPad This feature is available for the iPhone/iPad applications.
iPhone/iPad This feature is available for the iPhone/iPad applications.
Versions 18 and later
Universal Windows 10 App This feature is available in Windows Store apps mode.
Android Widget This feature is available in Android Widget mode.
New in version 18
Universal Windows 10 App This feature is available in Windows Store apps mode.
Android Widget This feature is available in Android Widget mode.
Universal Windows 10 App This feature is available in Windows Store apps mode.
Android Widget This feature is available in Android Widget mode.
Versions 21 and later
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
New in version 21
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
Universal Windows 10 App This feature is available in Universal Windows 10 App mode.
Apple Watch This feature is available in Apple Watch mode.
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 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 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 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()
Note: 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.
Versions 18 and later
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.
Some examples:
  • 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
New in version 18
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.
Some examples:
  • 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
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.
Some examples:
  • 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
Switch Case com n opções em cada case só usar virgula
SWITCH NUM

CASE 2,4,52,63
....

CASE 43
...


CASE 55
...


CASE 62,64,79,12
...


OTHER CASE
...

END
BOLLER
26 Nov. 2019
video SWITCH
https://youtu.be/NNuZF8I38i4
https://windevdesenvolvimento.blogspot.com/2019/09/dicas-2332-windev-webdev-mobile.html
// SELECTING A ROW OF CAR_CARROSEL
SWITCH CAR_CARROSEL
CASE 1
Info("CADASTRO CLIENTE")
CASE 2
Info("CADASTRO REPRESENTANTE")

CASE 3
Info("CADASTRO TRANSPORTADORA")
OTHER CASE
END
amarildo
30 Sep. 2019
Exemplo Dia da Semana em Numeros
diaSemana is string = Lower(DateToDayInAlpha(gData))

SWITCH Lower(diaSemana)
CASE "segunda-feira" : diaSemana = 1
CASE "terça-feira" : diaSemana = 2
CASE "quarta-feira" : diaSemana = 3
CASE "quinta-feira" : diaSemana = 4
CASE "sexta-feira" : diaSemana = 5
CASE "sábado" : diaSemana = 6
CASE "domingo" : diaSemana = 7
OTHER CASE : diaSemana = 0
END
BOLLER
09 Apr. 2019
Example
SWITCH UF

CASE "AC" : x = 1
CASE "AL" : x = 2
CASE "AP" : x = 3
CASE "AM" : x = 4
CASE "BA" : x = 5
CASE "CE" : x = 6
CASE "DF" : x = 7
CASE "ES" : x = 8
CASE "GO" : x = 9
CASE "MA" : x = 10
CASE "MT" : x = 11
CASE "MS" : x = 12
CASE "MG" : x = 13
CASE "PA" : x = 14
CASE "PB" : x = 15
CASE "PR" : x = 16
CASE "PE" : x = 17
CASE "PI" : x = 18
CASE "RJ" : x = 19
CASE "RN" : x = 20
CASE "RS" : x = 21
CASE "RO" : x = 22
CASE "RR" : x = 23
CASE "SC" : x = 24
CASE "SP" : x = 25
CASE "SE" : x = 26
CASE "TO" : x = 27

END

ListSelectPlus(COMBO_T006_UF,x)
BOLLER
02 Apr. 2019
Exemplo SWITCH
Exemplo SWITCH

EDT_resultado=""
SWITCH EDT_idade
CASE <= 18
EDT_resultado="Menor ou igual 18 anos"
CASE >=60
EDT_resultado="Maior ou igual que 60"
CASE 19 : EDT_resultado="19 anos"
CASE 20
EDT_resultado="20 anos"
CASE 21,22,23
EDT_resultado="21 22 23"
CASE 24 TO 30
EDT_resultado="entre 24 a 30 anos"
CASE 31 <*< 40
EDT_resultado="Entre 32 ate 39 anos"
OTHER CASE
END

//Blog com Video e Exemplo
http://windevdesenvolvimento.blogspot.com.br/2016/10/aula-934-windev-comandos-2-switch.html
https://www.youtube.com/watch?v=XAmIYCMb_ao

De matos
08 Oct. 2016