right use of switch(), case and break statements

This is a discussion on right use of switch(), case and break statements within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a question about using the switch() statement: when I use more than one 'case' for a single ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287

    right use of switch(), case and break statements

    Hello, I have a question about using the switch() statement: when I use more than one 'case' for a single action, should I 'break' all the cases or simply using a single 'break' will end all the cases? bad english + I don't know how to explain = let me explain with a sample

    Code:
    #include <conio.h>
    
    int main()
    {
    int a=1;
    switch(1)
        {
        case 0:
        case 1:
            {
            //whatever for 0 or 1
            }
        break;
        break;
        
        case 2:
            {
            //for 2
            }
        break;
        }
    getch();
    return 0;
    }
    That is, I have used 2 'case', for 0 and for 1; then should I use also twice 'break'? I see that the program runs without error, and also I have find on the net lots of codes that uses only one 'break' after start several 'case', that also works without error; sometimes I used also this method, only breaking one time at the end and my simple programs run without problem. I was wondering which is the right way to do it? Should I 'break' each 'case'?
    Thank's in advance
    niara
    Last edited by Niara; 01-12-2006 at 05:11 AM.

  2. #2
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    First, just to note:
    Code:
    switch(1)
    Will always cause "case 1:" to fire. "case 0:" will never execute, nor will "case 2:".

    Say I want to execute the same code for 1, 2, and 3:
    Code:
    switch(X) //X is an int or char variable
    {
    case 1: case 2: case 3: //Essentially the same thing you wrote with "case 0:" and "case 1:" above
    	//Have some code
    	break; //This break will cause us to "break out" of the switch statement
    }
    Therefore, in your example, only one break is necessary when you want to "break out". Since "case 0:" does not have a "break" before the next case, any code (if there was any) following "case 0:" would be executed, then the code will keep going into "case 1:", and then it hits the break. The second break will never ever be reached.

    The choice to "break" a case is all dependent on whether you want one of your cases which is above another one to also execute the code below it, or not.
    Pentium 4 - 2.0GHz, 512MB RAM
    NVIDIA GeForce4 MX 440
    WinXP
    Visual Studio .Net 2003
    DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    You may find some compilers will issue a "no path to this statement" type warning if you use 2 breaks.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    287
    Hello both, thank's for your time and help.
    Epo: yes, I know what happens with my silly sample code, it was only for help me to explain the '(1st)case2nd)case: (1st)break;(second break is needed?)' part. but you have 'switched on' a light on my question: the 'break' stops the 'switch' statement, from a single and from multiple 'case' conditionals.
    adrianxw: thank's for the observation. I'm using DevC++ and (for the moment) does not alert any error for that 'error', but I will have in mind.
    Thank's both.
    regards
    niara

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    If you have understood just tell what would be output of following program
    Code:
    int main()
    {
      int a=1;
      switch(a)
      {
        case 1:cout<<"First case";
        case 2:cout<<"Second case";
        case 3:cout<<"Third case";
      }
    return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 01:09 AM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 09:18 PM

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