


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流程控制语句的实例解析,希望对大家的学习有所帮助。

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.

JavaScript is widely used in websites, mobile applications, desktop applications and server-side programming. 1) In website development, JavaScript operates DOM together with HTML and CSS to achieve dynamic effects and supports frameworks such as jQuery and React. 2) Through ReactNative and Ionic, JavaScript is used to develop cross-platform mobile applications. 3) The Electron framework enables JavaScript to build desktop applications. 4) Node.js allows JavaScript to run on the server side and supports high concurrent requests.

Python is more suitable for data science and automation, while JavaScript is more suitable for front-end and full-stack development. 1. Python performs well in data science and machine learning, using libraries such as NumPy and Pandas for data processing and modeling. 2. Python is concise and efficient in automation and scripting. 3. JavaScript is indispensable in front-end development and is used to build dynamic web pages and single-page applications. 4. JavaScript plays a role in back-end development through Node.js and supports full-stack development.


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

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

Atom editor mac version download
The most popular open source editor
