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


Groups > comp.lang.javascript > #9377

Re: Break for last case in switch

Path csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!news.belwue.de!newsfeed.ision.net!newsfeed2.easynews.net!ision!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail
Content-Type text/plain; charset="ISO-8859-1"
Message-ID <2014557.oCFFeib8bu@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Reply-To Thomas 'PointedEars' Lahn <cljs@PointedEars.de>
Organization PointedEars Software (PES)
Date Sun, 18 Dec 2011 21:37:28 +0100
User-Agent KNode/4.4.11
Content-Transfer-Encoding 7Bit
Subject Re: Break for last case in switch
Newsgroups comp.lang.javascript
References <758d8d3d-1b7d-46a8-8dc7-8f5764138efc@q16g2000yqn.googlegroups.com> <fbidncC--v1e33PTnZ2dnUVZ8sWdnZ2d@giganews.com>
Followup-To comp.lang.javascript
MIME-Version 1.0
Lines 63
NNTP-Posting-Date 18 Dec 2011 21:37:28 CET
NNTP-Posting-Host 8f79fa2f.newsspool4.arcor-online.net
X-Trace DXC=K0l^Pb[Z]S9]E=H1Q9`7874IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;IHeiH@`A^P6DZm8W4\YJN<;?f@h5gMfb<dY906Ve2E67nZN7kc>T317
X-Complaints-To usenet-abuse@arcor.de
Xref x330-a1.tempe.blueboxinc.net comp.lang.javascript:9377

Followups directed to: comp.lang.javascript

Show key headers only | View raw


Stefan Weiss wrote:

> On 2011-12-18 20:04, Archos wrote:
>> Is ok don't add a "break" statement in the last "case" one? Because
>> JavaScript Lint shows:
>> 
>> lint warning: missing break statement for last case in switch
>> 
>> // ===
>> var x = tag
>> switch (1) {
>> case x < 0:
>> str = "negative"; break;
>> default:
>> str = "positive";
>> }
>> // ===
> 
> JavaScript Lint is mistaken.

You are mistaken.

> A 'break;' at the end of the last clause in a switch statement would be
> redundant.

  var x = 0,
      y = -1;

  switch (1)
  {
    case x < 1:
      y = x;
      break;

    default:
      /* 0 */
      console.log(x);
      y = 42;
    
    case 2:
      y = 23;
  }

  /* 23 */
  console.log(y);

It should be added that this result makes it rather obvious that, on top of 
the facts that omitting the `break' statement is error-prone considering 
code extensions, and that this approach is poor code style indeed (use the 
`if' statement instead), the approach simply does not work.

`x < 0' evaluates to a Boolean primitive value (either `true' or `false'), 
never to the Number primitive value `1' (type conversion is _not_ 
performed); therefore, the `default' clause will be considered.  If the 
`default' clause does not contain a `break' statement as last in its 
/StatementList/, subsequent `case' clauses will be considered *without* 
checking.  This is conforming with ECMA-262 Ed. 5.1, section 12.11 ("The 
switch Statement").


PointedEars
-- 
When all you know is jQuery, every problem looks $(olvable).

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