# Switch Case Expressions

## Recommended Posts

I'm having trouble with syntax like this:

```#define NUM1 7

switch (var) {
case (1 << NUM1):
break;
case (1 << 2):
break;
}```

I know the cases must be integer constants, but in normal operation SourceBoost will optimize these expressions out to constants. I can work around the issue for now. I just wanted to point it out in case you guys hadn't noticed.

##### Share on other sites

... interesting way to use a case statement

##### Share on other sites
... interesting way to use a case statement

Switch is for comparing variables to constants. Sometimes you have constants that depend on other constants. It doesn't have to use the << operator, + or - have the same problems. Or take a case like this:
```#define NUM1 5
#define NUM2 3
#define DIFF (NUM1 - NUM2)
...
var = DIFF;
switch (var) {
case DIFF:
...```

Still, it's not critical to me. I just thought there was a chance Dave and Pavel might like to know.

##### Share on other sites

edt,

Still, it's not critical to me.  I just thought there was a chance Dave and Pavel might like to know.

Its been added to our jobs list, but its a low priority.

Regards

Dave

##### Share on other sites
... interesting way to use a case statement

Switch is for comparing variables to constants. Sometimes you have constants that depend on other constants. It doesn't have to use the << operator, + or - have the same problems. Or take a case like this:
```#define NUM1 5
#define NUM2 3
#define DIFF (NUM1 - NUM2)
...
var = DIFF;
switch (var) {
case DIFF:
...```

Still, it's not critical to me. I just thought there was a chance Dave and Pavel might like to know.

Comment was more regarding the reverse logic you were using.

Usually the cases are static values and you change the in test variable.

I can see where it would be useful, would hate to ever inherit any

code that used it though ...

## Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.