First entry JavaScript Knowledge point (ii)

Source: Internet
Author: User

5.while StatementsWhile (condition) {

code block

Executes a code block when the Boolean value of the condition is true. Returns the Boolean value that determines the condition, and continues to execute the code block if it is still true. And so on, infinite loops. When a Boolean value of false is the sum of the while statement.

example 1:1+2+3+4+5+...+100=?

var s = 0;

var i = 1;

while (i<=100) {

s = s+i;

i++;

}

Console.log (s);

Example 1:1+2-3+4-5+ ... 100 =?

var S=1;

var i=2;

var isadd=1//Note naming rules general first word lowercase the first letter of the second word is capitalized and there are no spaces between the words.

while (i<=100) {

s + = iisadd;//This is ashorthand for s=s+i isadd.

i++;

Isadd=-isadd;

}

Console.log (s);

Braek Statements

function in a looping statement: ends the entire loop statement.

Continue statements

Terminates this cycle, and the subsequent loop continues.

both the break statement and the continue statement can use any of the loop statements6.do...while ... Statementdo{①}while (②) ③;

Execute the ① first, then judge whether the Boolean value of ② is true, is to execute ①, so loop, no ③.

7.for Cyclefor (initialize; Judge condition; increment) {code block}the order in which the For loop executes the code:

1. Execute the first statement of the parentheses, that is, initialize.

2. Execute the second statement of the parenthesis to determine whether its Boolean value is true. Is the code block that executes the curly braces, and no end the entire loop statement.

3. After each execution of a block of curly braces, the third statement of the parentheses is executed.

4. Each time the third statement of the parentheses is executed, the second statement of the parentheses is executed, the Boolean value is judged, and so is repeated.

8. Arraysvar array name [variables, variables , variables, variables, variables, variables, variables,]

When you want to use the name of the variable directly [the first item]

Tip: The first item in the array is from 0,1,2,3,4,5,6, ... began to count.

Enter the day of the month, judging the date of the entry is the first day of the year

var year = +prompt (' Please enter years ');

var month = +prompt (' Please enter the month ');

var day = +prompt (' Please enter Date ');

Determine if this year is a leap years

var Isrunnian = (Year% 4 = = = 0 && Year% 100!== 0) | | Year% 400 = = = 0;

How many days per month are the rules

var rules = [ Isrunnian, 29:28, +, +,--] 22> );

var result = 0;

for (var i = 0; i < month-1; i++) {

result = result + Rules[i];

Result + = Rules[i];

}

Result + = day;

Console.log (year + ' years ' + month + ' month ' + day + ' date is ' + year + ' + ' + result + ' days ');

First entry JavaScript Knowledge point (ii)

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.