www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - corner case with labled break

reply BCS <ao pathlink.com> writes:
should this work? (it doesn't now)

|void main()
|{
|	foo: while(true)
|		break foo;   // works
|
|	switch(6)
|	{
|		case 5:
|			while(true)
|				break case 5;   // fails
|
|		case 6:
|			goto case 5;  // works
|
|	}
|}
Jan 25 2008
next sibling parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:55391cb32812d8ca2d7783d0f566 news.digitalmars.com...
 should this work? (it doesn't now)

 |void main()
 |{
 | foo: while(true)
 | break foo;   // works
 |
 | switch(6)
 | {
 | case 5:
 | while(true)
 | break case 5;   // fails
 |
 | case 6:
 | goto case 5;  // works
 |
 | }
 |}

It's not in the grammar at all, so I'm not surprised that it doesn't. It's a tiny feature though.
Jan 25 2008
parent reply BCS <ao pathlink.com> writes:
Reply to Jarrett,

 "BCS" <ao pathlink.com> wrote in message
 news:55391cb32812d8ca2d7783d0f566 news.digitalmars.com...
 
 should this work? (it doesn't now)
 
 |void main()
 |{
 | foo: while(true)
 | break foo;   // works
 |
 | switch(6)
 | {
 | case 5:
 | while(true)
 | break case 5;   // fails
 |
 | case 6:
 | goto case 5;  // works
 |
 | }
 |}

It's a tiny feature though.

OK, I miss phrased the question: should this be in the grammer?
Jan 25 2008
parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"BCS" <ao pathlink.com> wrote in message 
news:55391cb3281338ca2d803bbfda0e news.digitalmars.com...
 OK, I miss phrased the question: should this be in the grammer?

If you're proposing a feature, go ahead and propose it. I mean, I can't see what I'd use this for, but..
Jan 25 2008
prev sibling parent reply Jason House <jason.james.house gmail.com> writes:
Shouldn't the code be:

|void main()
|{
|foo: while(true)
|break foo;   // works
|
|bar: switch(6)
|{
|case 5:
|while(true)
|break bar;   // fails
|
|case 6:
|goto case 5;  // works
|
|}
|}

With embedded switches, "case 5" isn't always unique.  Also, the "break case
5" syntax almost implies to me that it should return to case 6.  I hope
that's not what you wanted...
Jan 27 2008
parent BCS <ao pathlink.com> writes:
Reply to Jason,

 Shouldn't the code be:
 
 |void main()
 |{
 |foo: while(true)
 |break foo;   // works
 |
 |bar: switch(6)
 |{
 |case 5:
 |while(true)
 |break bar;   // fails
 |
 |case 6:
 |goto case 5;  // works
 |
 |}
 |}
 With embedded switches, "case 5" isn't always unique.

Ok, but that same problem happens with goto case #; based on that, this should be added: outer: switch(n) { case 1: switch(m) { case 2: goto outer case 2; } case 2: }
  Also, the
 "break case 5" syntax almost implies to me that it should return to
 case 6.  I hope that's not what you wanted...
 

Ok so it loops forever, but I /was/ intending that the break exit the loop and continue down the case statment This would be a more sane example. |void main() |{ | foo: while(true) | break foo; // works | | switch(6) | { | case 5: | while(true) | break case 5; // fails | somthing(); | break; | | case 6: | goto case 5; // works | | } |} The point is that case ###: lables act like normal lables elseware, why not with break?
Jan 27 2008