


Organize Javascript flow control statement study notes_javascript skills
1. Make a judgment (if statement)
The if statement is a statement used to execute the corresponding code based on the condition being met.
Syntax:
if(条件){ 条件成立时执行代码 }
Example: 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.
<script type="text/javascript"> var mycarrer = "HTML"; if (mycarrer == "HTML"){ document.write("你面试成功,欢迎加入公司。"); } </script>
2. Choose one of the two (if...else statement)
The if...else statement is the code that executes when the specified condition is true, and the code after else is executed when the condition is not true.
Grammar:
if(条件){ 条件成立时执行的代码 }else{ 条件不成立时执行的代码 }
Example: 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. Otherwise, you fail in the interview and cannot join the company.
<script type="text/javascript"> var mycarrer = "HTML"; //mycarrer变量存储技能 if (mycarrer == "HTML"){ document.write("你面试成功,欢迎加入公司。"); }else{ //否则,技能不是HTML document.write("你面试不成功,不能加入公司。"); } </script>
3. Multiple judgments (if...else nested statements)
To select one group of multiple groups of statements to execute, use if..else nested statements.
Syntax:
if(条件1) { 条件1成立时执行的代码} else if(条件2) { 条件2成立时执行的代码} ... else if(条件n) { 条件n成立时执行的代码} else { 条件1、2至n不成立时执行的代码}
Example: According to the age classification standards of the United Nations World Health Organization, those under 44 years old are young people; those between 45 and 59 years old are middle-aged people. The elderly are those between 60 and 89 years old; the longevity elderly are those over 90 years old. Zhao Hong is 99 years old this year. Which age group does she belong to?
<script type="text/JavaScript"> var myage =99;//赵红的年龄为99 if(myage<=44){ document.write("青年"); }else if(myage<=59) { document.write("中年人"); }else if (myage<=89){ document.write("老年人"); }else { document.write("长寿老年人"); } </script>
4. Multiple choices (Switch statement)
When there are many options, switch is more convenient to use than if else.
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 matches each case value. 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, the statement after default is executed.
Example: Let’s make a weekly plan, study concepts and knowledge on Monday and Tuesday, practice in the company on Wednesday and Thursday, summarize experience on Friday, rest and have fun on Saturday and Sunday.
<script type="text/JavaScript"> var myweek =3;//myweek表示星期几变量 switch(myweek){ case 1: case 2: document.write("学习理念知识"); break; case 3: case 4: document.write("到企业实践"); break; case 5: document.write("总结经验"); break; default: document.write("周六、日休息和娱乐"); } </script>
5. Repeat (for loop)
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. Loop statements are to repeatedly execute a piece of code.
for statement structure:
for(初始化变量;循环条件;循环迭代) { 循环语句 }
Example: If there are 6 balls in a box, we take one at a time and repeatedly take out the balls from the box until all the balls are taken.
<script type="text/javascript"> var num=1; for (num=1;num<=6;num++){ //初始化值;循环条件;循环后条件值更新 document.write("取出第"+num+"个球<br />"); } </script>
We have money of different denominations 1, 2, 3...10. Use the for statement to complete the total and see how much money we have in total?
<script type="text/JavaScript"> var mymoney,sum=0;//mymoney变量存放不同面值,sum总计 for(mymoney=1;mymoney<=10;mymoney++){ sum= sum + mymoney; } document.write("sum合计:"+sum); </script>
6. Repeatedly (while loop)
The while loop has the same function as the for loop. The while loop repeatedly executes a section of code until a certain condition is no longer met.
while statement structure:
while(判断条件) { 循环语句 }
Use a while loop to complete the action of taking balls from the box, one at a time, for a total of 6 balls.
<script type="text/javascript"> var num=0; //初始化值 while (num<=6){ //条件判断 document.write("取出第"+num+"个球<br />"); num=num+1; //条件值更新 } </script>
7. Back and forth (Do...while loop)
The basic principle of the do while structure is basically the same as that of the while structure, but it ensures 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...while statement structure:
do { 循环语句 } while(判断条件)
Try to output 5 numbers.
<script type="text/javascript"> num= 1; do{ document.write("数值为:" + num+"<br />"); num++; //更新条件 } while (num<=5) </script>
Use do...while statement to output 6 numbers.
<script type="text/javascript"> var mynum =6;//mynum初值化数值为6 do{ document.write("数字:"+mynum+"<br/>"); mynum=mynum-1; } while(mynum>=1); </script>
8. Exit the loop break
Use the break statement in while, for, do...while, while loops to exit the current loop and directly execute the following code.
The format is as follows:
for(初始条件;判断条件;循环后条件值更新){ if(特殊情况) {break;} 循环代码 }
Output the test scores. If the score is passed, continue to output the next score. If the score is failed, exit and subsequent scores will not be output.
<script type="text/JavaScript"> var mynum =new Array(70,80,66,90,50,100,89);//定义数组mynum并赋值 var i=0; while(i<mynum.length){ if(mynum[i]<60){ document.write("成绩"+mynum[i]+"不及格,不用循环了"+"<br>"); break; } document.write("成绩:"+mynum[i]+"及格,继续循环"+"<br>"); i=i+1; } </script>
9. Continue the loop
Statement structure:
for(初始条件;判断条件;循环后条件值更新){ if(特殊情况){ continue; } 循环代码 }
In the above loop, when a special situation occurs, this loop will be skipped, and subsequent loops will not be affected.
Example: Output test scores. If the score is passed, the next score will be output. If the score is failed, the score will not be output.
<script type="text/JavaScript"> var mynum =new Array(70,80,66,90,50,100,89);//定义数组mynum并赋值 var i; for(i=0;i<mynum.length;i++){ if(mynum[i]<60){ document.write("成绩不及格,不输出!"+"<br>"); continue; } document.write("成绩:"+mynum[i]+"及格,输出!"+"<br>"); } </script>
在一个大学的编程选修课班里,我们得到了一组参加该班级的学生数据,分别是姓名、性别、年龄和年级,接下来呢,我们要利用JavaScript的知识挑出其中所有是大一的女生的的名字哦。
学生信息如下:
('小A','女',21,'大一'), ('小B','男',23,'大三'),
('小C','男',24,'大四'), ('小D','女',21,'大一'),
('小E','女',22,'大四'), ('小F','男',21,'大一'),
('小G','女',22,'大二'), ('小H','女',20,'大三'),
('小I','女',20,'大一'), ('小J','男',20,'大三')
<script type="text/javascript"> //第一步把之前的数据写成一个数组的形式,定义变量为 infos var infos = [ ['小A','女',21,'大一'], ['小B','男',23,'大三'], ['小C','男',24,'大四'], ['小D','女',21,'大一'], ['小E','女',22,'大四'], ['小F','男',21,'大一'], ['小G','女',22,'大二'], ['小H','女',20,'大三'], ['小I','女',20,'大一'], ['小J','男',20,'大三'] ]; //第一次筛选,找出都是大一的信息 var arr1 = []; var n = 0; for(var i=0;i<infos.length;i++){ if( infos[i][3] == "大一" ){ arr1[n] = infos[i]; document.write(arr1[n]+"<br/>"); n=n+1; } } document.write("大一人数: "+arr1.length+"<br/>"); //第二次筛选,找出都是女生的信息 for(var i=0;i<arr1.length;i++){ //这里可以用switch if(arr1[i][1]=='女'){ document.write(arr1[i][0]+"<br/>"); } } </script>
以上就是关于Javascript流程控制语句的实例解析,希望对大家的学习有所帮助。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
