1, Process Control statements
) If statement
if (condition) {
Statement1
}else{
Statement2
}
Condition represents an arbitrary expression, the result of which the expression evaluates is not necessarily a Boolean type, and if it is not a Boolean type, ECMAScript calls the Boolean () conversion function to convert the expression result to a Boolean type, and if the condition evaluates to True, the value of the Execute statement1. If the evaluation result is false, execute the Statement2
if (condition) {
Statement1
}
Statement2
while (true) {
var text=prompt ("Please enter a number");
var num=number (text);
if (num) {
if (num%3==0) {
Alert ("The number is a multiple of 3. ");
}else{
Alert ("The number is not a multiple of 3.) ");
}
}else{
Alert ("Re-enter. ");
}
if (text== "") {
Break
}
}
Alert ("Welcome next use!");
) switch statement
switch (expression) {
Case Val:
Break
Case VAL2:
Break
Default
}
You can use any data type in a switch statement.
The value of a case is not necessarily a constant, it can be a variable or even an expression.
Principle of execution
Condition = = = constant
Break every case statement should have a break at the end
The default statement, if at the end of the code, does not require a break, or vice versa, requires
var day = prompt ("Please enter the number you want to convert");
Day = +day;//Day = number (day);
if (Day && day>=1 && day<=7) {
Switch (day) {
Default
Console.log ("Sunday");
Break
Case 1:
Console.log ("Monday");
Break
Case 2:
Console.log ("Tuesday");
Break
Case 3:
Console.log ("Wednesday");
Break
Case 4:
Console.log ("Thursday");
Break
Case 5:
Console.log ("Friday");
Break
Case 6:
Console.log ("Saturday");
Break
}
}else{
Console.log ("illegal input");
}
2. The loop statement
01) for statement
for Loop is a pre-test loop statement, but it has the ability to initialize variables and define the code to execute after the loop before the loop is executed. The following is the syntax for A For loop:
Initialize expression, control expression, post-loop expression
for ( initialization;expression;post-loop-expression) {
//loop option
}
Example:
var sum = 0;
for (var i=0;i<10;i++) {
sum +=i;
}
console.log (i);//10
99 Multiplication Table
for (Var i=1;i<=9;i++) {
for (Var j=1;j<=i;j++) {
document.write ("★" + "<br/>");
document.write (j+ "*" +i+ "=" +i*j+ " ");
}
document.write ("<br/>")
}
Output 10 rows 10 Columns ★
for (Var i=1;i<=10;i++) {
for (Var j=1;j<=10;j++) {
document.write ("★");
}
document.write ("<br/>")
}
/*
var i;
i = 1;
i<=100//true
Total +=i//Total 1
++i; i=2
i<=100//true
Total +=i//total 3
++i; I=3
i<=100//true
Total+=i//total 6
++i//i=4
...
i<=100;
Total+=i
++i//i=101
i<=100
*/
var total = 0;
var i;
for (I=1; i<=100; ++i) {
Total + = i;
}
Console.log (total);
Console.log (i);/*js no block-level scope */
For loop output 1-100 all prime numbers (only the number divisible by 1 and itself, excluding 1)
for (Var i=2;i<=100;i++) {//prime number
for (Var a=2;a<=i;a++) {//divisor
if (i%a==0 && i!=a) {//Excludes all numbers that can be divisible by a before i=a
Break
}
else if (i%a==0 && i==a) {//outputs all the numbers in I=a and i%a=0
Console.log (i);
}
}
}
ECMAScript there is no block-level scope, variables defined inside the loop can also be accessed externally to the
Dead loop
for (;;) {
When an expression in for is omitted, a dead loop is created
}
while (true) {
}
Do-while statement
Initialize condition
do{
Iteration Conditions
Loop body
}while (end condition);
After the test loop statement, the export condition is tested only after the code in the loop body is executed. The code in the loop body is executed at least once.
/*
var total = 0;
var i = 1;
Total +=i; Total= 1
i++//i =2
i<=100; True
Total +=i; Total = 3
i++//i = 3
i<=100//true
...
Total +=i
i++//i=101
i<=100//false End Loop
*/
var total = 0;
var I=1;
do{
Total+=i;
i++;
}while (i<=100);
Console.log (total);
Console.log (i);
) While statement
initialization conditions;
while (end condition) {
Loop body
Iteration Conditions
}
The pre-test loop statement, that is, the exit condition is evaluated before the code in the loop body is executed. Therefore, the code in the loop body may never be executed.
while (false) {
//
}
/*
var total = 0;
var i = 1;
i<=100; True
Total +=i; Total = 1
i++; i = 2
i<=100//true
Total +=i//total = 3;
i++; 3
...
i++//i=101
i<=100//false terminating loop
*/
var total = 0;
var i = 1;
while (i<=100) {
Total + = i;
i++;
}
Console.log (total);
Console.log (i);
For-in statement
is a precise iterative statement that can be used to enumerate the properties of an object
For (property in expression) {
Statement
}
For example:
Print out all the properties in the Window object
for (var propname in window) {
Console.log (propname);
}
Label statement
Use label to add tags to your code for future use
label:for (int i=0;i<10;i++) {
if (i = = 5) {
Break label;
}
}
Loop keywords
Break jumps out of the loop body
for (Var i=0;i<10;i++) {
if (i==5) {
Break
}
}//0,1,2,3,4 jump directly out of the loop
Continue end this cycle and proceed to the next cycle
for (Var i=0;i<10;i++) {
if (i==5) {
Continue
}
}//0,1,2,3,4,6,7,8,9 jump out of this cycle, continue the next cycle
3. Special statements
With statement
The main purpose is to set the scope of the code in a particular object, and the primary goal of defining the With statement is to simplify the work of accessing the same object multiple times, but the large use of with statements can cause performance degradation and is difficult to maintain, so it is not recommended.
For example:
var person = {
Name: ' Terry ',
Age:12,
Gender: ' Male '
}
To access the properties in the person object requires:
Console.log (Person.name);
Console.log (Person.age);
Console.log (Person.gender);
However, you can also use the WITH statement to implement
With (person) {
Console.log (name);
Console.log (age);
Console.log (gender);
}
javascript--statements