473,326 Members | 2,173 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

label: after where it's used

Hi all,

For some reason, this seems to fail for me (it's psuedo-code):

for(var i=0; i<length; i++) {
for(var j=0; j<length; j++) {
...
if(condition) {
break my_label;
}
}
....

my_label:
}

but, if I define my_label: before the outer loop, it works just fine.
Do I have to define the label earlier in the code than where it's
used? For the time being, I just got the same affect as this code by
setting a boolean variable before breaking and then continuing if it's
true.

Anyone know what's going on?

Thanks,
Jeff

Mar 24 '07 #1
4 1643
VK
On Mar 25, 12:35 am, "Jeff" <jeffrey.big...@gmail.comwrote:
Do I have to define the label earlier in the code than where it's
used?
Yes, though it is difficult to give a right answer to a wrong question
like that :-) Label is not GOTO target in BASIC or something like
that. It is the way to identify a loop so for nested loops one could
switch loop levels, like

:OUTER
for (var i=0; i<1000; i++) {
:INNER
for (var j=0; j<1000; j++) {
if (some_condition) {
break INNER;
}
else {
continue OUTER
}
}
}

Because it is for nested loops and because one cannot get inside
without entering a loop, the question "Do I have to define the label
earlier in the code?" is a bit strange to ask: but formally speaking
yes, you have.

Mar 24 '07 #2
On Mar 25, 7:35 am, "Jeff" <jeffrey.big...@gmail.comwrote:
Hi all,

For some reason, this seems to fail for me (it's psuedo-code):

for(var i=0; i<length; i++) {
for(var j=0; j<length; j++) {
...
if(condition) {
break my_label;
Break is intended as an escape from a looping structure or iteration
statement (for, switch, etc.), here you are asking to break *from*
my_label but you aren't in my_label.

Try this example:

function foo(txt) {
labelA: if (txt.length) {
break labelA;
} else {
alert('Need text');
return;
}
alert(txt);
}

foo(''); // Shows 'Need text'
foo('bar'); // Shows 'bar'
In your case you need to do:

my_label: for(var i=0; i<length; i++) {
for(var j=0; j<length; j++) {
...
if(condition) {
break my_label;
}
}
}

You can also use a continue statement to effectively skip the rest of
the statement and go back to the start (or some other labelled point),
but such structures start to look like GOTO. You almost never see it
used in anger, there are nearly always other ways that are simpler.
--
Rob

Mar 25 '07 #3
In comp.lang.javascript message <11*********************@e1g2000hsg.goog
legroups.com>, Sat, 24 Mar 2007 14:59:46, VK <sc**********@yahoo.com>
posted:
>On Mar 25, 12:35 am, "Jeff" <jeffrey.big...@gmail.comwrote:
>Do I have to define the label earlier in the code than where it's
used?

Yes, though it is difficult to give a right answer to a wrong question
like that :-) Label is not GOTO target in BASIC or something like
that. It is the way to identify a loop so for nested loops one could
switch loop levels, like

:OUTER
for (var i=0; i<1000; i++) {
:INNER
for (var j=0; j<1000; j++) {
if (some_condition) {
break INNER;
}
else {
continue OUTER
}
}
}

Because it is for nested loops and because one cannot get inside
without entering a loop, the question "Do I have to define the label
earlier in the code?" is a bit strange to ask: but formally speaking
yes, you have.
I thought for a while that, just for once, VK might have got it right.
But obviously he/she does not believe in testing code before posting.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 6
news:comp.lang.javascript FAQ <URL:http://www.jibbering.com/faq/index.html>.
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 25 '07 #4
VK
On Mar 25, 9:42 pm, Dr J R Stockton <j...@merlyn.demon.co.ukwrote:
I thought for a while that, just for once, VK might have got it right.
I'm always right, just not always people is ready for it.
;-)
But obviously he/she does not believe in testing code before posting.
Truthfully in my entire life I used the block labeling in JavaScript
four times including this one, where three times is for demonstration
and not for a practical purpose. Labels are postfixed, not prefixed
here:

OUTER:

INNER:

so note the error in my previous post.

Mar 25 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eric Goforth | last post by:
Hello, I'm getting a: "SQLSTATE" is not valid in the context where it is used. This is in a stored procedure when I try to a: FETCH FROM customerids_cursor INTO v_MYVAR1,v_MYVAR2,...
1
by: Zri Man | last post by:
Procedure Get_RowCount works fine when called independently. When called from Another procedure it gives the following error. SQL0206N "C" is not valid in the context where it is used....
2
by: balaji N R | last post by:
Hi friends, I want to know where the union are used. give me some applications where union are used in real time applications.Give me explanations of how to use it without any error . ...
1
by: bswanstrom | last post by:
Hi there, trying to execute a db2 sql statement from a command line from UNIX. Here is the command I'm running db2 export to /work/ftp/lt/sku_insert.csv of del select digits'('UPC_NO')', UPC,...
1
by: Rodney Maxwell | last post by:
The following are apparently legal Python syntactically: L L But they don't seem to work on lists: Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: list...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I am trying to loop a Textfile. This Textfile is located on a shared drive and it is used by another process. When I try to read it I get an exeption: The process cannot access the...
1
by: 7h0r | last post by:
When trying to insert this trigger: CREATE TRIGGER ad_des_part_jon AFTER DELETE ON DESIGJON.PART_LIST REFERENCING OLD AS o FOR EACH ROW mode db2sql UPDATE DEFINJON.PART_LIST A SET...
4
haridhasekar
by: haridhasekar | last post by:
hi all, pls analyse the code .. temme why it is used so ... is there any alternate method to validate whether it is a numeric....if so temme..... function isnumeric(e,obj) { var keynum;...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.