Thread: Why is it that my program does not print for case 1?

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    29

    Why is it that my program does not print for case 1?

    Code:
    main()
    {
    
     int x=0;
     switch(x)
     {
      case 1: printf( "One" );
      case 0: printf( "Zero" );
      case 2: printf( "Hello World" );
     }
     }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Because it does. In fact, it will print "OneZeroHello World".

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> Why is it that my program does not print for case 1?

    Because it doesn't equal 1, maybe?

    >> In fact, it will print "OneZeroHello World".

    It'll actually skip the first case label, so the output would be "ZeroHello World".
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why would you expect it to? Fall-through doesn't mean it always hits every case label. It means if it matches one, and there's no fall-through protection (break), it will do everything that comes after it until it finds a reason to stop.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    When it falls through, it falls down, not up.
    Set x=1 and it'll print them all.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Sebastiani View Post
    Because it doesn't equal 1, maybe?
    Ha! That will (okay, should) teach me to read the code and not the question.

  7. #7
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    Quote Originally Posted by tabstop View Post
    Ha! That will (okay, should) teach me to read the code and not the question.
    lol. Do you know about the break statement Jasper?

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    41
    I think the problem was to illustrate the out of order sequence of the case statements and how it will effect the outcome. I think he was suppose to test the code and learn. Personly instead of changing x to equal 1 I would simply change the case 1: to case 0: and case 0: to case 1: keeping the outputs in the same order. If the point was to get all three to print. :P

    Like :
    Code:
    main()
    {
    
     int x=0;
     switch(x)
     {
      case 0: printf( "One" );
      case 1: printf( "Zero" );
      case 2: printf( "Hello World" );
     }
     }

  9. #9
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by strickyc View Post
    I think the problem was to illustrate the out of order sequence of the case statements and how it will effect the outcome. I think he was suppose to test the code and learn. Personly instead of changing x to equal 1 I would simply change the case 1: to case 0: and case 0: to case 1: keeping the outputs in the same order. If the point was to get all three to print. :P

    Like :
    Code:
    main()
    {
    
     int x=0;
     switch(x)
     {
      case 0: printf( "One" );
      case 1: printf( "Zero" );
      case 2: printf( "Hello World" );
     }
     }
    You'd rather change 2 things than 1?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    case 0: printf( "One" );
    case 1: printf( "Zero" );
    I hope all of your code doesn't end up as "clear" as this.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    Jus a nitpick, but you have things backwards - case 0 is printing 'one' and case 1 is printing 'zero' in the "fixed" code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  2. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  3. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 08:13 PM
  4. Replies: 2
    Last Post: 05-10-2002, 04:16 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM