Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > How does the break statement work?

Reply
Thread Tools

How does the break statement work?

 
 
Cogito
Guest
Posts: n/a
 
      03-20-2007
For the first time, I'm attempting to write a small Javascript program
using one on the online reference sites. I need some confirmation as
to the behaviour of the break statement.

In the following code:

for ( row = 0 ; row <= 7 ; row++ ) A <----
{
for ( col = 0 ; col <=7 ; col++ ) B <----
{
if ( check ( row, col ) == "pass" )
break ;
}
}
}

Where will control pass to once the break statement is executed?
Will it continue with the first 'for' statement (A) or the second (B)?

Also my 'check' function needs to pass an indication as to it's
success or failure. It does it by:

return ( "pass" ) ;

Am I doing it correctly?

Any links to useful reference sites would be welcomed.
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 4:33 pm, Cogito <(E-Mail Removed)> wrote:
> For the first time, I'm attempting to write a small Javascript program
> using one on the online reference sites. I need some confirmation as
> to the behaviour of the break statement.
>
> In the following code:


Please indent using 2 spaces in posted code.

>
> for ( row = 0 ; row <= 7 ; row++ ) A <----
> {
> for ( col = 0 ; col <=7 ; col++ ) B <----
> {
> if ( check ( row, col ) == "pass" )


Syntax error: missing opening { -----------------------------^

Also, check is not defined, row and col should be kept local with var.

> break ;
> }


alert('Broke to B');

> }


alert('Broke to A');


> }
>
> Where will control pass to once the break statement is executed?


Fix the syntax error, then try it and see.


> Will it continue with the first 'for' statement (A) or the second (B)?
>
> Also my 'check' function needs to pass an indication as to it's
> success or failure. It does it by:
>
> return ( "pass" ) ;
>
> Am I doing it correctly?


Why not just return true or false? Then you do:

if (check(row, col)) {
...
}


> Any links to useful reference sites would be welcomed.


<URL: http://jibbering.com/faq/index.html >
<URL: http://www.JavascriptToolbox.com/bestpractices/ >


--
Rob

 
Reply With Quote
 
 
 
 
Cogito
Guest
Posts: n/a
 
      03-20-2007
On 20 Mar 2007 00:16:45 -0700, "RobG" <(E-Mail Removed)> wrote:

>Syntax error: missing opening


Thanks for your reply.
Is there a tool to check syntax? I just use notepad to type the
program.
 
Reply With Quote
 
Cogito
Guest
Posts: n/a
 
      03-20-2007
On 20 Mar 2007 00:16:45 -0700, "RobG" <(E-Mail Removed)> wrote:

> alert('Broke to A');
>


Just figured out what alert does. This is a great tool for
debugging!!! Thanks for mrntioning it.


>>
>> Also my 'check' function needs to pass an indication as to it's
>> success or failure. It does it by:
>>
>> return ( "pass" ) ;
>>
>> Am I doing it correctly?

>
>Why not just return true or false? Then you do:
>
> if (check(row, col)) {
> ...
> }
>


That looks more elegant. I did not know it.
btw, how would I check for a false condition?


 
Reply With Quote
 
Tom Cole
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 5:31 am, Cogito <(E-Mail Removed)> wrote:
> On 20 Mar 2007 00:16:45 -0700, "RobG" <(E-Mail Removed)> wrote:
>
> > alert('Broke to A');

>
> Just figured out what alert does. This is a great tool for
> debugging!!! Thanks for mrntioning it.
>
>
>
> >> Also my 'check' function needs to pass an indication as to it's
> >> success or failure. It does it by:

>
> >> return ( "pass" ) ;

>
> >> Am I doing it correctly?

>
> >Why not just return true or false? Then you do:

>
> > if (check(row, col)) {
> > ...
> > }

>
> That looks more elegant. I did not know it.
> btw, how would I check for a false condition?


A break statement basically tells the code to terminate processing
within the current scope (block). In your case it would only have left
the check block, which is basically useless...

You might try something like:

function checkTable() {
var passed = true;
for (var row = 0; (row < 8 && passed); row++) {
for (var col = 0; (col < 8 && passed); col++) {
if (! check(row, col)) {
passed = false;
}
}
}
return passed;
}

Once the check return fails, no other loops will be processed and the
false condition will be returned. As long as check returns true, the
loops will continue and true will be returned.

Notice that we returned not a string, but a boolean value (true or
false, no quotes). Your check function should do that as well. This
way you can use the returned value in conditional testing AS true or
false as we did in our line

