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>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)