Javascript process control

Keywords: Javascript

Process control

JavaScript executes program flow through process statement, which consists of several statements. Under normal circumstances, statements in a program are executed in writing order, which is called sequential structure. In addition to sequential structure, there are alternative structure and cyclic structure.

 

1. Selection structure

(1) if-else statement

Syntax:

if(condition){

  statementS;

}else{

  statements;

}

 

(2)if-else if-else statement

Syntax:

if(condition){

  statementS;

}else if{

  statements;

}

……

else{

  statements;

}

For example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="GB2312">
    <title>Selection structure</title>
    <script language="JavaScript">
        function  max() {
            var fist=parseInt(form1.fist.value);
            var secod=parseInt(form1.secod.value);
            if(isNaN(fist)){
                alert("The first number is not a numerical type");
                form1.fist.value="";

            }
            else if (isNaN(secod)) {
                alert("The second number is not a numerical type");
                form1.secod.value="";
            }else{
                var max=(fist>=secod?fist:secod);
                document.write("The larger number between the two numbers is:"+max);
            }

        }
    </script>

</head>
<body>
<form name="form1">
    //Please enter the first number (numerical type):
    <input type="text" name="fist" />
    <br>
    //Please enter the second number (numerical type):
    <input type="text" name="secod" />
    <br>
    <input type="button" ONCLICK="max()" value="Choose a larger value" />&nbsp;&nbsp;
    <input type="reset" value="Refill" />
</form>
</body>
</html>

(3) switch statement ([] is optional)

Syntax:

switch(expression){

  case value1:

    statement;

    break;

  case value2:

    statement2;

    break;

  ……

  case valueN;

    statementN;

    break;

  [defalut:

    defalutStatements;]

}

For example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="GB2312">
    <title>switch Sentence</title>
</head>
<body>
<script language="JavaScript">
    var day= new Date().getDay();
    switch (day){
        case 0:
            title="Today is Sunday.";
            break;
        case 1:
            title="Today is Monday"
            break;
        case 2:
            title="Today is the second term of exercise.";
            break;
        case 3:
            title="Today is Wednesday";
            break;
        case 4:
            title="Today is Thursday";
            break;
        case 5:
            title="Today is Friday";
            break;
        case 6:
            title="Today is Saturday.";
            break;
    }
    document.write(title);
</script>

</body>
</html>

 

 

2. Loop statement

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

Syntax:

for(inintal-condition;conditin;increment){

  statements;

}

Example:

Output Nine-Nine Multiplier Table

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Cyclic structure</title>
</head>

<body>
<font size="-1" color="blue">
    <script language="JavaScript">
        var i,j;
        for(i=1;i<=10;i++){
            for(j=1;j<=i;j++){
                document.write(j+"*"+i+"="+i*j);
                document.write("&nbsp;&nbsp;");
            }
        document.write("<br>");
        }
    </script>
</font>


</body>
</html>

 

(2) for-in statements (usually used to traverse arrays)

Syntax:

for(elements in object){

  statement;

}

For example:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="GB2312">
    <title>for-in</title>
</head>
<body>
<script language="JavaScript">
    var student=new Object();
    student.name="Ming Ming Wang";
    student.no="20120156";
    student.addreess="Jinan";
    for(e in student){
        document.write(e+":"+student[e]+"<br>");
    }
</script>
</body>
</html>

(3) while statement (number of unknown loops)

Syntax:

while(condition){

  statement;

}

For example:

Multiple of 3 in output 1-100

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="GB2312">
    <title>while loop</title>
</head>
<body>
<script language="JavaScript">
    var i=1;
    while(i<100){
        if(i%3==0){
            document.write(i+"&nbsp");
        }
        i++;
    }
</script>

</body>
</html>

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

Syntax:

do{

  statement;

}

while(conditions);

For example:

Calculate the sum of 1-100

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <meta charset="GB2312">
    <title>do-while Sentence</title>
</head>
<body>
<script language="JavaScript">
    var i = 1;
    var num = 0;
    do {
        sum+=i;
        i++
    }while(i<=100);
    document.write("1-100 The sum is:"+sum);
</script>
</body>
</html>

 

 

Transfer statement

(1) break statement

The break statement is used in loop statements such as switch statement and for statement. The statement 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, for-in statements to end this cycle and execute the next cycle, usually with the if statement.

(3) return statement

The return statement is generally used in functions. The return statement expression can be used to return any type of function value, and the return function value can be accepted by variables.

 

 

 

I hereby declare that if you need to reproduce, please indicate the source, if you have any questions, please put forward in time for correction, if there is any infringement, contact to delete, thank you.

Posted by DesertFox07 on Mon, 28 Jan 2019 21:51:14 -0800