#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmers Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use it's exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# Switch

Posts: 209Member
Hi!

I checked about the [b]switch[/b] logic.
Doesn't seem like it allows you to initialise like this?

[code]
case '0-39': 20
case '40-79': 40
[/code]

Meaning, if the value passed was 0 to 39 (e.g 1 or 20 or 38), then assign [b]20[/b].

Is [b]switch[/b] for only single-value evaluation?
For example,

[code]
case 'A': 20 //If the value is 'A', then assign 20
case 'B': 40 //If the value is 'B', then assign 40
[/code]

Thanks!!
· ·

• Posts: 3,711Member
: Hi!
:
: I checked about the [b]switch[/b] logic.
: Doesn't seem like it allows you to initialise like this?
:
: [code]
: case '0-39': 20
: case '40-79': 40
: [/code]
:
: Meaning, if the value passed was 0 to 39 (e.g 1 or 20 or 38), then assign [b]20[/b].

[blue]No, that is not valid in C/C++.
You would need to use a bunch of if-else statements.
[/blue]

:
: Is [b]switch[/b] for only single-value evaluation?
: For example,
:
: [code]
: case 'A': 20 //If the value is 'A', then assign 20
: case 'B': 40 //If the value is 'B', then assign 40
: [/code]
:
:
: Thanks!!
:

[blue]No, that is not valid either.

Do something like this:

[code]
switch(ch)
{
case 'A':
ch = 20;
break;

case 'B':
ch = 40;
break;

case 'C':
case 'D':
ch = 50;
break;

default:
ch = 0;
}
[/code]

[/blue]

· ·
• Posts: 85Member
: [code]
: case '0-4': 3
: case '5-9': 8
: [/code]
For your example it would be easier to use:
[code]
int x;
if(x >= 0 && x <= 4) {
x = 3;
}
int y;
if(y >= 5 && y <= 9) {
y = 8;
}
[/code]

I think the previous way is easier to understand, but can also be accomplished by the following:
[code]
int x;
switch(x) {
case 0:
case 1:
case 2:
case 3:
case 4:
x = 4;
break;
default:
// what happens if value is not matched?
}
int y;
switch(y) {
case 5:
case 6:
case 7:
case 8:
case 9:
y = 8;
break;
default:
// what happens if value is not matched?
}
[/code]
:
: [code]
: case 'A': 20 //If the value is 'A', then assign 20
: case 'B': 40 //If the value is 'B', then assign 40
: [/code]
:
Some newer compilers like C# support this without any problem. There is a work around for older compilers, but it is uglier then a bunch of nested if()else statements.
· ·
• Posts: 3,711Member
: [code]
: int x;
: switch(x) {
: case 0:
: case 1:
: case 2:
: case 3:
: case 4:
: x = 4;
: break;
: default:
: // what happens if value is not matched?
: }
: int y;
: switch(y) {
: case 5:
: case 6:
: case 7:
: case 8:
: case 9:
: y = 8;
: break;
: default:
: // what happens if value is not matched?
: }
: [/code]

[blue]Not that the above will be very slow compared to if-statements.[/blue]

: :
: : [code]
: : case 'A': 20 //If the value is 'A', then assign 20
: : case 'B': 40 //If the value is 'B', then assign 40
: : [/code]
: :
: Some newer compilers like C# support this without any problem. There is a work around for older compilers, but it is uglier then a bunch of nested if()else statements.

[blue]Err... C# is not a "newer compiler", it is a totally different language. It is not valid syntax in C/C++.[/blue]

· ·
• Posts: 621Member
: : [code]
: : int x;
: : switch(x) {
: : case 0:
: : case 1:
: : case 2:
: : case 3:
: : case 4:
: : x = 4;
: : break;
: : default:
: : // what happens if value is not matched?
: : }
: : int y;
: : switch(y) {
: : case 5:
: : case 6:
: : case 7:
: : case 8:
: : case 9:
: : y = 8;
: : break;
: : default:
: : // what happens if value is not matched?
: : }
: : [/code]
:
: [blue]Not that the above will be very slow compared to if-statements.[/blue]
:
:
: : :
: : : [code]
: : : case 'A': 20 //If the value is 'A', then assign 20
: : : case 'B': 40 //If the value is 'B', then assign 40
: : : [/code]
: : :
: : Some newer compilers like C# support this without any problem. There is a work around for older compilers, but it is uglier then a bunch of nested if()else statements.
:
: [blue]Err... C# is not a "newer compiler", it is a totally different language. It is not valid syntax in C/C++.[/blue]
:
C# doesn't support this either.

Greets,
Eric Goldstein
http://www.gvh-maatwerk.nl/english/E_index.htm

· ·
• Posts: 669Member ✭✭
You *might* be able to use something like result = (test_val < 40 ? 20 : test_val < 80 ? 40 : default_value) depending on your need.

: Hi!
:
: I checked about the [b]switch[/b] logic.
: Doesn't seem like it allows you to initialise like this?
:
: [code]
: case '0-39': 20
: case '40-79': 40
: [/code]
:
: Meaning, if the value passed was 0 to 39 (e.g 1 or 20 or 38), then assign [b]20[/b].
:
: Is [b]switch[/b] for only single-value evaluation?
: For example,
:
: [code]
: case 'A': 20 //If the value is 'A', then assign 20
: case 'B': 40 //If the value is 'B', then assign 40
: [/code]
:
:
: Thanks!!
:
· ·
• Posts: 209Member

:
: [blue]No, that is not valid either.
:
: Do something like this:
:
: [code]
: switch(ch)
: {
: case 'A':
: ch = 20;
: break;
:
: case 'B':
: ch = 40;
: break;
:
: case 'C':
: case 'D':
: ch = 50;
: break;
:
: default:
: ch = 0;
: }
: [/code]
:
: [/blue]
:

Oh I see, that's right!
Thanks!!

Noted SIWTCH won't work for 0-30 kind of cases.

· ·
• Posts: 209Member

: [/code]
:
: I think the previous way is easier to understand, but can also be accomplished by the following:
: [code]
: int x;
: switch(x) {
: case 0:
: case 1:
: case 2:
: case 3:
: case 4:
: x = 4;
: break;
: default:
: // what happens if value is not matched?
: }
: int y;
: switch(y) {
: case 5:
: case 6:
: case 7:
: case 8:
: case 9:
: y = 8;
: break;
: default:
: // what happens if value is not matched?
: }
: [/code]

Oh, this is GOOD!
Thanks for telling me this!
· ·
• Posts: 209Member
: You *might* be able to use something like result = (test_val < 40 ? 20 : test_val < 80 ? 40 : default_value) depending on your need.
:
: : Hi!
: :
: : I checked about the [b]switch[/b] logic.
: : Doesn't seem like it allows you to initialise like this?
: :
: : [code]
: : case '0-39': 20
: : case '40-79': 40
: : [/code]
: :
: : Meaning, if the value passed was 0 to 39 (e.g 1 or 20 or 38), then assign [b]20[/b].
: :
: : Is [b]switch[/b] for only single-value evaluation?
: : For example,
: :
: : [code]
: : case 'A': 20 //If the value is 'A', then assign 20
: : case 'B': 40 //If the value is 'B', then assign 40
: : [/code]
: :
: :
: : Thanks!!
: :
:

I see, you mean use the function and its return value to valify the logic?
· ·