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


Groups > comp.lang.javascript > #9377

Re: Break for last case in switch

Message-ID <2014557.oCFFeib8bu@PointedEars.de> (permalink)
From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Organization PointedEars Software (PES)
Date 2011-12-18 21:37 +0100
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

Followups directed to: comp.lang.javascript

Show all headers | 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