How does the break statement work?

Discussion in 'Javascript' started by Cogito, Mar 20, 2007.

  1. Cogito

    Cogito Guest

    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.
     
    Cogito, Mar 20, 2007
    #1
    1. Advertising

  2. Cogito

    RobG Guest

    On Mar 20, 4:33 pm, Cogito <> 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
     
    RobG, Mar 20, 2007
    #2
    1. Advertising

  3. Cogito

    Cogito Guest

    On 20 Mar 2007 00:16:45 -0700, "RobG" <> wrote:

    >Syntax error: missing opening


    Thanks for your reply.
    Is there a tool to check syntax? I just use notepad to type the
    program.
     
    Cogito, Mar 20, 2007
    #3
  4. Cogito

    Cogito Guest

    On 20 Mar 2007 00:16:45 -0700, "RobG" <> 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?
     
    Cogito, Mar 20, 2007
    #4
  5. Cogito

    Tom Cole Guest

    On Mar 20, 5:31 am, Cogito <> wrote:
    > On 20 Mar 2007 00:16:45 -0700, "RobG" <> 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.
     
    Tom Cole, Mar 20, 2007
    #5
  6. Cogito

    RobG Guest

    On Mar 20, 8:31 pm, Cogito <> wrote:
    > On 20 Mar 2007 00:16:45 -0700, "RobG" <> 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)) { ... }
     
    RobG, Mar 20, 2007
    #6
  7. Cogito

    RobG Guest

    On Mar 20, 11:22 pm, "Tom Cole" <> 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
     
    RobG, Mar 20, 2007
    #7
  8. Cogito

    Tim Streater Guest

    In article <>,
    Cogito <> wrote:

    > On 20 Mar 2007 00:16:45 -0700, "RobG" <> 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.
     
    Tim Streater, Mar 20, 2007
    #8
  9. In comp.lang.javascript message <67vuv2tsvj2vr7hb4jf0up90b5claa18gr@4ax.
    com>, Tue, 20 Mar 2007 06:33:47, Cogito <> 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.
     
    Dr J R Stockton, Mar 20, 2007
    #9
  10. 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.
     
    Richard Cornford, Mar 20, 2007
    #10
  11. Cogito

    Evertjan. Guest

    Richard Cornford wrote on 20 mrt 2007 in comp.lang.javascript:

    > 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)
     
    Evertjan., Mar 20, 2007
    #11
  12. "Evertjan." wrote:
    > Richard Cornford wrote on 20 mrt 2007 in comp.lang.javascript:
    >
    >> 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.
     
    Richard Cornford, Mar 20, 2007
    #12
  13. Cogito

    Cogito Guest

    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.
     
    Cogito, Mar 21, 2007
    #13
  14. In comp.lang.javascript message <1b2203dtih5ts0l99m94fin52ra92gskqg@4ax.
    com>, Wed, 21 Mar 2007 10:44:40, Cogito <> posted:
    >
    >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.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.
     
    Dr J R Stockton, Mar 21, 2007
    #14
  15. Cogito

    Cogito Guest

    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?
     
    Cogito, Mar 22, 2007
    #15
  16. Cogito

    RobG Guest

    On Mar 22, 4:24 pm, Cogito <> wrote:
    > 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
     
    RobG, Mar 22, 2007
    #16
  17. Cogito

    Cogito Guest

    On 22 Mar 2007 01:22:30 -0700, "RobG" <> wrote:

    >On Mar 22, 4:24 pm, Cogito <> wrote:
    >> 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.
     
    Cogito, Mar 22, 2007
    #17
  18. Cogito

    Evertjan. Guest

    Cogito wrote on 22 mrt 2007 in comp.lang.javascript:

    > On 22 Mar 2007 01:22:30 -0700, "RobG" <> wrote:
    >
    >>On Mar 22, 4:24 pm, Cogito <> wrote:
    >>> 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)
     
    Evertjan., Mar 22, 2007
    #18
  19. Cogito

    Cogito Guest

    >
    >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.
     
    Cogito, Mar 22, 2007
    #19
  20. Cogito

    Evertjan. Guest

    Cogito wrote on 22 mrt 2007 in comp.lang.javascript:

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


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



    --
    Evertjan.
    The Netherlands.
    (Please change the x'es to dots in my emailaddress)
     
    Evertjan., Mar 22, 2007
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    36
    Views:
    1,520
    Chris Uppal
    Jun 6, 2005
  2. Replies:
    16
    Views:
    3,722
    steve
    Jul 11, 2006
  3. Michele Simionato

    extending the break statement

    Michele Simionato, Oct 22, 2003, in forum: Python
    Replies:
    9
    Views:
    289
    Derrick 'dman' Hudson
    Oct 23, 2003
  4. Colin King

    Switch statement without break

    Colin King, Oct 9, 2005, in forum: C Programming
    Replies:
    7
    Views:
    462
    Skarmander
    Oct 10, 2005
  5. Replies:
    12
    Views:
    987
Loading...

Share This Page