The basic composition of JavaScript scripting language is programmed by control statements, functions, objects, methods, attributes, etc.
One, program control flow
In any language, program control flow is necessary, it can make the whole program reduce chaos, so that it smoothly in a certain way to execute. The following are commonly used program control flow structures and statements for JavaScript:
1, if condition statement
Basic format
if (expression)
statement paragraph 1;
......
Else
statement paragraph 2;
.....
Function: If the expression is true, the statement segment 1 is executed, otherwise the statement segment 2 is executed.
Description
The If-else statement is the most basic control statement in JavaScript, which allows you to change the order in which statements are executed.
In an expression, you must use a relational statement to implement the judgment, which is evaluated as a Boolean value.
It converts 0 and nonzero numbers to false and true respectively.
If the statement after if has more than one line, you must enclose it with curly braces.
Nesting of IF statements
if (Boolean) statement 1;
Else (Boolean) statement 2;
else if (Boolean) statement 3;
......
Else Statement 4;
In this case, each level of the Boolean expression will be computed, if true, then execute its corresponding statement, otherwise execute else after the statement.
2, for Loop statement
Basic format
for (initialization; condition; increment)
Statement set;
Function: Implement conditional cycle, when the condition is set up, execute statement set, otherwise jump out of circulation body.
Description
The initialization parameter tells the starting position of the loop, and the initial value of the variable must be given;
Condition: is used to determine the condition of the circular stop. If the condition satisfies, then executes the circulation body, otherwise jumps out.
Increment: The primary definition of how the loop control variable changes in each loop.
Three main statements, you must separate them with commas.
3, while loop
Basic format
while (condition)
Statement set;
This statement, like the For statement, repeats the loop when the condition is true, or exits the loop.
For and while statements
Both statements are circular statements, and a for statement is easier to understand and compact when working with the numbers, while the while loop works more specifically for complex statements.
4. Break and Continue statements
As with the C + + language, use the break statement to make the loop jump out of a for or while, continue to skip the remaining statements in the loop and into the next loop.