Home >Web Front-end >Front-end Q&A >What statement structures does javascript have?

What statement structures does javascript have?

青灯夜游
青灯夜游Original
2021-11-04 16:06:442538browse

Javascript has three statement structures: 1. Sequential structure; top-down, line-by-line execution. 2. Select a branch structure; there are multiple paths, and according to different conditions, only execute one of them or selectively execute multiple paths. 3. Loop structure; execute certain codes repeatedly.

What statement structures does javascript have?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

Three major structures of JavaScript language

  • Sequential structure

    Top-down, line-by-line execution, first come first served, same statement, the following ones overwrite the previous

  • Select branch structure

    Multiple paths, according to different conditions, only one of them will be executed or selectively Execute multiple

  • Loop structure

    Repeatedly execute certain codes to replace certain repetitive operations, reduce code redundancy and improve efficiency

Select branch structure

1. Single branch structure

Choose one of the two paths, do it or not do it, Yes or no, executed or not executed;

(1) if judgment statement

//语法结构:
if(){}
    if	//语句名
    ()	//是否执行的条件,true 或 false
    {}	//执行的内容
//例:
if(true){
   console.log("hello");//"hello"---if括号中是true则输出hello,若为false则不输出
}

2. Double branch structure

There are two options, both of which must execute one

(1) if-else judgment statement

//当条件判断为true时,执行花括号内的语句,如果条件为假false,跳过花括号内的语句执行else里的内容
if(条件){        
    执行语句1,条件为真时执行   
}else{        
	执行语句2,条件为假时执行    
}

(2) Special circumstances in the if condition

1) Normal situation

Usually, a judgment statement is filled in the brackets of if to determine whether it is true or false. What is inside the if() brackets is usually called logic. point, judgment point When the content in the brackets is true, it is true, and when it is false, it is false. true or false is a Boolean value (boolean) representing true and false, which needs to be obtained through logical operators

##2) Special Situation
Let’s look at an example first

if(1){ //括号内填写数字"1"会发生什么
    console.log("会输出么?"); //会输出吗?---会在控制台输出内容
}

The number 1 is a numeric value, not a Boolean value, let alone true. Why is the statement within the curly braces executed?

In some specific circumstances, the computer will perform some implicit conversions. This is different from forced type conversion. It does not require you to do anything and will automatically convert the data type.

During operation, implicit type conversion will occur for the statement in if (parentheses)

//if隐式转换例题
var a = "1",b = 2;    
console.log(a + b);//12---+号进行字符串拼接,拼成12,而不是进行数值运算等于3

" "The sign has two meanings:

  • " "The sign is on both sides Number: This is the " " sign operator in mathematics. It adds two numbers and performs numerical operations

  • " " ". One or more characters appear on both sides of the sign: This is String splicing

and string splicing is given priority, so when it is detected that there are strings on the left and right sides of the number, the data on the other side that is not a string will be automatically converted into String, and then perform a string splicing operation (string splicing is to join the contents on both sides of the " " sign completely without any modification)

Note: In addition to the sign having two meanings, " The -*/%" sign has only mathematical meaning, so when performing "-*/%", the data types on both sides of the operator are implicitly converted to numeric types by default before performing the operation.

Then if you want to perform What should I do if I don't want to be implicitly converted into characters by the " " sign during addition operation?

Method:

var a = "1",b = 2;    
console.log(a + b);//12---默认进行字符串拼接输出字符串"12"
console.log((a-0) + b );//3---先通-0操作将字符转换为数字,再进行相加,输出数值3

