Thursday, July 31, 2014

Break and continue statements

-For and while loops:

  • we talked in previous lesson about for and while loops and we now know what is it used for and you can check it again here For and while loops
  • sometimes we want to make a condtion if it happens then we dont want our for or while loop to continue just break that loop and execute the code out of that loop
-Break:

  • its a javascript Keyword which used to stop the executing of the code running inside the for or while loop 
  • you can use it for different purpose we wont make a specific example here :

Comparison and Logical operations

as you can see this will lead to add 1 to the first and second elements of the array but when it'll come to x[3]=55 this will break the loop so the third and fourth elements wont be added to one.

  • its the same for the while loops 


-Continue:
  • its has a opposite job this will check the condition and see if its true then go and execute the code in the for or while loops but if its not true then it wont execute it but it wont get out the for loop it wont break it thats very important to know so after it continue it'll start a new counter.
this example shows that when the array value is 55 it wont add one to this element it'll increment the "i" counter and start a new loop.

-Related to this post:

No comments:

Post a Comment