JavaScript basics 05-day07 [conditional operators, operator precedence, code blocks, if statements]

Study address:

  1. Grain College---Silicon Valley
  2. Bilibili website——Silicon Valley's latest version of JavaScript basic full set of tutorials (140 episodes of practical teaching, JS from entry to master)

Summary of JavaScript basics and advanced study notes [Silicon Valley latest version of the full set of JavaScript basic tutorials (140 episodes of practical teaching, JS from entry to master)]

table of Contents

Class 25: JS basic _ conditional operator

Class 26: JS basics _ operator priority

Operator precedence table (from top to bottom, with decreasing precedence)

Class 27: JS Basics_Code Block

Class 28: JS basic _if statement (1)

Class 29: JS basic _if statement (two)

Class 30: JS Basic_Exercise


Class 25: JS basic _ conditional operator

/*
 * Conditional operator is also called ternary operator
 * Syntax:
 *          Conditional expression? Statement 1: Statement 2;
 *-Process of execution:
 * When the conditional operator is executed, the conditional expression is first evaluated,
 * If the value is true, statement 1 is executed and the execution result is returned
 * If the value is false, statement 2 is executed and the execution result is returned
 * If the evaluation result of the expression of the condition is a non-Boolean value ,
 * will It is converted to a Boolean value and then calculated.
 */

Class 26: JS basics _ operator priority

/*
 *, operator
 * can be used to split multiple statements, generally can be used when declaring multiple variables,
 */
//use, the operator declares multiple variables at the same time
//var a, b, c;

//You can declare multiple variables and assign them at the same time
//var a=1, b=2, c=3;
//alert(b);

/*
 * Just like in mathematics, operators in JS also have precedence.
 * For example: multiply and divide first and then add and subtract.
 * There is a table of operator precedence in JS.
 * The higher the table, the higher the priority. , The higher the priority, the more priority it will be calculated.
 * If the priority is the same, it will be calculated from left to right.
 * But we don’t need to remember this table. If the priority is not clear, you
 can use () to change the priority
 */

1 || 2 -> 1: The first one is true, do not look at the second one.

1 && 3: The first one is true, and the second one (3) is returned.

Operator precedence table (from top to bottom, with decreasing precedence)

Class 27: JS Basics_Code Block

Statement

  • The expressions and operators mentioned above can be understood as words and phrases in our language.
  • And the statement is the complete sentence in our language.
  • A statement is the basic unit of a program. A JS program is composed of one statement, and each statement uses the end.
  • Statements in JS are executed in order from top to bottom by default, but we can also control the execution order of statements through some flow control statements.

/*
 * Our program is composed of one by one statement
 * The statements are executed one by one in a top-down order
 * In JS, you can use {} to group statements,
 * The same statement in {} we It is called a group of statements,
 * They are either executed or not executed,
 * A statement in {} is also called a code block
 * No need to write after the code block;
 * 
 * JS in The code block only has the function of grouping and has no other purpose.
 * The content of the code block is completely visible from the outside.
 */

Class 28: JS basic _if statement (1)

/*
 * Flow control statements
 *-The program in JS is executed line by line from top to bottom
 *-The flow of program execution can be controlled through flow control statements,
 * The program can be selected to execute according to certain conditions
 *-Statement classification:
 * 1. Conditional judgment statement
 * 2. Conditional branch statement
 * 3. Loop statement
 * 
 * 
 * Conditional judgment statement:
 *-Use the conditional judgment statement to judge before executing a certain statement,
 * If the condition is satisfied, the statement will be executed, condition If not, the statement will not be executed.
 *-if statement
 *-Syntax 1:
 * if (conditional expression){  * statement...  *}  *               * When the if statement is executed, the conditional expression will be evaluated first,  * if the value of the conditional expression If true, the statement after the if is executed,  * If the value of the conditional expression is false, the statement after the if will not be executed.  * The if statement can only control the statement that immediately follows it,







 * If you want the if statement to control multiple statements,
 * You can put these statements in a code block.
 The code block after the if statement is not necessary, but try to write the code block in the development, even if there is only one statement after the if
 *             
 */

Class 29: JS basic _if statement (two)

/*
 * if statement
 * Syntax 2:
 * if(conditional expression){  * statement...  * }else{  * statement...  *}  *   * if...else... statement  * When the statement is executed , The conditional expression after the if will be evaluated first,  * If the value is true, the statement after the if is executed  * If the value is false, the statement after the else is executed  *   * Syntax 3:  * if(condition Expression){  * statement...  * }else if(conditional expression){  * statement...  * }else if(conditional expression){  * statement...  * }else{  * statement...  *}  * 





















 * if...else if...else
 * When the statement is executed, the conditional expression will be evaluated from top to bottom.
 * If the value is true, the current statement will be executed.
 * If the value is false, continue to judge downward.
 * If all the conditions are not met, the statement after the last else
 will be executed * In this statement, only one code block will be executed. Once the code block is executed, the statement will be ended directly
 */

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script type="text/javascript">
			var age = 50;
			if (age >= 60) {
				alert("你已经退休了~~");
			} else {
				alert("你还没退休~~~");
			}
			
			age = 200;
			if (age > 100) {
				alert("活着挺没意思的~~");
			} else if (age > 80) {
				alert("你也老大不小的了~~");
			} else if (age > 60) {
				alert("你也退休了~~");
			} else if (age > 30) {
				alert("你已经中年了~~");
			} else if (age > 17) {
				alert("你已经成年了");
			} else {
				alert("你还是个小孩子~~");
			}
			
			age = 90;
			if (age > 17 && age <= 30) {
				alert("你已经成年了");
			} else if (age > 30 && age <= 60) {
				alert("你已经中年了");
			} else if (age > 60 && age <= 80) {
				alert("你已经退休了");
			} else {
				alert("你岁数挺大的了~~");
			}
		</script>
	</head>
	<body>
	</body>
</html>

Class 30: JS Basic_Exercise

/*
 * prompt() can pop up a prompt box with a text box.
 * The user can enter a piece of content in the text box. This function requires a string as a parameter.
 * The string will be The prompt text of the prompt box
 * 
 * The content entered by the user will be returned as the return value of the function. You can define a variable to receive the content
 */
var score = prompt("Please enter Xiao Ming's final score:");
alert(score) ;

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/108610833