c++ Switches?

Sharky Forums


Results 1 to 9 of 9

Thread: c++ Switches?

  1. #1
    Tiger Shark andy3109's Avatar
    Join Date
    Sep 2001
    Location
    Syracuse
    Posts
    607

    c++ Switches?

    Could someone explain switches to me in a way I can understand. It's not to clear to me how the book I am reading is explaining it. Thanks in advance.
    -andy
    AMD 1600+ @ 1.90ghz - Mushkin PC2100 CAS2 KR7A-RAID Mobo - Geforce III Ti200
    75GB WD and 30GB Maxtor (both 7200RPM)
    Two smart fan 2 & two generic 80MM fans
    19' flat screen
    52x cd rom - 12x Burner floppy - Tape, bolts, clench grip
    and most importantely..your mom
    IM PART OF THE COOLERS GUILD, I HAD NO IDEA!

  2. #2
    Ultra Great White Shark!! richardginn's Avatar
    Join Date
    Feb 2001
    Posts
    16,118
    www.myeducational plan.com-come see my plan to fix the USA educational system. I hope this is sig legal. Major Site Design Update on July 18, 2006. On June 18, 2009 passed the 10,000 post mark. December 24, 2009: Major Theme change and more....

  3. #3
    Tiger Shark andy3109's Avatar
    Join Date
    Sep 2001
    Location
    Syracuse
    Posts
    607
    understand. Now, are switch statements important in C++? Anyone use them in their projects? Thx.
    -andy
    AMD 1600+ @ 1.90ghz - Mushkin PC2100 CAS2 KR7A-RAID Mobo - Geforce III Ti200
    75GB WD and 30GB Maxtor (both 7200RPM)
    Two smart fan 2 & two generic 80MM fans
    19' flat screen
    52x cd rom - 12x Burner floppy - Tape, bolts, clench grip
    and most importantely..your mom
    IM PART OF THE COOLERS GUILD, I HAD NO IDEA!

  4. #4
    Mako Shark dighn's Avatar
    Join Date
    Nov 2000
    Location
    Vancouver, BC Canada
    Posts
    3,171
    Originally posted by andy3109
    understand. Now, are switch statements important in C++? Anyone use them in their projects? Thx.
    -andy
    yes it's useful when you wnt to test for a lot of integral values. heck lot of cleaner and easier to type/write than a bunch of else ifs, and you can leave out "break" to do some special things, a boring example would be to have the same code for two cases. eg:

    case 1:
    foo();
    break;

    case 3:
    case 4:
    bar();
    break;

    you will use it sometime.
    Last edited by dighn; 12-15-2002 at 05:29 AM.
    .

  5. #5
    Reef Shark
    Join Date
    Dec 2001
    Location
    Henrietta, New York, USA
    Posts
    264
    Switches are much faster then chained if-elses.

  6. #6
    Tiger Shark andy3109's Avatar
    Join Date
    Sep 2001
    Location
    Syracuse
    Posts
    607
    can you do cases that make perform a function if x<30 or something like that?
    -andy
    AMD 1600+ @ 1.90ghz - Mushkin PC2100 CAS2 KR7A-RAID Mobo - Geforce III Ti200
    75GB WD and 30GB Maxtor (both 7200RPM)
    Two smart fan 2 & two generic 80MM fans
    19' flat screen
    52x cd rom - 12x Burner floppy - Tape, bolts, clench grip
    and most importantely..your mom
    IM PART OF THE COOLERS GUILD, I HAD NO IDEA!

  7. #7
    Catfish
    Join Date
    Mar 2002
    Posts
    109
    No, a switch only can only compare the variable to a single number or a single letter, not an expression. Use an if statement for that.

  8. #8
    Registered User peterlak's Avatar
    Join Date
    Dec 2002
    Location
    Bolton, Ontario, canada
    Posts
    8
    please dont mine my late post. i am new to this form.


    the reason we have swites is cause c technically does not have something called an "else if" it rather has something else called "if else".

    the following...

    if (x=1)
    printf("1");
    else if (x=2)
    printf("2");
    else
    printf("3");

    is the same thing as

    if (x=1)
    {
    printf("1");
    }
    else
    {
    if (x=2)
    printf("2");
    else
    printf("3");
    }

    wich is the same as...

    if (x=1)
    printf("1");
    else
    if (x=2)
    printf("2");
    else
    printf("3");

    wich is the same as...

    if (x=1)
    printf("1");
    else if (x=2)
    printf("2");
    else
    printf("3");


    now do you see how the else if come about? the switch statment does the same thing more efficiantly from a low level perspective.

  9. #9
    Hammerhead Shark m316foley's Avatar
    Join Date
    Nov 2000
    Posts
    2,706
    good example of a switch:

    [code]
    int days=0;
    cout << "Enter the number of days: ";
    cin >> days;

    cout << "On the " << days << " day of Christmas my true love sent to me: " << endl;
    switch(days)
    {
    case 12:
    cout << "12 Drummers Drumming" << endl;
    case 11:
    cout << "Eleven Pipers Piping" << endl;
    case 10:
    cout << "Ten Lords a Leaping" << endl;
    case 9:
    cout << "Nine Ladies Dancing" << endl;
    case 8:
    cout << "Eight Maids a Milking" << endl;
    case 7:
    cout << "Seven Swans a Swimming" << endl;
    case 6:
    cout << "Six Geese a Laying" << endl;
    case 5:
    cout << "Five Golden Rings" << endl;
    case 4:
    cout << "Four Calling Birds" << endl;
    case 3:
    cout << "Three French Hens" << endl;
    case 2:
    cout << "Two Turtle Doves" << endl;
    case 1:
    cout << "and a Partridge in a Pear Tree" << endl;
    }
    Amd Athlon Mobile 2600+ (200 X 12.5 @ 1.7)
    Asus A7N8X-Deluxe (Not Rev. 2)
    2-80gig WD Hard drives
    Corsair XMS pc3200 - 512megs
    Saphire Radeon 9800pro
    Soundblaster Live Platinum 5.1
    3Com EtherLink 10/100 3CR990-TX-97
    Creative Blaster v.92 Modem
    Sony Trinitron 21inch Monitor
    Plextor 24X Burnproof Cd-Writer
    Pioneer 16X DVD Drive
    Klipsch Ultras 5.1 Surround Sound
    Sony 3.5inch Floppy Drive
    Windows 2k of course...
    -------------------------
    Bart: Hey! Why is it killing my toys?
    Lisa: They must have programed it to destroy the competition.
    Bart: Sort of like Microsoft!
    -------------------------
    *HAVE A NICE DAY*

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •