JavaScript flow control statements and commonly used special statement

1. flow control statements

(. 1) determines if
the syntax of the form if block:

<script type="text/javascript">
	if(//条件1){
		//条件1成立,要执行的语句块;
	}else if(//条件2){
		//条件2成立,要执行的语句块;
	}else{
		//条件1、条件2都不成立,要执行的语句块;
	}
	//其中只要有一条条件成立 则不会执行以后的语句块
</script>

(2) determination switch
syntax of the switch statement is as follows:

<script type="text/javascript">
	switch(//表达式){
	case /*值1*/:/*执行的语句块*/;break;
	case /*值2*/:/*执行的语句块*/;break;
	case /*值3*/:/*执行的语句块*/;break;
	default:/*执行的语句块*/
	}
	//default相当于else;break表示终止,否则会一直执行到switch结尾
</script>

(. 3) while circulating through the bad bad and do while
while circulating bad syntax is as follows:

<script type="text/javascript">
	while(//循环条件){
		//循环体语句块;
	}
	//先判断循环条件,再进行循环体语句;即循环体条件不成立,循环体语句不会执行
</script>

do while circulating bad syntax is as follows:

<script type="text/javascript">
	do{
		//循环体语句块;
	}while(//循环条件)
	//先进行循环体语句,再判断循环条件;即循环体条件不成立,循环体语句也会执行一次;
</script>

Difference between the two: the while execution after the first judgment, a loop statement may not perform, do while after the implementation of the first judgment, the loop statement is executed at least once.
(. 4) for circulating and for in through the bad bad
for circulating bad syntax is as follows:

<script type="text/javascript">
	for(//表达式1;表达式2;表达式3){
		//循环体语句块;
	}
	//表达式1用于初始化 var i = 0;
	//表达式2用于循环条件 i<10;
	//表达式1用于更新循环体变量 i++;
</script>

Note: In the case where the determined cycles generally used for loop. Compared while and do while syntax is more concise.
(. 5) for in-circulating
for in-circulating the syntax is as follows:

<script type="text/javascript">
	for(//变量 in 对象){
		//循环体语句块;
	}
</script>

Role: 1. through all the array elements in the array. 2. traverse all the properties of the JavaScript object.
Example:

<script type="text/javascript">
	var arr = new Array();
	arr[0]=1;
	arr[2]=2;
	arr[3]="abc";
	arr[4]=true;
	for(var index in arr){
		document.write(arr[index]+"</br>")
	}
	for(var properties in navigator){
		document.write("属性:"+properties+",属性值:"+navigator[properties]+"</br>")
	}
</script>
JavaScript provides a break and continue to change the flow of control loop

break usage:

<script type="text/javascript">
	var i = 0;
	for(i=0;i<=5;i++){
		if(i==4){
			break;//终止循环
		}
		document.write("这个数字是:"+i+"</br>");
	}

	这个数字是:0
	这个数字是:1
	这个数字是:2
	这个数字是:3
</script>

continue usage:

<script type="text/javascript">
	var i = 0;
	for(i=0;i<=5;i++){
		if(i==4){
			continue;//结束本次循环;进行下一次循环
		}
		document.write("这个数字是:"+i+"</br>");
	}

	这个数字是:0
	这个数字是:1
	这个数字是:2
	这个数字是:3
	这个数字是:5
</script>

Summary: BREAK direct terminate the loop, continue the end of this cycle directly into the next cycle.

2. Common special statement

Sentence is the basic unit of execution of JavaScript, are each statement ends with a semicolon, the statement in addition to assignments, operators, statements and other statements, there are some commonly used special statement. As follows:
1. block of statements
is actually wrapped with {} js code only some, of course, can not be independent scope statement block.

<script type="text/javascript">
{ // 语句块
    标签1: function test1(){return "hehe"};
    标签2: var test2 = function(){return "hehe"};
}
</script>

Independent scoping wording

<script type="text/javascript">
	(function () {
	    // 功能块代码...
	})();
	//自调用函数,不仅可以独立作用域,还可以在 UglifyJS,Closure Compressor 等工具编译的时候更加优化。
</script>

2. Empty statement
in JavaScript, when it is desired to be as a plurality of statements using statement, instead of using the compound statement. Empty statement (empty statement) is just the opposite, it allows the statement contains 0;
no action is executed when empty statement JavaScript interpreter. However, when creating a loop having loop empty, empty statement is useful
in the following cycle, all the operations in the expression a [i ++] = 0 completed, any loop is not necessary here. However, the need JavaScript loop body comprises at least one statement, therefore, where only one represents a semicolon to separate empty statement

//初始化一个数组a
for(i = 0; i < a.length; a[i++] = 0);
//在for、while循环或if语句的右圆括号后的分号很不起眼,这可能造成一些bug,而这些bug很难定位到
//因为;的多余,造成与预想不同的结果
if((a == 0) || (b == 0));
0 = null;
//如果有特殊目的需要使用空语句,最好在代码中添加注释,这样可以更清楚地说明这条空语句是有用的
for(i = 0; i < a.length; a[i++] = 0)/*empty*/;

3. exception is thrown statement
JavaScript has built-in error object provides error messages when an error occurs. error object provides two useful properties: name and message.

<script type="text/javascript">
{
	for(var i=0;i<10;i++){
		document.write("这个数字是:"+i+"</br>");
		if(i==4){
			throw new Error("报错啦");
		}	
	}
}
</script>

4. Abnormal catch statement
of a try statement enables you to test a block of code for errors.
catch statement allows you to handle errors. Can only be used once
finally statement allows you to execute code after the try and catch, regardless of the result of an error.

<script type="text/javascript">
try{
	var age=5;
	if(age==5){ 
		throw new Error("年龄太小啦")
	}
}catch(e){//e是异常的封装对象
	document.write("出错:"+e.message);
}finally{
	document.write("总会执行的finally块");
}
</script>

5.with statement
with statement for setting a code for a specific object in scope.

<script type="text/javascript">
with(document) {
  write("输出第一行数据</br>");
  write("输出第二行数据</br>");
  write("输出第三行数据</br>");
}
//等于
  document.write("输出第一行数据</br>");
  document.write("输出第二行数据</br>");
  document.write("输出第三行数据</br>");
</script>

Tip: with the statement to be slow to run code blocks, particularly in the property value has been set. In most cases, if possible, it is best to avoid using it.

Guess you like

Origin blog.csdn.net/zhangjx1213/article/details/95348774