oneThe order in which a program is executed
- In program development, programs can be executed in three different ways:
- Sequential – Execute code sequentially, from top to bottom
- Branch – Decide which branch of code to execute based on criteria
- Loop – Lets specific code execute repeatedly
- The code blockIs a collection of multiple lines of executing code, held together by a {}
- In development, when a single line of code is difficult to accomplish a particular function, we put the code in a block of code
2. Branch statements
2.1 if the branch
There are three if branch structures:
- Single branched structure
- if..
- Multibranched structure
- if.. else..
- if.. else if.. else..
2.1.1 Single branch IF
- “If” is equivalent to the keyword in JavaScript if a score over 90 is a condition (the > symbol can be used)
- Case 2: Unit 5 yuan/jin apple, if you buy more than 5 jin, then immediately reduce 8 yuan note: here we let the user input the weight of the purchase, calculate the final price and pop up the result
2.1.2 Multi-branch If… else
- Multi-branch statement 1: If.. else..
- Case 1: Go to the playground if your score is over 90, otherwise go to a cram school
- When the conditions are met, do something
- Not satisfied (else), do something else
- Example 2: m=20, n=30, compare the size of two numbers, get the larger number
2.1.3 Multi-branch if… elseif… else
- Case 1: Spend holiday with girlfriend
- Define the holidayName string variable to record the holidayName
- If it’s Valentine’s Day, buy roses/go to the movies
- If it’s Christmas Eve, you should buy apples/have a big dinner
- If it’s a birthday Party, you should buy a cake/have a Party
- The rest of the day, you go to work
- Case 2: Score rating:
- A test score greater than 90: excellent
- Greater than 80 and less than or equal to 90: good
- Greater than 60 and less than 80: qualified
- Less than 60: fail
2.1.4 Implicit Boolean type conversion
- In development, the flow control statement implicitly converts the value of the expression in parentheses to a Boolean type.
- Note the five special cases: 0, “, NaN, undefined, and null, all of which return false when converted to a Boolean type.
2.2 the switch branch
- Switch is a branching statement:
- It executes the corresponding branch body by determining whether the result of an expression is equal to the constant of a case statement
- Unlike the if statement, where the switch statement can only do equality (using the congruence operator ===), the if statement can do range
- Case penetration problem:
- After a case statement is completed, the next case statement is automatically executed
- This phenomenon is called case penetration
- Break the keyword
- Solve this problem by adding the break keyword after each case block of code
- Case: Accompany girlfriend’s birthday case (via switch)
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, Initial-scale =1.0"> <title> <script type="text/javascript"> Var score=parseInt(64/10); var score=parseInt(64/10); Switch (score){case 10: case 9: case 8: case 7: case 6: document.write(" qualified "); break; Case 5: Case 4: case 3: case 2: default: document.write(" failed "); break; } //console.log(score); var score1=54; Switch (true){//case parseInt(score1/10)>=6: case score1>=60: document. Write (" valid "); break; Default: document.write(" failed "); break; } </script> </head> <body> </body> </html>Copy the code
3. Loop statements
- Why do we need loops?
- Loops reduce the need to write repetitive code (which can lead to messy code and more work).
- For example: Print HelloWorld 100 times/print numbers from 0 to 99 / calculate the sum of 1 to 100
- If we print it one by one the way we did before, it’s very troublesome. But it’s easy to do with loops
- This is where computers are most useful, helping us do repetitive, boring tasks
- In JavaScript, common loop statements:
- The while loop
- do.. The while loop
- The for loop
3.1 the while loop
-
Grammar:
-
Initialization statement; While (conditional expression){statement; Update expression; }Copy the code
-
-
As with if, the conditional expression in while is converted to a Boolean type, and the code block is executed repeatedly as long as the value is true.
-
Each time a block of code executes, the conditional expression is recalculated.
-
A while loop with a condition expression of true is an infinite loop and can be terminated with a break
3.2 the do… The while loop
-
Grammar:
-
Do {execute statement}while(conditional expression);Copy the code
-
-
do… While is very similar to while, except that it checks the value of the expression at the end of the loop instead of at the top.
-
==do… The while loop is executed at least once. = =
-
The difference between while and do: while judges first, then performs; But do… While is execute first, judge later
3.3 a for loop
-
The for statement is also a loop control statement, which we also call a for loop.
-
Most loops have a counter that controls how many times the loop is executed. The three key operations of the counter are initialization, detection, and update. The for statement makes these three steps explicit as part of the syntax.
-
Grammar:
-
For (initializes the expression; Conditional expression; Update expression){statement... }Copy the code
-
-
The for loop is the most used loop statement in development:
- Statement 1: Initialize the expression: var I =1
- Statement 2: conditional expression, used to judge the condition judgment
- Statement 3: incrementing or subtracting expressions, such as I ++ or I —
3.3.1 Circular exercises
<! DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, Initial-scale =1.0"> <title> loop statement </title> <script type="text/javascript"> */ var money=1000; /* var num=0; While (true) {money + = 0.05 * money; num++; if(money>=5000){ break; } } console.log(num); //33*/ var year=0; Do {money + = 0.05 * money; year++; if(money>=5000){ break; } }while(true); console.log(year); //33 </script> </head> <body> </body> </html>Copy the code
3.3.2 Variations of the for loop
var j = 1; for (; j < 11;) { document.write(j + ' '); j++; } document.write("<hr/>"); var j = 1; var count = 0; for (; j;) { //document.write(j+' '); if (count == 10) { document.write('count=' + count); break; } document.write(j + ' '); count++; j++; } document.write("<hr/>"); var j = 1; var count = 0; for (; j;) { document.write("a" + ' '); count++; if (count == 10) { j = 0; }}Copy the code
3.3.3 Multiple for loop exercises
Var I = 1; var I = 1; i < 10; Var j = 1; var j = 1; j <= i; J++) {/ / control the number of columns document. Write (I + '*' + '=' + I + j * j + '& have spent '); } document.write("<br/>"); } document.write("<hr/>"); For (var I = 1; i <= 4; For (var k = 1; k <= 4 - i; K++) {// this loop is used to control the front blank; document.write(" " ); } for (var j = 1; j <= 2 * i - 1; // control column number document.write("@"); } document.write("<br/>"); } for (var I = 3; i >= 1; i--) { for (var k = 1; k <= 4 - i; k++) { document.write(" " ); } for (var j = 1; j <= 2 * i - 1; j++) { document.write("@"); } document.write("<br/>"); } document.write("<hr/>");Copy the code
Break and continue keywords
-
Loop jump
- During the execution loop, we might want to do something when we meet a certain condition
- For example, the loop body stops executing (even if it hasn’t finished executing) and breaks out of the loop
- For example, if the loop body is not executed this time, the next loop body is executed
-
Loop jump control
- Break: Directly breaks out of the loop and the loop ends
- Break When a condition is met, the loop is broken and no further repeated code is executed
- Continue: Skip this loop and execute the next loop body
- Continue When a condition is met, subsequent iterations of the code are not executed
- Break: Directly breaks out of the loop and the loop ends
-
The break and continue statements are used to precisely control the execution of the code within the loop.
-
Using the break statement causes the program to immediately exit the nearest loop and force the statements following the loop to execute.
-
The break statement is only used in loops and switch statements.
-
Using the continue statement causes the program to skip past loops and continue with the next loop, without completing the entire loop.
-
Continue can only be used in loops and cannot appear in other structures.
Six.Breakpoint debugging
- In development, to get the value of a variable, we print it to the console
- However, to see a change in a variable, we can debug through breakpoints
- Breakpoint debugging is also called debug
- There are two common ways to enable breakpoint debugging:
- Method one: find your own source code and break the point in it
- Method 2: Write the debugger in the code