473,804 Members | 2,243 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
22 2541
In comp.lang.javas cript message <67vuv2tsvj2vr7 hb4jf0up90b5cla a18gr@4ax.
com>, Tue, 20 Mar 2007 06:33:47, Cogito <no****@nospam. nospamposted:
>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.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 #11
Richard Cornford wrote on 20 mrt 2007 in comp.lang.javas cript:
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;
}


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 20 '07 #12
"Evertjan." wrote:
Richard Cornford wrote on 20 mrt 2007 in comp.lang.javas cript:
>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))){

==
<snip>

Look again.

Richard.
Mar 20 '07 #13
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.
Mar 21 '07 #14
In comp.lang.javas cript message <1b2203dtih5ts0 l99m94fin52ra92 gskqg@4ax.
com>, Wed, 21 Mar 2007 10:44:40, Cogito <no****@nospam. nospamposted:
>
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) 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 22 '07 #15
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?
Mar 22 '07 #16
On Mar 22, 4:24 pm, Cogito <nos...@nospam. nospamwrote:
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.

--
Rob

Mar 22 '07 #17
On 22 Mar 2007 01:22:30 -0700, "RobG" <rg***@iinet.ne t.auwrote:
>On Mar 22, 4:24 pm, Cogito <nos...@nospam. nospamwrote:
>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.
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.
Mar 22 '07 #18
Cogito wrote on 22 mrt 2007 in comp.lang.javas cript:
On 22 Mar 2007 01:22:30 -0700, "RobG" <rg***@iinet.ne t.auwrote:
>>On Mar 22, 4:24 pm, Cogito <nos...@nospam. nospamwrote:
>>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.

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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Mar 22 '07 #19
>
>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.
Mar 22 '07 #20

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
24801
by: Sivas | last post by:
Hi, Can anyone tell me why this does not work: --------------------------------------------- float b = 2.51F; switch(b) {
1
1475
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
2028
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
2131
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
10226
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
10014
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
10600
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...
0
10352
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10097
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9175
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
7642
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
6867
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
5535
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...
0
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3002
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.