-6

I've been working with VB.NET for years and I luv the language. Now dispersing myself into C++ I'm having a tough time to grasp the logics of C++.

Kindly tell me any workaround. Thanks a lot !

Bas
  • 469
  • 8
  • 22
user3636333
  • 15
  • 1
  • 1
  • 5
    That doesn't make sense.. How do you want to use `||` in `switch`? – Maroun May 14 '14 at 11:06
  • 2
    *This question appears to be offtopic because it is just a rant instead of elaborating what the user really wants to do with some code examples of what he tried* – PlasmaHH May 14 '14 at 11:07
  • 1
    Are you trying `switch(x) { case 0: case 1: /*this is an or */ break; case 3: break; }` ? –  May 14 '14 at 11:08
  • @MarounMaroun: `case 42: if( a || b ) { ... ` ? ^^ – PlasmaHH May 14 '14 at 11:08
  • instead of `||` you can specify multiple conditions in a switch case e.g. `case 1: case2: break;` – sgarizvi May 14 '14 at 11:08
  • 1
    Your question makes no sense, please re-phrase it and don't give subjective remarks about the language such as '"ill-logical" logics of C++' just because you do not understand it. – RamblingMad May 14 '14 at 11:08
  • Because there is not a reason. DIfferent languages and different syntax (for example complex switch expressions also in C# aren't allowed). – Adriano Repetti May 14 '14 at 11:08
  • @PlasmaHH I thought he wanted to use it **in** `switch`. – Maroun May 14 '14 at 11:09
  • @MarounMaroun: `switch(foo) { case 42: if( a || b ) { ...` ? ^^ – PlasmaHH May 14 '14 at 11:10
  • @PlasmaHH Again..... I though `switch(something || somethingElse)`. His title is confusing. – Maroun May 14 '14 at 11:10
  • Take a look also to this duplicate (C# instead of C++ but same concept): http://stackoverflow.com/questions/20744845/converting-from-vb-to-c-sharp/20744943#20744943 – Adriano Repetti May 14 '14 at 11:11
  • @Adriano: actually, there's an historical reason - `switch` was born as a way to make the compiler generate a jump table, hence the labels (and the fall through), the limitation to exact match and the constraint of integral types only. – Matteo Italia May 14 '14 at 11:18
  • @MatteoItalia for C (and for all languages with C-style syntax because of LAP) I completely agree. – Adriano Repetti May 14 '14 at 11:22
  • While the absence of question marks hints that there is no clear question, I would assume that the intention is: *I cannot do `||` in a case in C++ like in VB, is there a similar construct?* – David Rodríguez - dribeas May 14 '14 at 12:57

2 Answers2

4

That is quite an unclear question, but you are probably asking why you cannot do this:

switch(mynumber) { //assuming an int here
  case 1:
    printf("Something.");
    break;
  case 2 || 3: 
    printf("Something else.");
    break;
 }

This will not work as you expected: the || operator in C++ will actually do 2 || 3, evaluating to 1. Instead, you can replicate your case statements:

switch(mynumber) {
  case 1:
    printf("Something.");
    break;
  case 2: 
  case 3: 
    printf("Something else.");
    break;
 }
E_net4
  • 27,810
  • 13
  • 101
  • 139
2

Looks like VB.NET supports expression in case statements while C++ does not.

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • This actually answers the question :) – Maroun May 14 '14 at 11:10
  • 1
    I'm not sure. It does support expressions in case labels: `#include int main() { const bool y = false, z = true; switch (42) { case 41+(y||z): printf("42\n"); } return 0; }` - they just have to be compile time constants. – Jerry Jeremiah May 15 '14 at 00:18