JS Basics 3

Source: Internet
Author: User

<! DOCTYPE html>
<meta charset= "UTF-8"/>
<title>Document</title>
<body>

</body>
<script type= "Text/javascript" >
Loop structure
Loops are divided into cyclic conditions and cyclic bodies
The cyclic structure is an important structure in the program, which is characterized by the repeated execution of a certain procedure when a given condition is established, until the condition is not established;
Classification
While loop do: While loop for loop
Cyclic three elements
1. Cycle start 2. The end condition of the Loop 3. Step forward;
//
While loop
while (loop condition) {
Statement
//}
Attention:
1. Judge the expression first, execute the statement after the expression is established
2. Variables used in cyclic conditions (loop variables) need to be initialized
3. In the loop body, there should be conditions to end the cycle, otherwise it will cause a dead loop
4. The loop body can write several sentences of the JS code, including if can also nest loop statements
1-100 of and
var I=1;
var sum=0;
while (i<=100) {
Sum+=i;
i++;
// }
Console.log (sum);
//
Number of M to n outputs
var m=number (Prompt ("Please enter M"));
var n=number (Prompt ("Please enter n"));
if (m>n) {
var i=n;
while (i<m) {
Console.log (i);
i++;
// }
// }
else if (m=n) {
Console.log ("No middle number");
// }
else {
var i=m;
while (I<n) {
Console.log (i);
i++;
// }

// }
//
//
Factorial
var i=5;
var j=1;
var sum=1;
while (j<=i) {
Sum*=j
j + +;
Console.log (sum);
// }
//
Do ... while
Syntax format
do{
Statement
}while (expression);
Note: In the loop body, there should be a condition to end the loop, otherwise it will cause a dead loop
example, 1 to 100 of the and;
var I=1;
sum=0;
do{
Sum+=i;
i++;
}while (i<=100);
Console.log (sum);
//
//
//
Odd even and number of M to n
var n=number (Prompt ("Please enter n"));
var m=number (Prompt ("Please enter M"));
var sum1=0;
var sum2=0;
var x=0,y=0;
if (m>=n) {
var t=m;
M=n;
n=t;//Exchange N Large
// }
do{
if (m%2==0) {

Sum1+=m;
x + +;
m++;
}else{

Sum2+=m;
y++;
m++;
// }
}while (m<=n);
Console.log ("Number of even and" +sum1+ "+x," odd and "+sum2+" +y ");


1 to 100 odd even numbers;
var I=1;
var sum1=0;
var sum2=0;
var x=0,y=0;
do{
if (i%2==0) {
Sum1+=i;
x + +;
// }
else{
Sum2+=i;
y++;
// }
i++;
}while (i<=100);
Console.log ("Number of even and" +sum1+ "+x," odd and "+sum2+" +y ");
//
//
The difference between the while and do while
1. If the loop condition is true for the first time, there is no difference
2. If the loop condition is false for the first time, the Do While loop performs at least one loop ti, while the while does not execute at a time;
The 3.while is first judged in execution, and do while is performed first, in judgment;
//
var i=0;
while (I&GT;10) {
i++;
// }
Console.log (i);
var j=0;
do{
i++;
}while (I&GT;10);
Console.log (i);
//
//
for (multifunction Loop)
for (expression 1; expression 2; expression 3)
//{
Statement
//}
Execution process:
1. Solve the expression 1 first (only once);
2. Solve the expression 2, if its value is true (not 0),
Executes the inline statement specified in the FOR statement,
Then solve the expression 3, in the solution Expression 2, if False, then the end of the loop, in the execution of the statement outside the For loop;
//
99 Multiplication Table
for (Var i=1;i<=9;i++) {

for (Var j=1;j<=i;j++) {
document.write (i+ "*" +j+ "=" + (I*J) + "");
// }
document.write ("</br>");
// }
One-dollar conversion method
var i=0;
for (var y = 0;y<=10;y++) {
for (var e = 0;e<=5;e++) {
for (var w = 0;w<=2;w++) {
if (y*1+e*2+w*5==10) {
Console.log (y+ "Zhang Yi Mao" +e+ "Zhang two Mao" +w+ "Zhang Five Mao");
i++;
// }
// }
// }
// }
Console.log (i);
//
//
Break Statement functionality
//
Using the process to jump out of the switch structure in a switch statement
Make the process jump out of the current loop in a looping statement
//
Emphasize
1. If a break statement has been executed, the statement after break in the loop body will not be executed
2. In a multilayer loop, a break statement jumps only one layer outward;
//
Functions of the Continue statement
Can only be used in a loop statement so that the loop ends, skipping statements that are not yet executed in the loop body
And then the next time the loop is executed.
//
Emphasize
Continue statements can only be used in loops.
//
for (Var i=1;i<=4;i++) {
for (Var j=1;j<=4;j++) {
if (i== (4-j)) {
Continue

// }
Console.log (j+ "")

// }

// }
</script>

JS Basics 3

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.