Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Switch and break

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Switch and break

    Hello! I just want to know if I understand the program correctly. The program looks like this;

    public class test{
    	static char bst = 'a';
    	static byte nr = 0;
     
    	public static void main(String[] args) {{
    		char bst = 'c';
    	}
     
    	switch(bst) {
    		case 'a': nr = 1;
    		case 'b': nr = 2;
    		case 'c': nr = 3;
    		case 'd': nr = 4;
     
    	}
    	Out.println("To "+bst + "belongs" +nr);
     
    	}
    }

    The output is "To a belongs 2", now the way I am interpreting this program is like this. We have this Class variable c that is initialzed as a and when we enter the switch after checking that it is a number 1 is assigned, but since there is no break we continue going down thus assigning number 2 where we exit the switch (via break). Am I understanding this correctly or ?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch and break

    since there is no break we continue going down thus assigning number 2 where we exit the switch
    Where is the break after assigning the 2? I don't see it in the posted code.

    What happens when the code is compiled and executed?

    Using {{ makes for confusing code. Each { should be paired with its } below and in the same column in the source file.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Switch and break

    Oh wait I copied wrong, my bad and I know that {{ is bad but again it is a question from the test, and the output is: "To a belongs 2. Here is how the Code ACTUALLY looks like, sorry for the mistake earlier.

    [code=Java]
    public class test{
    static char bst = 'a';
    static byte nr = 0;

    public static void main(String[] args) {{
    char bst = 'c';
    }

    switch(bst) {
    case 'a': nr = 1;
    case 'b': nr = 2; break;
    case 'c': nr = 3;
    case 'd': nr = 4;

    }
    Out.println("To "+bst + "belongs" +nr);

    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch and break

    Yes that makes a difference.
    Now what is your question about the new code?

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    Why would a test use poor coding like {{?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Switch and break

    public class test{
    static char bst = 'a';
    static byte nr = 0;
     
    public static void main(String[] args) {{
    char bst = 'c';
    }
     
    switch(bst) {
    case 'a': nr = 1;
    case 'b': nr = 2; break;
    case 'c': nr = 3;
    case 'd': nr = 4;
     
    }
    Out.println("To "+bst + "belongs" +nr);
     
    }
    }

    Sorry for the mistake, here is the code, well it would use something like {{ to make it difficult, that is the European way.My question is,actually not a question but more of a comfirmation. When we enter the switch and case is a so we assing 1 to it, but since we dont break we just continue going assigning 2 and than break exists the switch. Because the output is "To a belongs 2" so I'd presume the missing break at the first line in the switch causes this.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch and break

    That explanation sounds about right.
    Do you understand how the extra {}s around the char bst = 'c' statement creates a different scope than the code outside of the {}s?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Switch and break

    I think I do but I would mind a great explantion from you.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Switch and break

    Ask google for explanations on scope. I'm sure there are some good ones out there.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Switch and break

    Will do thanks

Similar Threads

  1. break point
    By want2code in forum Java IDEs
    Replies: 3
    Last Post: August 19th, 2014, 06:31 AM
  2. My break seems to be broken.
    By papadilbert in forum Loops & Control Statements
    Replies: 8
    Last Post: April 11th, 2014, 01:00 PM
  3. How do I make a break in this?
    By JavaA123Chris in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 28th, 2013, 06:10 PM
  4. where to break array sorting?
    By BITmixit in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 13th, 2011, 09:46 AM
  5. Break it down
    By [Kyle] in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2009, 09:04 PM