JavaScript syntax explained: If statement &for loops & Functions

Source: Internet
Author: User
Tags naming convention pow

This article starts with the blog Park and continues to update the front-end series on GitHub. Follow me on GitHub, get started and go to the advanced front.

The following is the text.

If statement of the most basic if statement

Structure of the IF statement: (format)

    if{        // 条件为真时,做的事情    }else{        // 条件为假时,做的事情    }

If statements also become "SELECT statement", "conditional judgment statement", we use the following image to explain.

Multi-Branch If statement

Format:

    if (条件表达式1{        // 条件1为真时,做的事情    }elseif (条件表达式2{        // 条件1不满足,条件2满足时,做的事情    }elseif (条件表达式3{        // 条件1、2不满足,条件3满足时,做的事情    }else{        // 条件1、2、3都不满足时,做的事情    }

In all of the above statement bodies, only one is executed.

Do a topic:

    根据BMI(身体质量指数)显示一个人的体型。    BMI指数,就是体重、身高的一个计算公式。公式是:    BMI =体重÷身高的平方    比如,老师的体重是81.6公斤,身高是1.71米。    那么老师的BMI就是  81.6 ÷ 1.712     等于 27.906022365856163    过轻:低于18.5    正常:18.5-24.99999999    过重:25-27.9999999    肥胖:28-32    非常肥胖, 高于32    用JavaScript开发一个程序,让用户先输入自己的体重,然后输入自己的身高(弹出两次prompt框)。    计算它的BMI,根据上表,弹出用户的身体情况。比如“过轻” 、 “正常” 、“过重” 、 “肥胖” 、“非常肥胖”。

Answer:

Notation 1:

        //First step, enter height and weight        varHeight= parsefloat(prompt("Please enter the height, the unit is the meter"));        varWeight= parsefloat(prompt("Please enter weight, unit is kg"));        ///Step two, calculate the BMI index        varBmi=Weight/Math.POW(Height, 2);        //Third step, if statement to judge. Notice the phenomenon of jumping        if(BMI< 18.5){            Alert("Thin");        } Else if(BMI<  -){            Alert("Normal");        } Else if(BMI<  -){            Alert("overweight");        } Else if(BMI<=  +){            Alert("Obesity");        } Else {            Alert("very fat.");        }

Notation 2:

        //First step, enter height and weight        varHeight= parsefloat(prompt("Please enter the height, the unit is the meter"));        varWeight= parsefloat(prompt("Please enter weight, unit is kg"));        ///Step two, calculate the BMI index        varBmi=Weight/Math.POW(Height, 2);        //Third step, if statement to judge. Notice the phenomenon of jumping        if(BMI>  +){            Alert("very fat.");        } Else if(BMI>=  -){            Alert("Obesity");        } Else if(BMI>=  -){            Alert("overweight");        } Else if(BMI>= 18.5){            Alert("Normal")} Else {            Alert("Thin");        }
Nesting of IF statements

Let's use the following example to draw a nested if statement.

    一个加油站为了鼓励车主多加油,所以加的多有优惠。    92号汽油,每升6元;如果大于等于20升,那么每升5.9;    97号汽油,每升7元;如果大于等于30升,那么每升6.95    编写JS程序,用户输入自己的汽油编号,然后输入自己加多少升,弹出价格。

The code is implemented as follows:

        //First step, enter        varBianhao= parseint(prompt("What oil would you like to add?" Fill in 92 or "));        varSheng= parsefloat(prompt("How many liters would you like to add?" "));        //Second step, Judge        if(Bianhao==  the){            //number is 92 time to do things            if(Sheng>=  -){                varPrice=Sheng* 5.9;            } Else {                varPrice=Sheng* 6;            }        } Else if(Bianhao==  the){            //number is 97 time to do things            if(Sheng>=  -){                varPrice=Sheng* 6.95;            } Else {                varPrice=Sheng* 7;            }        } Else {            Alert("Sorry, there's no petrol for this number!" ");        }        Alert("The Price is" +Price;
Several small knowledge points of the IF statement

(1) Else part can be omitted. For example:

        var=10;        if>20){            alert("这个数字大于20");        }

There is no else part, which means there is no "otherwise". If the conditional expression is not satisfied, then nothing is done.

(1) If there is a sentence to be done, the curly braces can be omitted. For example:

        var=2;        if>5alert("这个数字大于5");        alert("哈哈");

The popup content is "haha".

Structure of For loop for loop

For loop Example:

    for (var=1;<=100; i++{        console.log(i);    }

Explanation of the above code:

For loop traversal
    for (var=1;<13;=+4{        console.log(i);    }

The traversal steps of the above code:

程序一运行,将执行var i = 1;这条语句, 所以i的值是1。然后程序会验证一下i < 13是否满足,1<13是真,所以执行一次循环体(就是大括号里面的语句)。执行完循环体之后,会执行i=i+4这条语句,所以i的值,是5。程序会会验证一下i < 13是否满足,5<13是真,所以执行一次循环体(就是大括号里面的语句)。执行完循环体之后,会执行i=i+4这条语句,所以i的值,是9。程序会会验证一下i < 13是否满足,9<13是真,所以执行一次循环体(就是大括号里面的语句)。执行完循环体之后,会执行i=i+4这条语句,所以i的值,是13。程序会会验证一下i < 13是否满足,13<13是假,所以不执行循环体了,将退出循环。最终输出输出结果为:1、5、9

Next, do a few questions.

Topic 1:

    for (var=1;<10;=+3{        =+1;        console.log(i);    }

Output results: 2, 6, 10

Topic 2:

    for (var=1;<=10; i++{    }    console.log(i);

Output results: 11

Topic 3:

    for(var=1;<7;=+3){    }    console.log(i);

Output results: 7

Topic 4:

    for (var=1;>0; i++{        console.log(i);    }

The dead loop.

Algorithm problem Exercises

Temporary.

Function

Function: is to encapsulate some statements, and then execute them in the form of calls .

Role of the function:

    • Write a large number of repeated statements in the function, and later when you need these statements, you can call the function directly, avoid duplication of work.

    • Simplify programming and make programming modular.

Let's look at an example:

    console.log("你好");    sayHello();     //调用函数    //定义函数:    functionsayHello(){        console.log("欢迎");        console.log("welcome");    }
First step: definition of function

Syntax for function definitions:

    function 函数名字(){    }

The explanations are as follows:

    • Function: is a keyword. Chinese is "function", "function".

    • Function name: The naming convention is the same as the naming of variables. Can only be letters, numbers, underscores, dollar signs, and cannot start with a number.

    • Parameters: There is a pair of parentheses behind, which is used for parameters.

    • Inside the curly braces is the statement of this function.

Step Two: Call the function

Syntax for function calls:

    函数名字();
Parameters of the function: formal parameters and arguments

The parameters of the function include the formal parameter and the actual parameter. Take a look at the following diagram to understand:

Note: The number of actual and formal parameters should be the same.

Example:

        sum(3,4);        sum("3",4);        sum("Hello","World");        //函数:求和        functionsum(a,{            console.log+ b);        }

Console output Results:

    7    34    helloworld
return value of the function

Example:

        console.log(sum(3,4));        //函数:求和        functionsum(a,{            return+ b;        }

The function of return is to end the method.

My public number.

Want to learn soft skills Beyond the code ? You may wish to follow my public number: Life team (ID: vitateam ).

Sweep, you will discover another new world, and it will be a beautiful surprise:

JavaScript syntax explained: If statement &for loops & Functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.