Thread: question about switch case

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    75

    question about switch case

    why output is CD?
    Code:
    void main(){
    	int a=2;
    	switch(a)
    	{
    		case 4: printf("A");
    		break;
    		case 3: printf("B");
    		default : printf("C");
    		case 1 : printf("D");
    		break;
    		case 5 : printf("E");
    
    
    	}
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Why do you keep posting code and ask for a full explanation? You're supposed to at least attempt to figure it out yourself, and explain what you do understand and what you don't.

    Your last several posts appear to be some sort of homework or quiz questions, rather than actual problems. This may make people hesitant to help you, since you do not appear to be putting any effort in.

    Tell us why you think the output might be "CD", or why you think it should be something else, and we can help correct your analysis of the code, if necessary.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    i have wait for 5 days for attempting all those questions so at last i post here for help
    i thought output should be E bcoz there is no case 2, so it will print default statment. that is C but it prints CD

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by san12345 View Post
    i have wait for 5 days for attempting all those questions so at last i post here for help
    We are here to help people, but the level of help is typically related to the perceived effort shown by those asking the questions.

    Quote Originally Posted by san12345 View Post
    i thought output should be E bcoz there is no case 2, so it will print default statment.
    If there is no case for 2, why do you think the output should be "E"?

    Quote Originally Posted by san12345 View Post
    that is C but it prints CD
    I suggest you read about "switch" in any introductory text or tutorial. There's a very basic behavior of "switch" that explains perfectly why the output is what it is.

    And "main()" returns int, not void.

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    75
    theres no case 2 then default expression will execute and output would be C as i thought then why output is CD

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,508
    Quote Originally Posted by san12345 View Post
    theres no case 2 then default expression will execute and output would be C as i thought then why output is CD
    As I said, the output of "CD" is due to a particular behavior of "switch" statement, which is explained in any introductory text.

    Here, a link to the "switch" tutorial on this site: Switch Case in C - Cprogramming.com

    Read and see if you can understand why you're getting that output.

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,553
    Did you try to find and read some documentation for the switch()?

    You really really need to start reading documentation for the features you want to use if you're getting results different than what you expect.

    Jim

  8. #8
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    This escaped your eyes?
    A switch statement can have an optional default case, which must appear at the end of the switch.
    The default case can be used for performing a task when none of the cases is true.
    No break is needed in the default case.
    Last edited by tonyp12; 02-26-2016 at 01:22 PM.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,215
    > A switch statement can have an optional default case, which must appear at the end of the switch.
    It is not a requirement of the language that it should be placed at the end - it's only a stylistic convention so it roughly mimics the if / else if / else chain of thought.

    > No break is needed in the default case.
    Unless you place it in the middle, and "fall-through" is not your intention (as per the original post).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,007
    Quote Originally Posted by tonyp12
    A switch statement can have an optional default case, which must appear at the end of the switch.
    (...)
    No break is needed in the default case.
    No, the default label, if there is one, is conventionally placed after the other case labels, but this is not required syntax. Consequently, no break is needed because it is conventionally at the end, but if it is not, then the lack of a break means that fall-through will happen as per usual.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question About a Switch Case Situation
    By GokhanK in forum C Programming
    Replies: 2
    Last Post: 07-31-2013, 03:50 PM
  2. Switch Case question (from C++ Tutorial Example)
    By dacart in forum C++ Programming
    Replies: 2
    Last Post: 08-15-2011, 07:59 AM
  3. switch case question
    By dantegl36 in forum C Programming
    Replies: 2
    Last Post: 12-04-2006, 11:12 AM
  4. Switch Case Question
    By dnysveen in forum C++ Programming
    Replies: 12
    Last Post: 06-06-2006, 05:34 AM
  5. switch() case 1: Question
    By Zero_X in forum C++ Programming
    Replies: 0
    Last Post: 04-18-2006, 12:33 PM