Home >Web Front-end >JS Tutorial >How to perform process control in javascript (with code)

How to perform process control in javascript (with code)

不言
不言Original
2018-08-14 17:30:311200browse

The content of this article is about how JavaScript performs process control (with code). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Process control: refers to the execution sequence of the control code:
1. Sequential process control: compiler default
2. Select process control: if, switch
3. Loop process control : for, while, do...while

Sequential process: The code is executed from top to bottom according to the layout, and the code is executed sequentially according to the layout

Select process control (branch structure): Selective execution Code: Grammar rules:
if(condition) { //The result of the condition must be a Boolean value
Code snippet
}
If the result in () is true, the code inside {} will be executed. If the result is false, it will not be executed, but the code after the curly brackets will be executed.

            if(yushu == 0) {
                document.write(shu+"/ 2 结果为:" + jieguo);
            }            if(yushu != 0 ) {
                document.write("不能被整除");
            }

Multiple The code for if is very complex to write, so another double-selection syntax is provided:

        if(条件) {

        }else {

        }

If the if condition is true, the content of the first brace will be executed, otherwise the content of the brace after else will be executed.

In the actual development process, since there may be more than 2 conditions, another multi-selection syntax needs to be used,

                        if(条件1) {                            //代码块1
                        } else if(条件2) {                            //代码块2

            }else if(条件3) {                //代码块3
            }.....{

            }else {                //代码N
            }var gongzi = 15000;            
            if(gongzi <= 3500) {                
            document.write("不交税!");
            } else if(3500< gongzi && gongzi <= 5000){                
            var shui = (gongzi - 3500) * 0.03;                
            document.write("应该要交税:" + shui);
            } else if(5000< gongzi && gongzi <= 8000){                
            var shui1 = 1500 * 0.03;                
            var shui2 = (gongzi - 5000) * 0.1;                
            document.write("应该要交税:" + (shui1 + shui2));
            } else if(8000< gongzi && gongzi <= 12500){                
            var shui1 = 1500 * 0.03;                
            var shui2 = 3000 * 0.1;                
            var shui3 = (gongzi - 8000) * 0.2;                
            document.write("应该要交税:" + (shui1 + shui2 + shui3));

            } else if(12500< gongzi && gongzi <= 38500){                
            var shui1 = 1500 * 0.03;                
            var shui2 = 3000 * 0.1;                
            var shui3 = 4500 * 0.2;                
            var shui4 = (gongzi - 12500) * 0.25;                
            document.write("应该要交税:" + (shui1 + shui2 + shui3 + shui4));

            }else {                
            document.write("数字错误!");
            }

Another manifestation of multi-branching: switch

            switch(变量名) {                
            case 值1:
                    代码块;                    
                    break;                
                    case 值2:
                    代码块;                    
                    break;                
                    default:
                    代码块;                    
                    break;
            }var  shuzi = 3;            
            switch(shuzi) {                
            case 1:                    
            document.write("星期一");                    
            break;                
            case 2:                    
            document.write("星期二");                    
            break;                
            case 3:                    
            document.write("星期三");                    
            break; //结束,后面就不判断了                
            case 4:                    
            document.write("星期四");                    
            break;                
            case 5:                    
            document.write("星期五");                    
            break;                
            case 6:                    
            document.write("星期六");                    
            break;                
            case 7:                    
            document.write("星期天");                    
            break;                
            default:                    
            document.write("数字错误!");                    
            break;

            }

Loop process control: Loop is to repeatedly execute a certain piece of code,

    for(定义表达式; 判断表达式; 步长表达式) {
                代码块
            }

Related recommendations:

Organize Javascript process control statement study notes_javascript skills

Detailed explanation of JavaScript flow control statements_javascript skills

java study notes (introduction)_program flow control structures and methods

The above is the detailed content of How to perform process control in javascript (with code). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn