Question about SWITCH

This is a discussion on Question about SWITCH within the C++ Programming forums, part of the General Programming Boards category; hello guys, I was reading about SWITCH now and I have something misunderstood. take a look on this piece of ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    14

    Question about SWITCH

    hello guys,

    I was reading about SWITCH now and I have something misunderstood.
    take a look on this piece of code please

    http://db.tt/BwcGOr8g

    My question is why when the number == 5 it still prints all the other cout's of the other numbers (4,3,2,1).

    if the number == 5 then when line 16 saying "case 4: cout.." but the case is not 4 it is 5, so why does it still prints out "nice pick" ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,119
    There is no break after the statement for case 5, hence there is fall through to the other cases.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    14
    Hi,

    But although there is no break line, the number 4 or 3 does not have any meaning? Although it is not the case of 4 (in this e.g. It's 5) it will still print what has to be printed in case of the number 4?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,119
    Quote Originally Posted by Jonathan Yaniv
    But although there is no break line, the number 4 or 3 does not have any meaning?
    It is not that they have no meaning; it is just that control did not jump to them, but rather control reached them from the previous statement.

    Quote Originally Posted by Jonathan Yaniv
    Although it is not the case of 4 (in this e.g. It's 5) it will still print what has to be printed in case of the number 4?
    Yes, because control falls through until it encounters a break or the end of the switch is reached.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Dec 2005
    Posts
    134
    Quote Originally Posted by Jonathan Yaniv View Post
    It seems that you have read about "switch" only at above link!!

    Code:
    //Switch case block starts
    switch (color) {
        case 1:
            printf("Red\n");
            break;
        case 2:
            printf("Blue\n");
            //break;
        case 3:
            printf("Yellow\n");
            break;
        case 4:
            printf("Indigo\n");
            break;
        
        default:
            printf("Please select a color number\n");
    }
    //Switch case block ends
    A switch case statement can consists of a number of case as case 1: case 2: .... case n: (n is the variable which receives value from switch statement '(color)', Remember n should be an integral value). Based on value what switch received in 'color' it jumps on that case and process the lines afterwards sequentially unless it find a break.

    Notice a commented break after case 2 in above code.
    So When you will enter 1, it jumps to case 1: and print Red, process further and there is a break so quit from the Switch case block.
    When you enter 2, it jumps to case 2: and print Blue and "as the break is commented" so it process further and it will print Yellow, process further and finds a break hence quit.
    When you enter 3, it jumps to case 3: and print Yellow, process further and there is break, so quit.

    Switch case statement is simplest condition to understand in "C"
    S_ccess is waiting for u. Go Ahead, put u there.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,901
    Quote Originally Posted by maven View Post
    Switch case statement is simplest condition to understand in "C"
    But this isn't C; it's C++!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner if/switch question
    By GuitarNinja in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 12:11 AM
  2. switch question
    By l2u in forum C++ Programming
    Replies: 17
    Last Post: 11-08-2006, 12:01 PM
  3. Switch Case Question
    By dnysveen in forum C++ Programming
    Replies: 12
    Last Post: 06-06-2006, 06:34 AM
  4. switch() case 1: Question
    By Zero_X in forum C++ Programming
    Replies: 0
    Last Post: 04-18-2006, 01:33 PM
  5. A switch question
    By Death_Wraith in forum C++ Programming
    Replies: 25
    Last Post: 07-14-2004, 08:16 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21