2.6. Process Control Statement 2.6.1. Judge
Judgment statement
Syntax for the IF statement:
if (condition) statement1 else statement2
If the condition evaluates to True, Statement1 is executed and statement2 is executed if the condition evaluates to False.
Attention:
An automatic type conversion occurs when a condition is judged:
Number: False if the non 0 is true,0
String: False if non-null or non-NULL is True
Undefined:false
Nan:false
Object type: Non-null is true, otherwise false.
2.6.2. Select
The syntax of the switch statement:
switch (expression) case value:statement; break; Case Value:statement; break; Case Value:statement; break; Case Value:statement; break; Case Value:statement; break; Default:statement;
Each case is a representation of "execute statement if expression equals value".
The keyword break causes the code to jump out of the switch statement. If there is no keyword break, the code execution continues into the next case.
The keyword default describes an action when the result of an expression is not equal to any one of the cases (in fact, it is relative to the ELSE clause).
Switch statements in ECMAScript and Java
The switch statements in ECMAScript and Java differ by two points. In ECMAScript, the switch statement can be used for strings, and it can use values that are not constants to describe the situation:
1. Basic data types can be passed to the switch case statement.
2. The case statement can be an expression
function Test6 () {var color = "XX"; var value1 = "Red", value2 = "green"; switch (color) {case value1: alert ("Red"); break; Case value2: alert ("green"); break; Default: alert ("execute default");
here, the switch statement is used for string scolor, which declares that the case uses the variable BLUE, R ED and GREEN, which is perfectly effective in ECMAScript.
Practice:
A Boolean value is passed in switch?
var num = 20;switch (true) {case num >= 0 && num <=: alert ("greater than 0 is less than or equal to Ten"); break; Case NUM>10&&NUM<=20: alert ("Greater than 10 is less than or equal to"); break;}
2.6.3. Loops
while Statement
while The statement is to test the loop first. This means that the exit condition is calculated before executing the code inside the loop. Therefore, the loop body may not be executed at all.
Its syntax is as follows:
while (expression) statement
Example:
var i = 0;while (i <) { i + = 2;}
Do-while Statement
Do-while The statement is a post-test loop, that is, the exit condition is evaluated after the code inside the loop is executed. This means that the loop body is executed at least once before the expression is evaluated.
Its syntax is as follows:
Do {statement} while (expression);
Example:
var i = 0;do {i + = 2;} while (I < 10);
for Statement
for The statement is a pre-test loop, and can initialize the variable before entering the loop, and define the code to execute after the loop.
Its syntax is as follows:
for (initialization; expression; post-loop-expression) statement
Note: post-loop-expression You cannot write a semicolon after this, or you cannot run it.
Example:
ICount = 6;for (var i = 0; i < ICount; i++) {
This code defines an initial value of 0 the variableI. Only if the conditional expression (I < ICount) has a value oftruebefore entering the forLoop so that the body of the loop may not be executed. If the loop body is executed, the loop-after expression is executed and the variable is iteratedI.
for There is no local variable in the loop, it is a global variable.
2 . 6.3. in statement
for A statement is a strict iteration statement that enumerates the properties of an object or iterates through an array of elements.
Its syntax is as follows:
For (property in expression) statement
Example:
For (Sprop in window) { alert (sprop);}
here, for-in statement is used to display window all properties of the object.
2.6.4. withStatement
wth the function of the statement: with the with statement, you do not have to specify the reference object repeatedly when accessing object properties and methods.
format :
With (obj) { Operation obj Property statement; }
<script type= "Text/javascript" > <!-with (document) { write ("Hello!"); Write ("<br> title of this document is: \" "+ title +" \ "); Write ("<br> URL of this document is:" + URL); Write ("<br> now you don't have to write the document object prefix every time!"); } --></script>
JavaScript Learning Summary-Basic syntax-(iii)