Sequential structure: a program structure executed line by line from top to bottom branch structure: a structure that determines the direction of program execution based on different conditional judgments (also called selection structure) Loop structure: a program structure that requires the same operation to be performed repeatedly is called a loop structure.

If determines logical statements

if(true) {// The statement to execute
}
if(false) {// Skips statements that are not executed
}
if(Judgment condition){Statement 1 is executed if the result in the if parentheses is true, otherwise statement 2 is executed
    //语句1
}else{
    //语句2
}

if(Judgment condition1) {Statement 1 is executed when condition 1 results in true
    //语句1
}else if(Judgment condition2) {Statement 2 is executed when condition 1 is false and condition 2 is true
    //语句2
}else{ // Execute statement 3 when condition 1 and condition 2 are both false
    //语句3
}
Copy the code

As we all know, only Boolean values are needed in the if parentheses. All data types are implicitly converted to Boolean types in an if judgment. Note: 0, -0, null, “”, false, undefined, or NaN results in false in the if condition

Implicit type conversion

With the exception of the + operator, – * / has only mathematical meaning, so by default – * / implicitly converts the data type on both sides of the operator to a numeric type before performing the operation.

var a = 2, b = "2";
if(a == b){
    alert("A and B are equal.");    / / can play
}
if(a === b){
    alert("A and B are equal.");    / / won't play
}
if(1= =true){
    alert("The number 1 is true");
}
if(a = b){
    alert("A and B are equal.");
}
// An equal sign represents the assignment operation, resulting in the value to the right of the equal sign
Copy the code

Switch Case Multi-branch statement

switch(statement) {// The result of the statement matches the contents of each case
    case 1:
       alert(1);
           break;
        case 2:
             alert(2);
             break;
        case 3:
            alert(3);
            break;
        default:
            alert(0);
}
Copy the code

Note the application scenarios of the switch (when there are multiple deterministic values to determine); Note case penetration, adding a break statement (if the program does not find a break statement, the parser continues parsing);

var a = 1;
switch(a){ 
    case 1:
       alert(1);
    case 2:
        alert(2);
    case 3:
        alert(3);
    default:
        alert(0);
}
// Each alert is executed, and an alert blocks the program process
Copy the code

practice

1. Determine which range an integer belongs to: greater than 0; Less than zero. = 0 2. Check whether an integer is even or odd, and output the result 3. Develop a software, according to the formula (height -108) *2= weight, can have about 10 catties floating. 4. Determine if a three-digit number is the number of daffodils (e.g. 153 = 1^3 + 5^3 + 3^3)

practice

1. Click to switch div color 2. Enter the score to determine the grade 3. Enter the month to display the days of the month. Requirements: Simplify code 5 with case penetration. Enter a number to display the day of the week, such as: enter 0, pop up Sunday, and so on (two methods) 6. Design a guessing game: Suppose: 1 represents rock, 2 represents scissors, and 3 represents paper. Each time the computer randomly appears any number from 1 to 3 (parseInt (math.random ()*3) + 1), the player also has three states. The player’s number is compared with the number given by the computer, and the winner is determined according to the rules of the game.

practice

1. According to a numerical date, determine what day of the year the date is. For example, 20160211. Make a form with username, password, and phone requirements: 1) Do a good point 2) Click the save button, use JS to judge whether the user’s contact phone number is pure digits, if the user input error, use alert() pop-up warning 3) judge whether the user name is filled in, if blank, use alert pop-up warning 4) judge password length, must be greater than 8 bits, otherwise alert warning: Gets the length of the string via the length attribute

var str = "hello";
str.length // Get the length
Copy the code