CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    A question regarding switch/case

    Here is the code,
    Code:
    int main()
    {	
    	int i=4;
    	switch(i)
    	 {
    	    default:printf("zero\n");
    	    case 1: printf("one\n");
    		   break;
    	    case 2:printf("two\n");
    		  break;
    		case 3: printf("three\n");
    		  break;		
    	  }
    
    	return 0;
    }
    Why does the output result include "one"? Thanks.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A question regarding switch/case

    Quote Originally Posted by LarryChen View Post
    Why does the output result include "one"? Thanks.
    What did you expect to print? Where is the break statement for the default case?

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding switch/case

    Quote Originally Posted by Paul McKenzie View Post
    What did you expect to print? Where is the break statement for the default case?

    Regards,

    Paul McKenzie
    Actually, I expected the output to be just "zero". Since i is 4, so it doesn't satisfy case 1.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A question regarding switch/case

    Quote Originally Posted by LarryChen View Post
    Actually, I expected the output to be just "zero". Since i is 4, so it doesn't satisfy case 1.
    Again, where is the break statement for the default case? Do you know why break makes a difference?

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jul 2005
    Posts
    1,030

    Re: A question regarding switch/case

    Quote Originally Posted by Paul McKenzie View Post
    Again, where is the break statement for the default case? Do you know why break makes a difference?

    Regards,

    Paul McKenzie
    I thought case statement is like if statement. So in my example, when i is 4 and it meets case 1, then case 1 is not going to be entered. Why am I wrong? Thanks.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A question regarding switch/case

    Quote Originally Posted by LarryChen View Post
    I thought case statement is like if statement. So in my example, when i is 4 and it meets case 1, then case 1 is not going to be entered. Why am I wrong? Thanks.
    That's been answered twice by Paul and once by me already. Basically a switch statement starts at the first case that meets the expression and keeps going till it hits a break.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: A question regarding switch/case

    Quote Originally Posted by LarryChen View Post
    I thought case statement is like if statement. So in my example, when i is 4 and it meets case 1, then case 1 is not going to be entered. Why am I wrong? Thanks.
    Larry, don't you see your reasoning is faulty?

    OK, so what does break do? What purpose does it serve if, by your reasoning, once a case condition is met, the switch is exited as soon as the next case condition is met? If that's your conclusion, then why not just remove the break statements from case 1, case 2, and case 3? It's just unnecessary typing, according to your conclusion.

    The answer is as GCDEF mentioned -- break does mean something in a case statement, and that is to ensure that the condition doesn't "fall through" to the next case condition.

    Regards,

    Paul McKenzie

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: A question regarding switch/case

    Quote Originally Posted by LarryChen View Post
    Actually, I expected the output to be just "zero". Since i is 4, so it doesn't satisfy case 1.
    And if you set i to equal 1, and your case 1 didn't include a break statement, what do you think the output would be?

  9. #9
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: A question regarding switch/case

    This "fall through" into the next case behaviour is sometimes a very handy feature of how the switch in C++ works. (as opposed to other programming languages where this isn't possible)

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured