# Can someone explain this to me plz...

• 07-22-2002
CAP
Can someone explain this to me plz...
Ok I have tried to use the 'case and switch' thing in a program and I can't get it to work for one thing but that isn't all too important, what is important is that I would like someone with some time on their hands to explain how it works...
I know it goes something(don't yell if not even close ;))

Code:

```int a,b,c,d; switch(d) {     case 'a' {whatever}     case 'b' {whatever}     case 'c' {whatever}```
I completely realize that isn't close but what I am confused about is how you check the thing in brackets after switch to the things in brackets with the cases. If I wanted the program to say take in user input, place it in d, and then go to each of the cases and match it(like if it was 1 then do case a or whatever)but I dont' know how it checks that out...do I have to define what a b c are ahead of time for it to work?

Thanks in advance and once again sorry bout the sloppy example.
• 07-22-2002
netboy
Switch is actually a selection case similar to If-Then-Else. The switch statement will check for a condition that match the case condition and then perform the function or the statement in it as if you are using If-Then-Else.

Example: If you want to print out Male if user input m and print out Female if user key in f.

Code:

```char gender printf("Enter your gender: "); scanf("%c", &gender); switch(gender) {     case 'm' : printf("Your gender is male\n");                     break;     case 'f' : printf("Your gender is female\n");                   break;     default: break; }```
Understand how it actually worked? The default case is actually for wrong or unmatched condition...
• 07-22-2002
quzah
A few things to keep in mind:

Code:

```int x,y; y = prompt_for_y( ); switch( y ) {     case 'x': /* this is the character x, not the variable x */     char x: /*this would work if x was a constant variable and not just a variable */     char 1: /* this means if y is the decimal value of 1 then this is true */     char '1': /* this would be if y contained the decimal value for the character '1' */ }```
Furthermore:

Code:

```case <something>:     { /* optional */         printf("Wheee...");         printf("Ooooo...");         printf("Aaaa...");     } /* optional */ break; /* optional */```
Anything tagged "optional" is just that. You do not need the { } in a cast statement unless you're going to define new variables there.

You do not need the break statement unless you don't want your case statement to "cascade" down the list. If you want it to only execute a single case, then you need a break statement.

Quzah.
• 07-23-2002
CAP
Thanks netboy and quzah, the examples helped, I just didn't understand how if you had switch(d) or whatever how the program would know what to do because I thought that(by an earlier example from somewhere) that case 'a' meant number 1 like it was first, not like it was a variable or if the answer was a do this, thanks though I get it now:D