How to use switch?

 
switch (x)
{
case 1 to 4: g=1;break;
case 5 to 7: g=2;break;
.
.
.
}

not working

even 

case 1 - 4

 
Avi: switch (x) { case 1 to 4: g=1;break; case 5 to 7: g=2;break; . . . }

Please read the documentation attentively ... Documentation on MQL5: Language Basics / Operators / Switch Operator

switch (x)
{
   case 1:
   case 2:
   case 3:
   case 4: g=1; break;
   case 5:
   case 6:
   case 7: g=2; break;
.
.
.
}
Documentation on MQL5: Language Basics / Operators / Switch Operator
Documentation on MQL5: Language Basics / Operators / Switch Operator
  • www.mql5.com
Switch Operator - Operators - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Please read the documentation attentively ... Documentation on MQL5: Language Basics / Operators / Switch Operator

Thank you I read that before

But what if I need:

case 1 to 50:

case 52 to 270:

case > 270

.

.

 
Avi #: Thank you I read that before. But what if I need: case 1 to 50: case 52 to 270: case > 270

It is called "switch" for a reason.

If you want to define ranges then use a conditional "if-else" operator instead of a "switch":

     if( x >=   1 && x <=  50 ) { ... }
else if( x >=  52 && x <= 270 ) { ... }
else if( x >  270             ) { ... }
else                            { ... }
  
Documentation on MQL5: Language Basics / Operators / Conditional Operator if-else
Documentation on MQL5: Language Basics / Operators / Conditional Operator if-else
  • www.mql5.com
Conditional Operator if-else - Operators - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

It is called "switch" for a reason.

If you want to define ranges then use a conditional "if-else" operator instead of a "switch":

OK thank you