if (! check(row, col)) {

HTH.

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 8:31 pm, Cogito <(E-Mail Removed)> wrote:
> On 20 Mar 2007 00:16:45 -0700, "RobG" <(E-Mail Removed)> wrote:

[...]
> > if (check(row, col)) {
> > ...
> > }

>
> That looks more elegant. I did not know it.
> btw, how would I check for a false condition?


Use the logical NOT operator ! so that false is true and true is
false:

if (!check(row, col)) { ... }


 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-20-2007
On Mar 20, 11:22 pm, "Tom Cole" <(E-Mail Removed)> wrote:
[...]
> You might try something like:
>
> function checkTable() {
> var passed = true;
> for (var row = 0; (row < 8 && passed); row++) {
> for (var col = 0; (col < 8 && passed); col++) {
> if (! check(row, col)) {
> passed = false;
> }


Consider replacing the if block with:

passed = !check(row, col);


which leads to the question why the check() function is returning the
opposite of what is required.


--
Rob

 
Reply With Quote
 
Tim Streater
Guest
Posts: n/a
 
      03-20-2007
In article <(E-Mail Removed)>,
Cogito <(E-Mail Removed)> wrote:

> On 20 Mar 2007 00:16:45 -0700, "RobG" <(E-Mail Removed)> wrote:
>
> > alert('Broke to A');
> >

>
> Just figured out what alert does. This is a great tool for
> debugging!!! Thanks for mrntioning it.
>
>
> >>
> >> Also my 'check' function needs to pass an indication as to it's
> >> success or failure. It does it by:
> >>
> >> return ( "pass" ) ;
> >>
> >> Am I doing it correctly?

> >
> >Why not just return true or false? Then you do:
> >
> > if (check(row, col)) {
> > ...
> > }
> >

>
> That looks more elegant. I did not know it.
> btw, how would I check for a false condition?


Personally I do this:

if (check(row,col)==true)

or

if (check(row,col)==false)

as appropriate, being much more readable. All this stuff with:

if (!check(row,col))

just gives me a headache.
 
Reply With Quote
 
Dr J R Stockton
Guest
Posts: n/a
 
      03-20-2007
In comp.lang.javascript message <67vuv2tsvj2vr7hb4jf0up90b5claa18gr@4ax.
com>, Tue, 20 Mar 2007 06:33:47, Cogito <(E-Mail Removed)> posted:
>For the first time, I'm attempting to write a small Javascript program
>using one on the online reference sites. I need some confirmation as
>to the behaviour of the break statement.
>
>In the following code:
>
> for ( row = 0 ; row <= 7 ; row++ ) A <----
> {
> for ( col = 0 ; col <=7 ; col++ ) B <----
> {
> if ( check ( row, col ) == "pass" )
> break ;
> }
> }
> }
>
>Where will control pass to once the break statement is executed?
>Will it continue with the first 'for' statement (A) or the second (B)?
>
>Also my 'check' function needs to pass an indication as to it's
>success or failure. It does it by:
>
> return ( "pass" ) ;
>
>Am I doing it correctly?
>
>Any links to useful reference sites would be welcomed.


The following structures should do what you seem to need.

function check(a, b) { return a*b != 21 } // Test dummy

function owZat() { var row, col
for ( row = 0 ; row <= 7 ; row++ )
for ( col = 0 ; col <= 7 ; col++ )
if ( ! check ( row, col ) ) return false
return true }

// or

OK = true
DOWN:
for ( row = 0 ; row <= 7 ; row++ )
for ( col = 0 ; col <= 7 ; col++ )
if ( ! check ( row, col ) ) { OK = false ; break DOWN }


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.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      03-20-2007
Tom Cole wrote:
<snip>
> A break statement basically tells the code to terminate processing
> within the current scope (block).


As the units of scoping in javascript are functions not blocks, that is
a misleading assertion. In addition, all looping constructs may use any
statement so not necessarily a block statement, so break does not even
imply exiting a block, just an iteration statement.


> In your case it would only have left
> the check block, which is basically useless...
>
> You might try something like:
>
> function checkTable() {
> var passed = true;
> for (var row = 0; (row < 8 && passed); row++) {
> for (var col = 0; (col < 8 && passed); col++) {
> if (! check(row, col)) {
> passed = false;
> }
> }
> }
> return passed;
>
> }

<snip>

A ladled break statement would be as effective.

function checkTable(){
var passed = true;
outerLoop: for(var row = 0;row < 8;++row){
for(var col = 0;col < 8;++col){
if(!(passed = check(row, col))){
break outerLoop;
}
}
}
return passed;
}

Richard.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
How to break a while loop inside a switch statement? howachen@gmail.com Java 16 07-11-2006 10:23 AM
Why doesn't "switch" statement have "break" as default? MaXiaochi@gmail.com Java 36 06-06-2005 08:28 AM
extending the break statement Michele Simionato Python 9 10-23-2003 08:09 PM



Advertisments