Thread: Can someone explain this to me plz...

  1. #1
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179

    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.

  2. #2
    Addicted to the Internet netboy's Avatar
    Join Date
    Dec 2001
    Posts
    158
    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...
    It's unfulfilled dreams that keep you alive.

    //netboy

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User CAP's Avatar
    Join Date
    May 2002
    Posts
    179
    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
    -Microsofts Visual C++ Introductory Kit-
    Current Projects: Learning Everything C.

    Everyone has a photographic memory, some people just don't have any film.
    ______________________________

    When was the last time you went for a colon cleansing? Because quite frankly, you're so backed up with crap that it's spilling out your mouth

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. plz explain the output of the program.
    By raj_ksrt in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 03:43 AM
  2. Plz explain difference between functions returning ref
    By dudeomanodude in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2008, 01:03 PM
  3. plz explain a small function
    By samirself in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 10:14 AM
  4. plz explain
    By polonyman in forum C++ Programming
    Replies: 1
    Last Post: 09-12-2004, 04:15 AM
  5. Replies: 1
    Last Post: 05-07-2002, 04:18 AM