JS conditional statement

Time:2021-2-25

1. If structure

Grammar:

If (condition){

Satisfies the statement block to be executed

}

Implementation process:

1. Judgment conditions
2. If the condition is true, the contents in the statement block are executed
3. If the condition is false, skip the statement block to execute other contents

be careful:

(1) the condition in if should be a Boolean value. If it is not a boolean type, the condition will be automatically converted to false
       if(0)
       if(0.0)
       if(undefined)
       if(null)
       if(“”)
       if(NaN)

(2) The {} after if can be omitted. If omitted, only the first statement under if is controlled

2.if … else … structure

Grammar:

If (condition){

The block of statements to execute when the condition is met

  }else{

The statement block to execute when the condition is not met

  }

practice:

From the pop-up box, enter a year information, judge whether the year is leap year, and give a prompt

function showYear(){
    var year =  window.prompt (please enter a year:);
    If ((year% 4 = = 0) & (year% 100! = 0) | (year% 400 = = 0)) {// the condition for judging whether it is a leap year
    console.log (year + "year is leap year");
    }else{
        console.log (year + "year is not leap year")
    } 
}

3.if … else if… structure

Grammar:

If (condition 1){

The statement block to execute when condition 1 is met

}Else if (condition 2){

The statement block to execute when condition 2 is met

}Else if (condition 3){

The statement block to execute when condition 3 is met

  }else{

Statement block executed when none of the above conditions are met

  }

Exercise: input test scores from the pop-up box, if the score is > = 90, output a; if the score is > = 80, output B; if the score is > = 60, output C; otherwise, output D

function showScore(){
    var score =  window.prompt ("please enter your score:);
    //Score > = 90, a
    //Grade > = 80, B
    //Grade > = 60, C
    //Other results were d
    if (score >= 90){
        console.log ("your grade is a");
    }else if(score >= 80){
        console.log ("your grade is B");
    }else if(score >= 60){
        console.log ("your grade is C");
    }else{
        console.log ("your grade is d");
    }
}

4.switch … Case structure

Grammar:

Switch (variable){
Case constant value 1:
Statement block 1;
Break; / / end switch execution through break (optional)
Case constant value 2:
Statement block 2;
Break; / / optional
    default:
Statement block n;
Break; / / optional
  }

practice:

(1) Use a special switch structure to complete the requirements: input year and month, output the number of days in the month

function testSwitch(){
    Var year = prompt ("please enter year");
    Var month = number (prompt ("please enter month");
    switch(month){
        case 1 :
        case 3 :
        case 5 :
        case 7 :
        case 8 :
        case 10 :
        case 12 :
            console.log (year + "year" + month + "month has 31 days");
            break;
        case 4 :
        case 6 :
        case 9 :
        case 11 :
            console.log (year + "year" + month + "month has 30 days");
            break;
        case 2 :
            if((year%4==0)&&(year%100!=0)||(year%400==0)){
                console.log (year + year + month + month has 29 days)
            }else{
                console.log (year + year + month + month has 28 days)
            }
            break;
        default :
            console.log ("you entered it incorrectly");
    }
}

(2) Using switch Case statement programming, when the input date, calculate this day is the first day of the year

function Days(){
            Var year = number (prompt ("please enter year");
            Var month = number (prompt ("please enter month");
            Var day = number (prompt ("please enter date");
            var totalDay = 0;
            switch(month-1){
                case 11 :
                    totalDay += 30;
                case 10 :
                    totalDay += 31;
                case 9 :
                    totalDay += 30;
                case 8 :
                    totalDay += 31;
                case 7 :
                    totalDay += 31;
                case 6 :
                    totalDay += 30;
                case 5 :
                    totalDay += 31;
                case 4 :
                    totalDay += 30;
                case 3 :
                    totalDay += 31;
                case 2 :
                    if((year%4==0)&&(year%400!=0)||(year%400==0)){
                        totalDay +=29;
                    }else{
                        totalDay +=28;
                    }
                case 1 :
                    totalDay += 31;
            }
            totalDay +=day;
            console.log (year + year + month + month + day + day is the day + totalday +);
        }