How does the break statement work?

C

Cogito

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.
 
R

RobG

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/ >
 
C

Cogito

alert('Broke to A');

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

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?
 
T

Tom Cole

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






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.
 
R

RobG

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)) { ... }
 
R

RobG

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.
 
T

Tim Streater

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



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.
 
D

Dr J R Stockton

In comp.lang.javascript message <67vuv2tsvj2vr7hb4jf0up90b5claa18gr@4ax.
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.
 
R

Richard Cornford

Tom Cole wrote:
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.
 
C

Cogito

Many thanks for all your replies and comments. They were all of great
help. I have implemented some of them but unfortunately my program now
goes into a state of deep thoughts and produces a beautiful white
screen.

As they say, back to the drawing board. I think that my two nested
'for' loops will have to go and be replaced by a single 'while' loop.
It seems that since I modify the values of 'col' and 'row' in the loop
itself, I somehow end up in an endless loop.

One more question before I embark on reshaping the code: Can you have
multiple conditions to control the termination of a loop ('for' or '
while') or is it limited just one condition? Searching through some of
the sites I could not find an example with more than one test.
 
D

Dr J R Stockton

In comp.lang.javascript message <1b2203dtih5ts0l99m94fin52ra92gskqg@4ax.
As they say, back to the drawing board. I think that my two nested
'for' loops will have to go and be replaced by a single 'while' loop.
It seems that since I modify the values of 'col' and 'row' in the loop
itself, I somehow end up in an endless loop.

One can do that in javascript, but it is generally better not to alter
the control values for FOR loops in the body of the loop.
One more question before I embark on reshaping the code: Can you have
multiple conditions to control the termination of a loop ('for' or '
while') or is it limited just one condition? Searching through some of
the sites I could not find an example with more than one test.

There can only be a single Boolean condition; but the calculation of
that can be as complex as you like.

This peculiar sample code, if executed in alternate 5-second periods on
a Wednesday, will whizz-count in status until the end of the period, and
otherwise does nothing :-
for ( ; D = new Date(), D.getSeconds()%10<5 && D.getDay()==3 ; )
{ window.status++ }

A break statement will also terminate a loop; and the condition for
its execution is a Boolean value, calculated however you wish; so the
contents of if( ) can be a complex Boolean expression.

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

Cogito

I have one more very basic question. How do you check for typing
errors?
Say, I type my program using Notepad and I have a variable called
'area'. Some fifty lines of code later I make reference to this
variable and due to a typo I enter 'arae'. How will I ever discover
the mistake?
 
R

RobG

I have one more very basic question. How do you check for typing
errors?
Say, I type my program using Notepad and I have a variable called
'area'. Some fifty lines of code later I make reference to this
variable and due to a typo I enter 'arae'. How will I ever discover
the mistake?

Your program probably won't work the way you expect. Frequent and
thorough testing is always required.
 
C

Cogito

Your program probably won't work the way you expect. Frequent and
thorough testing is always required.

I have no doubt that the program won't work but how would you detect
such an error?
In the days when I programmed mainframe computers, the process of
compiling the program would highlight such mistakes well before
program execution.
 
E

Evertjan.

Cogito wrote on 22 mrt 2007 in comp.lang.javascript:
I have no doubt that the program won't work but how would you detect
such an error?
In the days when I programmed mainframe computers, the process of
compiling the program would highlight such mistakes well before
program execution.

However, in scripting that is not the case.
In scripting there is no compiling involved in the sense that the compiled
result is distributed. That is why it is called scripting.

On the other hand we noow have the concept of modular programming,
helping you in the art of debugging.

I hope you mean "area" as the name of a js variable.

for <area .. > please ask a html NG.
 
C

Cogito

I hope you mean "area" as the name of a js variable.

for <area .. > please ask a html NG.


Actually I did not. It was just a word I picked at random. I was just
wondering if there is a way of detecting typos other than careful
scrutiny.
 
E

Evertjan.

Cogito wrote on 22 mrt 2007 in comp.lang.javascript:
Actually I did not. It was just a word I picked at random. I was just
wondering if there is a way of detecting typos other than careful
scrutiny.

JS being a easy comlpiance language,
no that usually is not possible.
[VBS has "option explicit"]

Some help may come from using an editor with code colouring,
like:

<http://www.editpadpro.com/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top