1. Conditional statements
(1). If...else
if (judging condition) {
Statement 1
}else{
Statement 2
}
(2). If...else If....else
if (judging condition) {
Statement 1
}else if (judging condition) {
Statement 2
}else{
Statement 3
}
(3). If
if (judging condition) {
}
If...else If....else and If...else execute only one statement at a time, and then jump out of the statement after execution.
2. Looping statements
Loop statements are commonly used with do...while and for and while with for in.
All loop statements are executed until the condition is met, as long as the loop condition is met.
(1). Do...while statements
Grammar:
Do
{
Statement 1
}while (cyclic condition)
var v = 0;
var a = 0;
Do
{
A+=v;
v++;
}while (i<=100)
Note: If the Do loop is not added with v++, then this is a dead loop, so the do...while easily forms a dead loop.
(2). While statement
Grammar:
while (loop condition)
{
}
While statements are similar to Do...while, the only difference is that while executing the statement, the statement in the while loop executes at least 0 times and Do...while executes at least once.
(3). For statement
Grammar:
for (initial expression; loop condition; action expression)
{
Statement 1
}
var v = 0;
for (i=0;i>100;i++)
{
V+=i;
}
Note: The For statement is not easy to form a dead loop because it has an action expression.
(4). For in
Note: For in can only loop arrays and objects, and for in does not form a dead loop.
Grammar:
for (variable name in array)
{
Statement 1
}
JavaScript conditional statements and loop statements