www.webdeveloper.com
Results 1 to 4 of 4

Thread: break loop not working?

  1. #1
    Join Date
    Aug 2012
    Posts
    2

    break loop not working?

    Hello

    I am new to programming and to a javascript..
    I just want to ask why my codes doesnt run correctly?

    see my code

    Code:
    for(i=1;i<=2;i++)
    {
    	if(i===alert2)
    	{
    	break;
    	}
    var alert1;
    alert("1");
    
    var alert2;
    alert("2");
    
    var alert3;
    alert("3");
    }
    I want to stop the loop when the code reach my var alert2,
    can somebody explain me why?the code ignore the if statement..

    thanks

  2. #2
    Join Date
    Mar 2007
    Location
    U.K.
    Posts
    1,127
    Don't worry, your if statement is not being ignored.
    At what point does alert2 get a value that could match that of i ?
    Where used, return should be executed unconditionally and always as the last statement in the function.

    That's my signature, it's not part of the damn post!

  3. #3
    Join Date
    Sep 2011
    Location
    Bristol, England, United Kingdom
    Posts
    192
    I'll just echo the sentiments of Logic Ali; your if statement isn't being ignored. You might try putting an else clause in your code just so you can tell whether or not your logic is correct.

  4. #4
    Join Date
    Aug 2012
    Posts
    2
    Hello thanks for the reply..
    my friend told me that I need to parsInt the var, so that the integer i will read my alert2 as integer.. but I dont know how to parsInt each var. this is my 1week learning of javascripting, maybe I need to learn more..

    parsInt is confusing to me., can anybody correct the code?
    thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles