Section 5 break continue and Exception Handling in October 2014 by singles' Javascript, October 2014 by Singles' Day

Source: Internet
Author: User

Section 5 break continue and Exception Handling in October 2014 by singles' Javascript, October 2014 by Singles' Day

Let's talk about the main usage of break and continue. break is used to jump out of the loop and continue is used to skip an iteration in the loop. Simply put, break jumps out of the statement directly, but continue does not jump out of the loop statement, but it will come again, that is, it will not be executed again this time.

 

*************

1. Let's explain it through an example. First, let's look at code 1:

var sum = 0;for(i = 0;i<10;i++){    if(i==5){        break;    }    sum += i;}alert(sum);

2. the output result is 10, the reason is very simple, it will be increased from 0 to 4, and when the I value is 5, the program exits from this for loop, then the alert data is generated.

3. Then let's look at the following code:

Var sum = 0; for (I = 0; I <10; I ++) {if (I = 5) {// note that this is replaced with continue ;} sum + = I;} alert (sum );

4. At this time, the output result is 40. Why not 45? Because when the value of I is 5, it is called by the conitnue statement, and then the next loop is automatically executed.

5. In addition, the break statement can also be used in the switch, and the continue cannot be used in the switch statement.

 

**************

1. When it comes to error handling, it is actually the same as in Java. try... catch is used to handle errors, and throw is used to actively throw an exception.

2. First, let's take a look at the pseudo code of try... catch:

Try {// trial run code} catch (err) {// handle errors}

3. Then, let's take a look at the actual situation and run the following code:

try{    xinxing(3);}catch(err){    alert(err);}

4. The above code will pop up a dialog box with the following content:

ReferenceError: xinxing is not defined

5. What does it mean? It indicates that we have found this exception. It is a reference exception and xinxing is an undefined function.

6. We can also actively trigger exceptions. We use throw to trigger exceptions. Sample Code:

Try {throw "";} catch (err) {alert (err );}

7. Here we directly throw an exception in the try block. When we print it out, it will display a message "Xin Xing ".

 

*************

1. We first differentiate the difference between continue and break.

2. Then I briefly introduced the exception mechanism of Javascript. It is very simple if there are other programming basics.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.