Asked  7 Months ago    Answers:  5   Viewed   28 times

Why doesn't the compiler automatically put break statements after each code block in the switch? Is it for historical reasons? When would you want multiple code blocks to execute?

 Answers

81

Sometimes it is helpful to have multiple cases associated with the same code block, such as

case 'A':
case 'B':
case 'C':
    doSomething();
    break;

case 'D':
case 'E':
    doSomethingElse();
    break;

etc. Just an example.

In my experience, usually it is bad style to "fall through" and have multiple blocks of code execute for one case, but there may be uses for it in some situations.

Tuesday, June 1, 2021
 
Norgul
answered 7 Months ago
80

So, it looks like the behavior isn't consistent across browsers:

  • In IE11 and the last few versions I've been able to test this on, whether the span is an inline box or an inline-block box, IE respects the no-break space and does not separate the span from its preceding word.

  • In all other browsers, the behavior is as described in the question. The no-break space appears to be ignored when the span is an inline-block; instead, it gets forced onto its own line and separated from its preceding word. Note that the no-break space is still there; it appears at the end of the first line, just after the word, which can be seen by highlighting the line in Firefox.

The explanation given by some others here seems rather sound: an inline-block is formatted like a block box, except laid inline, so that could be a reason why it doesn't always behave the same as an inline box. However, I haven't been able to find anything in the specification that confirms this. Nor does it explain why IE behaves the way it does, or indeed, whether IE is behaving incorrectly or just differently.

The CSS2.1 spec does state the following in section 9.2.2, which lends itself quite well to the above explanation:

Inline-level boxes that are not inline boxes (such as replaced inline-level elements, inline-block elements, and inline-table elements) are called atomic inline-level boxes because they participate in their inline formatting context as a single opaque box.

But it does not fully describe the interaction between line breaks and atomic inline-level boxes, let alone the effect a no-break space would have on this interaction. So, unfortunately, there does not appear to be a sufficient explanation for the behavior of any one browser.


I've even looked in the CSS3 Text module, but all it has to say with respect to line breaks and atomic inlines is this:

  • The line breaking behavior of a replaced element or other atomic inline is equivalent to that of the Object Replacement Character (U+FFFC).

... and when I try it with a U+FFFC, the results are completely unreliable:

<p id='one'>Hello World how are you.&nbsp;<span>Entrepreneur</span></p>

<p>Hello World how are you.&nbsp;<span>Entrepreneur</span></p>

<p>Hello World how are you.&nbsp;&#xfffc;</p>

Namely:

  • IE behaves most consistently, breaking all three paragraphs the same way.

  • Firefox breaks the line with the character the same way as it does the second paragraph, respecting the no-break space.

  • Chrome does not even attempt to render the character.

Maybe I'm just misunderstanding the quote from the CSS3 Text module, maybe browsers aren't implementing certain control characters correctly, I can't say for certain. But, if anything, I'm starting to doubt that this behavior is really defined in any CSS specification at all.

Tuesday, August 3, 2021
 
mpen
answered 4 Months ago
25

The reason why switch works as it does is that this:

switch(p){  
        case (1):
            x--;
        case (2):
            x = 2;
        case (3):
            x = 3;
        default:
            x++;
    }

is really just syntactic sugar for this (basically):

if (p == 1)
    goto .L1;
else if (p == 2)
    goto .L2;
else if (p == 3)
    goto .L3;
else
    goto .L4;

.L1:
    x--;
.L2:
    x = 2;
.L3:
    x = 3;
.L4:
    x++;

Java doesn't have a goto statement, but C does, and that's where it comes from. So if p is 2, it jumps to .L2 and executes all the statements following that label.

Friday, August 6, 2021
 
Success Man
answered 4 Months ago
68

Read the official documentation about it at:

http://code.google.com/p/modwsgi/wiki/ReloadingSourceCode

Thursday, October 14, 2021
 
Mikita Belahlazau
answered 2 Months ago
52

In javascript switch and case are keyword logical operators and do not have a prototype or are introspectable by the javascript engine. However, functions are dynamic objects so if you put a switch statement within a function, then you can call toString() on that function to evaluate the function's contents like this:

var fn = function(value){
  switch(value){
    case "A": 
      return "Apple";
    case "B":
      return "Banana";
  }
};

var fnToString = fn.toString();
var fnBody = fnToString.match(/function[^{]+{([sS]*)}$/)[1];
var count = fnBody.match(/case/g).length; //should equal 2

Note: The regex is error prone, but gives you a gist of the strategy. I'll let you get fancy with the regex to find out how many times the word case occurs.

Saturday, November 27, 2021
 
Hadi J
answered 3 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share