JS Second lesson (Loop statement)

Source: Internet
Author: User

1. Conditional statements

1) comparison operator

= =,! =, >, >=, <, <=

Turn case: toUpperCase (), toLowerCase ()

2) Logical operators

and Operations &&

or Operation | |

Non-op!

3) If statement

If (condition)

{ ....}

Else if (condition)

{....}

Else

{....}

Cases:

var iday=number (Prompt ("Please enter Week", ""));
if (IsNaN (Iday))
{
Alert ("Please enter a number");
}
else if (iday<1| | IDAY&GT;7)
{
Alert ("Please enter the correct week");
}
Else
{
Switch (iday)
{
Case 1:
{
Alert ("Monday");
Break
}
Case 2:
{
Alert ("Tuesday");
Break
}
Case 3:
{
Alert ("Wednesday");
Break
}
DEFA ult:
{
Break
}}
}

Prompt (...) input box

Number (..) converted to digital

IsNaN (..) Determines whether the parameter is Nande, Nan is true, and vice versa is False

4) switch

Switch (..)

{

Case: :

{

...

Break

}

Case: :

{

...

Break

}

Default

{

..

}

Cases:

Iweek = parseint (Prompt ("input integer from 1 to 7", ""));
Switch (Iweek) {
Case 1:
document.write ("Monday");
Break
Case 2:
document.write ("Tuesday");
Break
Case 3:
document.write ("Wednesday");
Break
Case 4:
document.write ("Thursday");
Break
Case 5:
document.write ("Friday");
Break
Case 6:
document.write ("Saturday");
Break
Case 7:
document.write ("Sunday");
Break
Default
document.write ("Error");
}

2. Circular statements

1) while

while (...)

{

...

}

Cases:

var i=isum=0;
while (i<=100) {
Isum + = i;
i++;
}
alert (isum);

2) Do.....while

Do

{

...

}

while (...)

Cases:

var anumbers = new Array ();
var smessage = "You have entered: \ n";
var itotal = 0;
var vuserinput;
var iarrayindex = 0;
do{
Vuserinput = Prompt ("Enter a number, or ' 0 ' exit", "0");
Anumbers[iarrayindex] = vuserinput;
iarrayindex++;
Itotal + = number (vuserinput);
Smessage + = vuserinput + "\ n";
}while (Vuserinput! = 0)//exit loop body when input is 0 (default)
Smessage + = "Total:" + itotal;
alert (smessage);

3) for

For (...)

{

....

}

Cases:

for (Var i=1;i<10;i++) {//multiplication table altogether nine rows
document.write ("<tr>");//row is a row of table
for (j=1;j<10;j++) {//each row has 9 cells
if (j<=i)
{//Cells with content
document.write ("<td style= ' border:2px solid #004B8A; Background: #FFFFFF; ' > "+i+" * "+j+" = "+ (i*j) +" </td> ");
}
else//cells with no content
document.write ("<td style= ' Border:none; ></td> ");}
document.write ("</tr>");
}

JS Second lesson (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.