Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #9406

Re: Break for last case in switch

Message-ID <2253420.FRqVoOGUtd@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-12-19 12:25 +0100
Subject Re: Break for last case in switch
Newsgroups comp.lang.javascript
References (3 earlier) <f5qdnf4Rs5HfyXPTnZ2dnUVZ8v-dnZ2d@giganews.com> <3028473.Ge26DHhYnv@PointedEars.de> <4eydnfAOdKla9XPTnZ2dnUVZ8qWdnZ2d@giganews.com> <2974255.78QauJFxQc@PointedEars.de> <N5ydnVYLfpXSI3PTnZ2dnUVZ7sOdnZ2d@giganews.com>
Followup-To comp.lang.javascript

Followups directed to: comp.lang.javascript

Show all headers | View raw


Stefan Weiss wrote:

> On 2011-12-19 00:06, Thomas 'PointedEars' Lahn wrote:
>> Stefan Weiss wrote:
>>> On 2011-12-18 22:29, Thomas 'PointedEars' Lahn wrote:
>>>> Stefan Weiss wrote:
>>>>> And as expected, nothing changes when you add "break" after "y = 23".
>>>> What changes then is that there is no fall-through in either case.
>>> Or in other words... nothing changes :)
>>> Whatever you do, a 'break' at that position cannot possibly change the
>>> outcome. It is, as I said, redundant.
>>
>> No, look closer.  It does not change the outcome *here* because `2'
>> cannot possibly be a match for the value to be found by the algorithm.
> 
> Alright, then give us a counter example.
> 
> switch (1) {
>    /*
>     * your code here
>     */
>    break;   // <-- this will make a difference, according to you
> }
 
You are still missing the point.

A)

  var x = 2;
  var outcome = -1;

  switch (x)
  {
    case 1:
      console.log("case 1");

    default:
      console.log("default");
      outcome = "default";

    case 2:
      console.log("case 2");
      outcome = 2;
  }

  console.log(outcome);

Console output:

| case 2
| 2


B)

  var x = 3;
  var outcome = -1;

  switch (x)
  {
    case 1:
      console.log("case 1");

    default:
      console.log("case default");
      outcome = "default";

    case 2:
      console.log("case 2");
      outcome = 2;
  }

  console.log(outcome);

Console output:

| case default
| case 2
| 2


C)

  var x = 3;
  var outcome = -1;

  switch (x)
  {
    case 1:
      console.log("case 1");

    default:
      console.log("case default");
      outcome = "default";
      break;

    case 2:
      console.log("case 2");
      outcome = 2;
  }

  console.log(outcome);

Console output:

| case default
| default

A similar, more well-known fall-through occurs if the previously last `case' 
clause of a `switch' statement is not the last `case' clause anymore.

It makes sense for a lint for ECMAScript implementations to issue a warning 
if the `break' statement is missing for the last clause (in JavaScriptLint 
terminology: "the last case") of a `switch' statement (_not_ necessarily 
meaning "last `case' *clause*") in order to avoid accidental fall-through in 
the future.  From the results (*three* warnings) I am getting when omitting 
the other `break' statement as well, it is obvious to me that this was the 
*intention* of the JavaScriptLint authors, so it cannot be considered a bug.  
(A bug worth reporting is the ambiguous wording in the warning; they should 
have used "clause", not "case".)


PointedEars  
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
  -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Break for last case in switch Archos <raul.san@sent.com> - 2011-12-18 11:04 -0800
  Re: Break for last case in switch Stefan Weiss <krewecherl@gmail.com> - 2011-12-18 20:43 +0100
    Re: Break for last case in switch "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-12-18 22:13 +0200
    Re: Break for last case in switch Antony Scriven <adscriven@gmail.com> - 2011-12-18 12:21 -0800
    Re: Break for last case in switch Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-18 21:37 +0100
      Re: Break for last case in switch Stefan Weiss <krewecherl@gmail.com> - 2011-12-18 21:58 +0100
        Re: Break for last case in switch Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-18 22:29 +0100
          Re: Break for last case in switch "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-12-18 23:49 +0200
          Re: Break for last case in switch Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-18 23:09 +0100
            Re: Break for last case in switch "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-12-19 00:32 +0200
          Re: Break for last case in switch Stefan Weiss <krewecherl@gmail.com> - 2011-12-18 23:25 +0100
            Re: Break for last case in switch Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-19 00:06 +0100
              Re: Break for last case in switch Stefan Weiss <krewecherl@gmail.com> - 2011-12-19 05:30 +0100
                Re: Break for last case in switch Andrew Poulos <ap_prog@hotmail.com> - 2011-12-19 16:04 +1100
                Re: Break for last case in switch Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-12-19 12:25 +0100
                Re: Break for last case in switch "Evertjan." <exjxw.hannivoort@interxnl.net> - 2011-12-19 17:17 +0000
                Re: Break for last case in switch Gene Wirchenko <genew@ocis.net> - 2011-12-19 12:19 -0800
                Re: Break for last case in switch "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-12-19 14:47 +0200
                Re: Break for last case in switch John G Harris <john@nospam.demon.co.uk> - 2011-12-19 15:50 +0000
                Re: Break for last case in switch "Jukka K. Korpela" <jkorpela@cs.tut.fi> - 2011-12-19 18:45 +0200
                Re: Break for last case in switch Gene Wirchenko <genew@ocis.net> - 2011-12-19 12:22 -0800

csiph-web