Flow control statements include: 1. if statement; 2. "if...else" statement; 3. "if..else if.." nested statement; 4. "Switch..case" statement; 5. for statement; 6. while statement; 7. "do...while" statement; 8. "for..in", etc.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
The if statement is a statement used to execute the corresponding code based on the condition being established.
Grammar:
if(条件) { 条件成立时执行代码 }
Note: If lowercase, uppercase letters (IF) will cause errors!
Suppose you apply for a web front-end technology development position. If you know HTML technology and you succeed in the interview, you are welcome to join the company. The code is expressed as follows:
<script type="text/javascript"> var mycarrer = "HTML"; if (mycarrer == "HTML") { document.write("你面试成功,欢迎加入公司。"); } </script>
The if...else statement is to execute the code when the specified condition is true, and when the condition is not true Code after executing else.
Grammar:
if(条件) { 条件成立时执行的代码} else {条件不成立时执行的代码}
Suppose you apply for a web front-end technology development position. If you know HTML technology and your interview is successful, you are welcome to join the company. Otherwise, your interview will be unsuccessful and you will not be able to join the company.
The code is expressed as follows:
<script type="text/javascript"> var mycarrer = "HTML"; //mycarrer变量存储技能 if (mycarrer == "HTML") { document.write("你面试成功,欢迎加入公司。"); } else //否则,技能不是HTML { document.write("你面试不成功,不能加入公司。");} </script>
To select one group of multiple groups of statements to execute, use if..else if..nested statements.
Grammar:
if(条件1) { 条件1成立时执行的代码} else if(条件2) { 条件2成立时执行的代码} ... else if(条件n) { 条件n成立时执行的代码} else { 条件1、2至n不成立时执行的代码}
When there are many options When using switch, switch is more convenient than if else.
Grammar:
switch(表达式){case值1: 执行代码块 1 break;case值2: 执行代码块 2 break;...case值n: 执行代码块 n break;default: 与 case值1 、 case值2...case值n 不同时执行的代码 }
Grammar description:
Switch must be assigned an initial value, and the value is the same as each case value matches.
Satisfy all statements after executing the case, and use the break statement to prevent the next case from running.
If all case values do not match, execute the statement after default.
Assuming that students' test scores are evaluated on a 10-point full-score system, we grade the scores according to each grade and make different evaluations based on the grade of the scores.
Note: Remember to add a break statement after the statement executed by the case. Otherwise, just continue to execute the statement in the case below. Look at the following code:
Many things are not just done once, but done repeatedly. For example, print 10 copies of the test paper, one at a time, and repeat this action until the printing is completed. We use loop statements to accomplish these things. A loop statement is to repeatedly execute a piece of code.
forStatement structure:
for(初始化变量;循环条件;循环迭代) { 循环语句 }
If there are 6 balls in a box, we take one ball at a time and repeatedly take it out from the box ball until all the balls are taken.
<script type="text/javascript"> var num=1; for (num=1;num<=6;num++) //初始化值;循环条件;循环后条件值更新 { document.write("取出第"+num+"个球<br />"); } </script>
The while loop has the same function as the for loop. The while loop repeatedly executes a piece of code until a certain condition is no longer met.
whileStatement structure:
while(判断条件){ 循环语句 }
Use a while loop to complete the action of taking the ball from the box, one at a time, a total of 6 ball.
<script type="text/javascript"> var num=0; //初始化值 while (num<=6) //条件判断 { document.write("取出第"+num+"个球<br />"); num=num+1; //条件值更新 } </script>
The basic principle of the do while structure is basically the same as the while structure, but it guarantees that the loop body is executed at least once. Because it executes the code first, then judges the condition. If the condition is true, the loop continues.
do...whileStatement structure:
do{ 循环语句 }while(判断条件)
We try to output 5 numbers.
<script type="text/javascript"> num= 1; do { document.write("数值为:" + num+"<br />"); num++; //更新条件 } while (num<=5) </script>
The for in loop is a special type of loop and a variant of the ordinary for loop, mainly used to traverse Object, which can be used to cycle out the attributes in the object in sequence. The syntax format is as follows:
for (variable in object) { // 要执行的代码 }
Among them, variable is a variable, which will be assigned a different value each time it is looped. We can { } Use this variable to perform a series of operations; object is the object to be traversed. In each loop, the key of an attribute in the object object will be assigned to the variable variable until all attributes in the object have been traversed.
JS for in 循环示例代码:
// 定义一个对象 var person = {"name": "Clark", "surname": "Kent", "age": "36"}; // 遍历对象中的所有属性 for(var prop in person) { document.write("<p>" + prop + " = " + person[prop] + "</p>"); }
运行结果:
name = Clark surname = Kent age = 36
for of 循环是 ECMAScript6 中新添加的一个循环方式,与 for in 循环类似,也是普通 for 循环的一种变体。使用 for of 循环可以轻松的遍历数组或者其它可遍历的对象,例如字符串、对象等。
JS for of 循环的语法格式如下:
for (variable of iterable) { // 要执行的代码 }
其中,variable 为一个变量,每次循环时这个变量都会被赋予不同的值,我们可以在后面的{ }中使用这个变量来进行一系列操作;iterable 为要遍历的内容,在每次循环中,会将 iterable 中的一个值赋值给变量 variable,直到 iterable 中的所有值都遍历完。
示例代码如下:
// 定义一个数组 var arr = ['a', 'b', 'c', 'd', 'e', 'f']; // 使用 for of 循环遍历数组中的每个元素 for (var value of arr) { document.write(value + ", "); } document.write("<br>"); // 定义一个字符串 var str = "Hello World!"; // 使用 for of 循环遍历字符串中的每个字符 for (var value of str) { document.write(value + ", "); } document.write("<br>"); // 定义一个对象 var obj = {"name": "Clark", "surname": "Kent", "age": "36"}; // 使用 for of 循环遍历对象中的所有属性 for(var value in obj) { document.write(value + ", "); }
运行结果:
a, b, c, d, e, f, H, e, l, l, o, , W, o, r, l, d, !, name, surname, age,
在while、for、do...while、while循环中使用break语句退出当前循环,直接执行后面的代码。
格式如下:
for(初始条件;判断条件;循环后条件值更新) { if(特殊情况) {break;} 循环代码 }
当遇到特殊情况的时候,循环就会立即结束。看看下面的例子,输出10个数,如果数值为5,就停止输出。
continue的作用是仅仅跳过本次循环,而整个循环体继续执行。
语句结构:
for(初始条件;判断条件;循环后条件值更新) { if(特殊情况) { continue; } 循环代码 }
上面的循环中,当特殊情况发生的时候,本次循环将被跳过,而后续的循环则不会受到影响。
【推荐学习:javascript高级教程】
The above is the detailed content of What are the statements for javascript flow control?. For more information, please follow other related articles on the PHP Chinese website!