tutorial quizz question for switch operator

This is a discussion on tutorial quizz question for switch operator within the C Programming forums, part of the General Programming Boards category; 4. What is the result of the following code? Code: x=0; switch(x) { case 1: printf( "One" ); case 0: ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    8

    Question tutorial quizz question for switch operator

    4. What is the result of the following code?
    Code:
    x=0;
    switch(x)
    {
      case 1: printf( "One" );
      case 0: printf( "Zero" );
      case 2: printf( "Hello World" );
    }
    A. One
    B. Zero
    C. Hello World
    D. ZeroHello World

    why it is D, I thought it should be B

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    Because cases fall-through to the next case unless you use the break statement.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    1
    if "break;" statement is used then UR expected option B will be the result

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    8
    Rahmet

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    8

    Smile

    Quote Originally Posted by Salem
    Because cases fall-through to the next case unless you use the break statement.
    rahmet

  6. #6
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    so you mean after printing Zero the program doesn`t check if
    case (2 == x) and it will print anything after case 0

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,671
    No it doesn't.

    The switch condition is only checked once, and that decides where in the switch statement the program jumps to. After that, the code just executes in a linear fashion like any other code until it comes to the closing brace or a break statement.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about an operator
    By Programmer_P in forum C++ Programming
    Replies: 6
    Last Post: 05-10-2009, 08:58 PM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. quick c++ operator question
    By pkananen in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 05:46 PM
  5. C number operator question
    By unanimous in forum C Programming
    Replies: 6
    Last Post: 10-26-2001, 09:36 PM

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