JavaScript if ... Else statements, switch statements, For loops, while loops, Break and Continue statements

1, JavaScript if ... Else statement

1.1, if the statement

Only when a specified condition is true, the statement will execute code.
Syntax
IF (for condition Condition)
{
code is executed when the condition is true
}

Here Insert Picture Description

1.2, if ... else statement

Please use the if ... else statement to execute code in the condition is true, execute other code is false in conditions.
Syntax
IF (for condition Condition)
{
code is executed when the condition is true
}
the else
{
if the condition is not executed when the code is true
}
Here Insert Picture Description

1.3、if…else if…else 语句

Use if ... else if ... else statement to select one of the plurality of code blocks is performed.
Syntax
IF (condition1)
{
if Condition 1 is executed when the code to true
}
the else IF (condition2 The)
{
if condition 2 is executed when the code to true
}
the else
{
if conditions 1 and 2 are not executed when the code to true
}
Here Insert Picture Description

2, JavaScript switch statement

switch statement used to perform different actions based on different conditions.

2.1 Examples

<body>

<p>点击下面的按钮来显示今天是周几:</p>
<button onclick="myFunction()">点击这里</button>
<p id="demo"></p>
<script>
function myFunction(){
	var x;
	var d=new Date().getDay();
	switch (d){
  		case 0:x="今天是星期日";
    	break;
 		case 1:x="今天是星期一";
        break;
  		case 2:x="今天是星期二";
        break;
        case 3:x="今天是星期三";
   	 	break;
  		case 4:x="今天是星期四";
    	break;
  		case 5:x="今天是星期五";
        break;
  		case 6:x="今天是星期六";
    	break;
 	}
	document.getElementById("demo").innerHTML=x;
}
</script>

</body>

2.2, default keyword

Please use the default keyword required to do when there is no matching things:
examples

If today is not Saturday or Sunday, the default message is output:

var d=new Date().getDay();
switch (d)
{
    case 6:x="今天是星期六";
    break;
    case 0:x="今天是星期日";
    break;
    default:
    x="期待周末";
}
document.getElementById("demo").innerHTML=x;

3, JavaScript for loop

3.1, JavaScript cycle

Here Insert Picture Description##

3.2, For loop

Here Insert Picture Description

3.3, sentence 3

Here Insert Picture Description

3.4, For / In cycle

Here Insert Picture Description

4, JavaScript while loop

As long as a specified condition is true, the loop can always execute the code block.

4.1, while circulation

It is true while loop will execute the code block cyclic specified conditions.
Here Insert Picture Description

4.2, do / while loop

do / while loop is a variant of the while loop. The cycle will check whether the condition is true executed before a block of code, and if the condition is true, it will repeat the cycle.
Here Insert Picture Description

5, JavaScript Break and Continue statements

5.1, Break statement

We have seen the break statement in the previous section of this tutorial. It breaks out of switch () statement.

break statement can be used to break the loop.

After the break statement out of the loop will continue to execute code (if any) after the cycle:
Here Insert Picture Description

5.2, Continue statement

continue statement interruption iterative loop, and if the specified condition occurs, and then continue to the next iteration of the loop. The skip value example 3:
Here Insert Picture Description

5.3, JavaScript tags

To mark JavaScript statements, add a colon before the statement:
label:
statements
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43731793/article/details/92384089