Home  >  Article  >  Web Front-end  >  What is flow control in Javascript (code example)

What is flow control in Javascript (code example)

青灯夜游
青灯夜游forward
2018-10-20 16:34:402203browse

This article brings you an introduction to what process control is in Javascript (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Process control

JavaScript executes the program flow through process statements, and the program flow consists of several statements. Under normal circumstances, the statements in the program are executed in the order they are written. This structure is called a sequential structure. In addition to sequential structures there are selection structures and loop structures.

1. Selection structure

(1) if-else statement

Syntax:

if(condition){
  statementS;
}else{
  statements;
}

(2) if-else if-else statement

Grammar:

if(condition){
  statementS;
}else if{
  statements;
}
……
else{
  statements;
}

For example:

200ba6c2aed0a55a2e6eb1f53ccfa057100db36a723c770d327fc0aef2ce13b193f0f5c25f18dab9d176bd4f6de5d30e
    5dd619b3daa8098d81923634075d5aa9
    b2386ffb911b14667cb8f0f91ea547a7选择结构6e916e0f7d1e588d4f442bf645aedb2f
    c9b2223153d8e39163b0aa064e06fe9e
        function  max() {            
               var fist=parseInt(form1.fist.value);            
               var secod=parseInt(form1.secod.value);            
               if(isNaN(fist)){
                alert("第一个数不是数值类型");
                form1.fist.value="";

            }            else if (isNaN(secod)) {
                alert("第二个数不是数值类型");
                form1.secod.value="";
            }else{                
                    var max=(fist>=secod?fist:secod);
                document.write("两个数之间较大的数为:"+max);
            }

        }    
    2cacc6d41bbb37262a98f745aa00fbf0
 9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
 e8123d645f3ed9aaa22bb5c917b2ac04
    请输入第一个数(数值型):    8d769f98bd6c240628a9ad623ab1ef9a
    0c6dc11e160d3b678d68754cc175188a
    请输入第二个数(数值型):    aeb337a347f95a6cac33731d207fded9
    0c6dc11e160d3b678d68754cc175188a
    07e92316e492652a6160af3bc9f0417a  
    946ddf758becc4ea3151ff3dec6d001e
 f5a47148e367a6035fd7a2faa965022e
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

(3) switch statement (optional in [ ])

Syntax:

switch(expression){
  case value1:
    statement;
    break;
  case value2:
    statement2;
    break;
  ……
  case valueN;
    statementN;
    break;
  [defalut:
    defalutStatements;]
}

For example:


2. Loop statement

(1) for loop (a loop with a known number of loops)

Syntax:

for(inintal-condition;conditin;increment){
  statements;
}

Example:

Output the multiplication table

671f1587257673e3ab28a461c7daaf2f
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
    b2386ffb911b14667cb8f0f91ea547a7循环结构6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
  b525c03511ddfa0129a1894a0aafa571
    c9b2223153d8e39163b0aa064e06fe9e
        var i,j;        
        for(i=1;i<=10;i++){            
                for(j=1;j<=i;j++){
                document.write(j+"*"+i+"="+i*j);
                document.write("  ");
            }
        document.write("0c6dc11e160d3b678d68754cc175188a");
        }    
    2cacc6d41bbb37262a98f745aa00fbf0
  e6e38b3c62e8df885fe2e3986461aa63
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

(2) for-in statement (usually used Traverse the array)

Grammar:

for(elements in object){
  statement;
}

For example:

671f1587257673e3ab28a461c7daaf2f 
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
    5dd619b3daa8098d81923634075d5aa9
    b2386ffb911b14667cb8f0f91ea547a7for-in6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
 c9b2223153d8e39163b0aa064e06fe9e
    var student=new Object();
    student.name="王明明";
    student.no="20120156";
    student.addreess="山东济南";    
    for(e in student){
        document.write(e+":"+student[e]+"0c6dc11e160d3b678d68754cc175188a");
    }
 2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

(3) while statement (unknown number of loops)

Grammar:

while(condition){
  statement;
}

For example:

Output multiples of 3 in 1-100

671f1587257673e3ab28a461c7daaf2f
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
    5dd619b3daa8098d81923634075d5aa9
    b2386ffb911b14667cb8f0f91ea547a7while循环6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
     c9b2223153d8e39163b0aa064e06fe9e
      var i=1;    
      while(i<100){        
           if(i%3==0){
            document.write(i+" ");
        }
        i++;
      }
   2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

(4) do-while statement (this statement is executed at least once)

Syntax :

do{
  statement;
}
while(conditions);

For example:

Calculate the sum of 1-100

671f1587257673e3ab28a461c7daaf2f 
100db36a723c770d327fc0aef2ce13b1
93f0f5c25f18dab9d176bd4f6de5d30e
    5dd619b3daa8098d81923634075d5aa9
    b2386ffb911b14667cb8f0f91ea547a7do-while语句6e916e0f7d1e588d4f442bf645aedb2f
9c3bca370b5104690d9ef395f2c5f8d1
6c04bd5ca3fcae76e30b72ad730ca86d
  c9b2223153d8e39163b0aa064e06fe9e
    var i = 1;    
    var num = 0;    
    do {
        sum+=i;
        i++
    }while(i<=100);
    document.write("1-100的和是:"+sum);
  2cacc6d41bbb37262a98f745aa00fbf0
36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

Transfer statement

(1) break statement

The break statement is used in loop statements such as switch statements and for statements. . Used to terminate the switch statement and execute the statement after the switch statement.

(2) continue statement

The continue statement is used in for, while, do-while, and for-in statements to end this loop and execute the next loop. It is generally used with if statements are used together.

(3) return statement

The return statement is generally used in functions. You can use the return statement expression format to return any type of function value. The returned function value can be accepted by a variable.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit JavaScript Video Tutorial, jQuery Video Tutorial, bootstrap Tutorial!

The above is the detailed content of What is flow control in Javascript (code example). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete