Javascript basi...LOGIN

Javascript basic tutorial flow control statement

What are the conditional statements?

if statement

if (condition){

Execution code;

}

Example:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>javascript</title>
</head>
<body>
	<script type="text/javascript">
		var x=5;
		if(x<4){
			alert('true');
		}
	</script>
</body>
</html>

Note: x is equal to 5. Execute the if statement. If x is less than 4, execute the following statement. In this code, x does not meet the conditions, so it will not Execute the following statement, the output is empty;

if...else

if (condition){

Execution code if the condition is met

}else{

Execution code if the condition is not met

}

Example:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>javascript</title>
</head>
<body>
	<script type="text/javascript">
		var x=5;
		if(x<4){
			alert('true');
		}else{
			alert('false');
		}
	</script>
</body>
</html>

Note: x =5 Determine whether the condition meets x<4 If the condition is true, execute alert('true'); Otherwise, execute alert('false');

if.... .else if....else

if(condition 1){

Execute statement 1

}else if(condition 2){

Execution statement 2

}else if(condition 3){

Execution statement 3

}else{

Does not meet the above Conditional execution statement 4

}

Example:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>控制流程语句   if....else if....else </title>
</head>
<script type="text/javascript">
		var age = 50;
		if(age<=30){
			document.write('青年');
		}else if(age<=40){
			document.write('中年');
		}else if(age<=60){
			document.write('中老年');
		}else{
			document.write('老年');
		}
</script>
<body>
	
</body>
</html>

switch statement

##switch (condition) {

case 1:Execute statement;break;

case 2:Execute statement;break;

case 3:Execute statement;break;

default :Execution statement;

}

Example:

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>流程控制语句  switch 语句  </title>
</head>
<script type="text/javascript">
	var myweek=1;
	switch(myweek){
		case 1:document.write('学习html');break;
		case 2:document.write('学习div+css');break;
		case 3:document.write('学习javascript');break;
		case 4:document.write('学习jquery');break;
		case 5:document.write('学习php');break;
		default:document.write('休息');
	}
</script>
<body>

</body>
</html>

Note: switch is used in combination with break. When myweek is equal to 1, if there is no break after the execution statement ; Will output all the cases from case 1 to default, plus break. When the conditions are found to be met, no further execution will occur. The switch must have an initial value. When all conditions are not met, the default statement will be executed


Next Section

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>javascript</title> </head> <body> <script type="text/javascript"> var x=5; if(x<4){ alert('true'); } </script> </body> </html>
submitReset Code
ChapterCourseware