3) Implicit type conversion in if
if() only requires Boolean values, then in In the if judgment, all data types within the brackets will be implicitly converted into Boolean types. Any type will eventually be converted into a Boolean type

  • Numbers: non-0 is true, 0 Is false

  •                                                                       use   with           ’         out out out out out out out through out out through out's'  through off ‐   ‐ ‐‐ ‐ ‐ , to be false

  •                                        .

                Array: true at any time, array is also a kind of object
  •                                                                                                                                          
  • #                                                                                                                                                                                                                                                                          .

  • #isNaN():
  • Determining whether it is NaN can indirectly determine whether it is a number. When it returns true, it is NaN and not a number. When it returns false, it is a number

  • 3. Multi-branch structure

  • There are at least two or more, and one or more of the statements are executed according to the conditions

    (1)多个if(){}else{}嵌套使用

    if(true){
        console.log("hello");
    }else{
        if(true){
            console.log("hello");
        }else{
            console.log("world");
        }
    }
    或者
    if(true){
        console.log("hello1");
    }else if(true){
        console.log("hello2");
    }else if(true){
        console.log("hello3");
    }else if(true){
        console.log("hello4");
    }

    (2)使用switch(){}语句

    系统提供的语句

    switch(){}

    • switch语句名

    • ()要判断的值

    • {}执行语句,所有的分支路径都放在一个花括号内

    • case匹配()内的值则执行,不匹配则往下找,找到执行,都没找到输出default的内容

    案例:输入数字,判断后输出星期几

    switch(n){
        case 1:console.log("星期一");break;
        case 2:console.log("星期二");break;
        case 3:console.log("星期三");break;
        case 4:console.log("星期四");break;
        case 5:console.log("星期五");break;
        case 6:console.log("星期六");break;
        case 7:console.log("星期七");break;
        default:console.log("请输入1-7之间的数字");
    }

    (3)switch的注意事项

    case的穿透特性:

            在一个switch中,只会进行一次case判断,如果判断成功,后面的case则不会判断全部执行
    阻止case穿透
    使用关键字break;可以跳出当前循环,后面的都不执行

    switch和if-else的区别

    •                 switch只能判断具体的值,不能判断范围,不会进行隐式转换

    •                 if else可以判断范围

    循环结构

    1.while循环

    while(){}
        while	//语句名
        ()		//执行条件、判断调价
        {}		//执行语句循环体
    //例:
    var i = 0;
    while(i<10){//当括号内的条件为真时,会一直执行
        console.log(i);//输出0-9,十个数字
        i++;//改变计数器
    }
    //表示重复执行10次
    //注意:为避免死循环,一定要在循环体内 改变 条件中使用变量的值-改变计数器。
    //通常while被用在不确定执行次数的循环中,循环体内设置if判断,满足条件使用break结束循环,否则一直循环

    2.do-while循环

    do{}while(){}
        do		语句名
        {}		do的执行语句
        while	语句名2
        ()		执行条件
        {}		while的执行语句
        
    do{
        console.log("do的执行语句");
        i++;//改变计数器
    }while(i<10){
        console.log("while的执行语句");
    }
    //条件为true时,会执行do后面的语句
    //条件为false时,会执行一次while后面的语句
    //注意:do-while的改变计数器要鞋子啊do语句中,否则会造成死循环
    do-while和while的区别
        do-while任何情况下都比while多执行一次(do-while无论真假都会执行一次while里的语句)
        do-while相对于while结构紧密些

    3.for循环

    for(){}
        for		语句名
        ()		条件组
        {}		循环体
    for(var i = 0; i < 10; i++){
        console.log(i);//打印0-9。共十个数字
    }
    
    for循环括号内的内容
    var i=0;	//定义循环开始时计数器的初始值
    i<10;		//设置停止循环的条件,满足条件执行循环,不满足条件结束循环
    i++		//计数器加一(这条语句是在循环体内容结束后才执行)
    //注意:上面的三个内容必须用分号“;”隔开,否则报错
    
    for循环是最长使用的循环,还可进行for循环嵌套
    for(var i=0;i<10;i++){
        for(var j=0;j<10;j++){
            console.log(i+j);
        } 
    }

    4.死循环

    无法靠自身控制结束的循环,称为死循环
    不知道要循环几次的问题,利用死循环的原理,每次判断一个条件,直到满足条件,利用break跳出循环
    通常使用while来进行死循环

    5.continue关键字break关键字

    continue和break都是用来控制循环结构的,主要是用来停止循环。   
    控制关键字:控制循环的执行或停止
        break:结束循环语句,直接跳出当前循环语句,后面所有的下一次循环都不执行。
        continue:表示跳过当前所在的本次循环(continue下面的语句不执行跳过),下一次循环还会正常执行

    【推荐学习:javascript高级教程

The above is the detailed content of What statement structures does javascript have?. 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
Previous article:Is JavaScript a language?Next article:Is JavaScript a language?