Learning to get started with JavaScript (iii)

Source: Internet
Author: User

2016.12.6 10:30 Complete the second chapter of JavaScript study, read the beginning of the third chapter of the general statement, all say that the original JS every knowledge point can be divided into minutes to give up, and I am trying to explore. The end of the time to go home for a holiday, hope at home can also have a little practice, when the school can be JS over again, ah, and jquery!!!

Iii. judgments, loops, and functions (checking the validity of user input)

3.1 SELECT statement--IF statement and Swith statement (the program has certain logical judgment ability)

3.1.1 The following code: (assigns the result of a conditional expression to a variable)

3.1.2 If statement "if the room temperature exceeds Fahrenheit, turn on the air conditioner",

Similar code: if (Roomtemperature >) {roomtemperature = roomTemperature-10;}

There is only one statement in the code block, and the curly brace can be omitted to read: if (roomtemperature >) roomtemperature = roomTemperature-10;

The judging criteria must be placed in parentheses after the IF keyword, and no semicolon at the end.

Curly braces have a special purpose in JS, which means that multiple statements enclosed in curly braces are a block of code, and JS treats blocks of code as a single statement. However, if you need to execute multiple statements when the conditional statement expression is true, you should surround the statements with curly braces to form a block of code.

This can also be changed as follows:

When the condition is true, the entire block of code is executed, and when the condition is true, the entire block of code is executed. However, because the condition is true when you forget to use curly braces, the next statement immediately following the conditional expression of if is executed. However, the other lines of code are always executed, regardless of whether the condition of the If evaluates to TRUE or FALSE. To avoid such errors, even if there is only one statement, it is best to stick around with curly braces, which is a good programming habit to avoid forgetting to use curly braces to create errors.

As the following small example:

Code explanation: When the browser opens, the prompt box has entered 32 as the Fahrenheit temperature to be converted. For Fahrenheit 32°, the conditional judgment expression for the two if statements in the code is false, so the code in the two if statements is not executed, and only one row of output is on the page (32°fahrenheit is 0°centigrade);

Input 32 o'clock is displayed like this; When you enter 156

Two variables are declared, where the initial value of Degfahren is the return value of the prompt () function. Because the return value of the prompt () function is a string, the number () function is used to explicitly convert the string to a numeric value.

The variable degcent is used to hold the result of the arithmetic expression 5/9* (degFahren-32), which converts the Fahrenheit degree to Celsius.

The document then loses the converted results on the page

The two if statement is the new code, if the judged variable is less than 0 is true then the code in parentheses will be executed, it will use the document.write () method to output a piece of text information to the page; if the answer is false, the program will go directly to the next line of code after the curly brace Terminator ( Code indentation is a good programming habit that makes your code clearer and easier to read.

Learning to get started with JavaScript (iii)

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.