Process control statement
(1) Sequential statements top-down
(2) Branch statement:
a) if statement
Use syntax 1: if (relational expression) {code to execute}
Relational expression: Can calculate a true or false expression Execution process: when the relationship expression is true Execute when{}Internal code When the relationship expression is false Not when{}Inside
Use syntax 2: if(){}else {}
Execution process: When the relationship expression is true Execute when if after{}Internal code When the relationship expression is false Execute when else{}Inside Input box: prompt(Please enter....) As long as it is an input box, the output is string
Use syntax 3: if(){} else if(){}else if() {}... Else {}
Execution process: Judge the condition from top to bottom. If the condition is met, execute the corresponding code, and the subsequent code will not be executed if Multiple branches are generally compared for ranges switch Comparison is generally made for the specified range
Day 3 switch structure and loop structure
Content: switch structure,
Loop structure: while do... while for loop
1. switch statement
switch conditional branch structure is also a kind of conditional judgment statement, which is the judgment of a variable;
Execution process:
Match the expression after switch with the expression after case (congruent matching)
The matching is successful. The code blocks after the corresponding case are executed, down one by one, and break abort is encountered,
If the match is not successful, go to the default statement
Note:
The expression after switch is generally a variable, and the expression after case is generally a constant (a variable that will not change)
Syntax:
switch (expression) { case Case 1: Case 1 code to execute break case Case 2: Case 2 code to execute break case Case 3: Case 3 code to execute break default: Code executed when none of the above conditions are satisfied }
To determine when a variable is equal to a value, use it
Example: the number given according to the variable shows the day of the week
<script> var week = 1 switch (week) { case 1: alert('Monday') break case 2: alert('Tuesday') break case 3: alert('Wednesday') break case 4: alert('Thursday') break case 5: alert('Friday') break case 6: alert('Saturday') break case 7: alert('Sunday') break default: alert('Please enter a 1~7 Number between') } </script>
2. Cyclic structure
Loop structure is to execute the same piece of code repeatedly according to some given conditions
The loop must have some fixed content
1. Initialization
2. Condition judgment
3. Code to execute
4. Self change
1) WHILE loop
while is called when in Chinese. In fact, it means that the code will be executed when the conditions are met. Once the conditions are not met, it will not be executed
Syntax while (condition) {execute when condition is met}
Execution process: initialize the statement first
Making conditional judgment
true ------- execute the code in {}
--------- > judge the condition when the initial value changes
true ------- execute the code in {}
false ------ out of the loop, do not execute the code in {}, and execute the code below {}
false ------ out of the loop, do not execute the code in {}, and execute the code below {}
Because it is executed when the conditions are met, we must pay attention to setting a boundary value when writing, otherwise it will continue to cycle
<script> //1. Initialization condition var num = 0; //2. Conditional judgment while (num < 10) { // 3. Code to execute console.log('Current num The value of is ' + num) // 4. Self change num = num + 1 } </script>
If you don't change yourself, you'll keep cycling
2) DO WHILE loop
Is a loop similar to the while loop
while will judge the conditions first and execute if they are met. If they are not met, they will not be executed directly
But the do while loop is,
Execution sequence: execute once regardless of conditions, and then judge the conditions at the beginning
Syntax: do {code to execute} while (condition)
<script> // In the following code, the condition is not satisfied at the beginning, but the code behind {} do will still be executed once var num = 10 do { console.log('I did it once') num = num + 1 } while (num < 10) </script>
3) FOR cycle
A loop structure that is different from while and do while loops
The reason is the same as the other two, which execute code in a loop
Syntax: for (VaR I = 0; I < 10; I + +) {code to execute}
Execution sequence: 1. Go through the initialization statement first,
2. Condition judgment
True - > execute {} loop body - > variable update - > condition judgment - > true -
false - terminate loop
False - > terminate loop
<script> // Initialization, condition judgment and variable update are written together for (var i = 1; i <= 10; i++) { // Here is the code to be executed console.log(i)} // The console will output 1 ~ 10 in turn </script>
This just looks uncomfortable, but it's easy to use
difference:
The difference between a dowhile loop and a while loop: a dowhile loop goes through the loop body at least once. A while loop needs to judge the conditions before it goes
The difference between a for loop and a while loop is that if the number of times of a for loop is clear, the while loop is used to calculate the number of times of the loop if the number of times of the loop is not clear