Results 1 to 5 of 5
  1. #1
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default case statement options without BREAK in them...

    All,

    I have this code:

    Java Code:
            for (ctr = 0; ctr <= 16; ctr++)
            {
                sum = ctr;
                
                switch (sum)
                {
                    case 0: ret = ret + "0"; break;
                    case 1: ret = ret + "1"; break;
                    case 2: ret = ret + "2"; break;
                    case 3: ret = ret + "3"; break;
                    case 4: ret = ret + "4"; break;
                    case 5: ret = ret + "5"; break;
                    case 6: ret = ret + "6"; break;
                    case 7: ret = ret + "7"; break;
                    case 8: ret = ret + "8"; break;
                    case 9: ret = ret + "9"; break;
                    case 10: ret = ret + "10"; break;
                    case 11: ret = ret + "11";
                    case 12: ret = ret + "12";
                    case 13: ret = ret + "13";
                    case 14: ret = ret + "14";
                    case 15: ret = ret + "15";
                    case 16: ret = ret + "16";
                }
            }
    sum is an INT and ret is a STRING. the output of this is the following:

    Java Code:
    012345678910111213141516121314151613141516141516151616
    so essentially what is happening here is that the BREAK statements are doing their job, but when the "ctr" > 10, then the "factorial" type of accumulation starts happening. What is the reason for this? Is the cause just because the BREAK statements are missing? Obviously that is right based on the output I've posted here, but what I'm not understanding is why the CASE statements are run through completely even though the actual cases do not authenticate (check out). Is this just something we have to live with? That's fine, but I would like to know.

    thanks guys! As always, responses are appreciated.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,041
    Rep Power
    10

    Default Re: case statement options without BREAK in them...

    This is called falling through, and is exactly how case statements are supposed to work. Recommended reading: The switch Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: case statement options without BREAK in them...

    I was on that page. So the bottom line here basically, is that "falling through" is a concept that is relative to Java specifically but is pretty heavy and visible in the OOP world. correct?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: case statement options without BREAK in them...

    A case statement is nothing more than a 'computed go to'; after it has reached its target (a 'case' clause) it doesn't spontaneously jump away from a next case clause. This is actually more 'powerful' than the opposite: suppose breaks are automatically inserted; you can't 'fall through' a next case clause anymore.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    ajetrumpet is offline Member
    Join Date
    Feb 2013
    Posts
    57
    Rep Power
    0

    Default Re: case statement options without BREAK in them...

    I appreciate that Jos. That's very helpful. I did read that through a search as well. I will just understand that the "goto" portion of the issue is the only real thing happening and that the subsequent case statements are ignored. Thread solved!

Similar Threads

  1. Replies: 3
    Last Post: 11-26-2012, 12:06 PM
  2. break statement is ignored.
    By Pojahn_M in forum New To Java
    Replies: 11
    Last Post: 05-09-2011, 10:35 PM
  3. Replies: 10
    Last Post: 01-07-2011, 11:28 AM
  4. break statement
    By AbdulAziz Bader in forum New To Java
    Replies: 17
    Last Post: 06-02-2010, 04:42 PM
  5. case statement
    By skiing in forum New To Java
    Replies: 5
    Last Post: 05-07-2009, 01:13 PM

Posting Permissions

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