JS (conditional statement, loop statement)

Source: Internet
Author: User

Conditional statement IF

Syntax:if (condition) statements1 else Statement2

Chestnuts:

<Scripttype= "Text/javascript">    varI=Prompt ("Please enter your score."); if(i>=  the &&I<= -) {alert ("Good grades"); }Else if(i>=  - &&I<  the) {alert ("Score Qualified"); }Else if(i<  - &&I>=0) {alert ("failure to pass the grade"); }</Script>

Conditional Statement Switch

Grammar:

switch (expression) {case        value:            //statement break            ;        Case value:            //statement break            ;        Default:            //statement    }

Chestnuts:

<Scripttype= "Text/javascript">    varI=  -; Switch(i) { Case  -: Alert (i+ "equals");  Break;  Case  -: Alert ( -);  Break; default: Alert (" Other"); }</Script>

While loop

Syntax: while (exp) {//statements;}

Description: while (variable <= end value) {code to be executed}

Chestnuts:

<Scripttype= "Text/javascript">    varI= 1;  while(i< 3) {alert (i); I++; }</Script>

Do...while Cycle

Syntax: do {//statements;} while (condition);

Description: Do {code to be executed} while (variable <= end value)

Chestnuts:

<Scripttype= "Text/javascript">    varI= 1;  Do{alert (i); I++; } while(i< 3);</Script>

For loop

Syntax: for (initial; expression; post-loop-expression) {//statement;}

Description: for (variable = start value; variable <= end value; variable = variable + step value) {Code to execute}

Chestnuts:

<Scripttype= "Text/javascript">     for(varI= 1; I< 3; I++) {alert (i); }</Script>

Break statements and Continue statements

Description: The break statement can exit the loop immediately; The continue statement simply exits the current loop;

Chestnuts:

<Scripttype= "Text/javascript">    varN= 0;  for(varI= 1; I< Ten; I++){        if(i== 4){            Continue; } N++;        } alert (n); varm= 0;  for(varJ= 1; J< Ten; J++){        if(J> 5){             Break; } m++; } alert (m);</Script>

JS (conditional statement, loop statement)

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.