473,796 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does the break statement work?

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.
Mar 20 '07 #1
22 2540
On Mar 20, 4:33 pm, Cogito <nos...@nospam. nospamwrote:
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

Mar 20 '07 #2
On 20 Mar 2007 00:16:45 -0700, "RobG" <rg***@iinet.ne t.auwrote:
>Syntax error: missing opening
Thanks for your reply.
Is there a tool to check syntax? I just use notepad to type the
program.
Mar 20 '07 #3
On 20 Mar 2007 00:16:45 -0700, "RobG" <rg***@iinet.ne t.auwrote:
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?
Mar 20 '07 #4
On Mar 20, 5:31 am, Cogito <nos...@nospam. nospamwrote:
On 20 Mar 2007 00:16:45 -0700, "RobG" <r...@iinet.net .auwrote:
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.

Mar 20 '07 #5
On Mar 20, 8:31 pm, Cogito <nos...@nospam. nospamwrote:
On 20 Mar 2007 00:16:45 -0700, "RobG" <r...@iinet.net .auwrote:
[...]
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)) { ... }
Mar 20 '07 #6
On Mar 20, 11:22 pm, "Tom Cole" <tco...@gmail.c omwrote:
[...]
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

Mar 20 '07 #7
In article <4p************ *************** *****@4ax.com>,
Cogito <no****@nospam. nospamwrote:
On 20 Mar 2007 00:16:45 -0700, "RobG" <rg***@iinet.ne t.auwrote:
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.
Mar 20 '07 #8
In comp.lang.javas cript message <67vuv2tsvj2vr7 hb4jf0up90b5cla a18gr@4ax.
com>, Tue, 20 Mar 2007 06:33:47, Cogito <no****@nospam. nospamposted:
>
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)?
It will go to B. However, except in javascript of impressive antiquity,
the statement break fred will go to the statement labelled fred:
and likewise for continue fred - the label must be placed
appropriately.

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.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 20 '07 #9
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.
Mar 20 '07 #10

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

Similar topics

1
18903
by: Jay | last post by:
G'day all This registration form checks for the submit button then displays the next form from the include statement. But before it displays the next form it will check to make sure the user has entered details correctly and give error messages of whats missing before displaying the original form again. My problem is that I can't exit from the 'if' statement, it brings up the first form as desired, but I can't get it to quit there. I...
5
1729
by: Colleyville Alan | last post by:
I have an application that uses SQL. I am writing a new piece to it, so I used the same approach that worked before. I set up a string and add to it, i.e. strMySql = "some Sql Statement" and then strMySql = strMySql + "some other new statement". The program keeps crashing saying I am running an invalid operation. I go to debug.print and copy the contents of the strMySql to the sql window. Sure enough, where the sql statement wrapped...
8
24800
by: Sivas | last post by:
Hi, Can anyone tell me why this does not work: --------------------------------------------- float b = 2.51F; switch(b) {
1
1472
by: Neo | last post by:
I am in dire need of a break statement in VB.NET language. I have several pieces of code that would have much cleaner look and much less deeper if/else/endif nests IF VB.NET HAS A BREAK STATEMENT OR SOMETHING SIMILAR. Will VB.NET has a break statement in the coming Whidbey release?
8
2026
by: Howard Kaikow | last post by:
I got bored today, so I decided to rewrite the code in KB article 316383 to decrease the number of object references. This resulted in a number of nested With ... End With. The original code had a Dim r As Integer, c As Integer shortly before a For Next.
18
2130
by: sunny | last post by:
Hi Why does C allows declaration of variable inside switch block. ex: foll prg does not gives "undeclared "b" error msg. but also does not initialize b to 20 int a=1; switch(a) { int b=20; case 1: printf("b is %d\n",b);
26
10222
by: Alexander Korsunsky | last post by:
Hi! I have some code that looks similar to this: -------------------------------------------- char array = "abcdefghij"; for (int i = 0; i < 10; i++) {
38
6137
by: larry | last post by:
I am looking at some code which process an uploaded file and it has loops like: for(;;){ // for([two semicolons){ what is the function of the two semicolons? loop until break? I've tried to google it but the text filter seems to remove the semicolons.
2
10013
boss32178
by: boss32178 | last post by:
Ok I get this error when i try to run the web app. foreach statement cannot operte on variables of type 'object' does not contain a public definition for 'GetEnumerator' here is the code #region Number String public string MyNum(System.Object MyString) { string temp; foreach (string mylet in MyString)
0
9529
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10176
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4119
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.