JavaScript Branch Structure Learning (Learning Diary 5)

Posted by Bryan Ando on Sun, 21 Nov 2021 19:04:49 +0100

1. Statements

           Statement: JavaScript programs are executed in lines, which means line-by-line execution. In general, each line is a statement

            The difference between a statement and an expression:

                - Statements are meant to do something, and generally do not require a return value

                    * A semicolon indicates the end of a statement

                    * Multiple statements can be written on one line

                    * There can be nothing before the semicolon, which the JavaScript engine treats as empty statements

                - Expression is to get a return value, it must return a value

                    * Expression does not require a semicolon end

                    * Once a semicolon is added after an expression, the JavaScript engine treats the expression as a statement, which results in some meaningless statements

var a = 1 + 3 ; var b = 'abc';//A statement ends with a semicolon, and a semicolon indicates the end of a statement. Multiple statements can be written on one line.

;;;//Represents three empty statements.

'abc';//A statement simply produces a value and has no practical meaning.

 var a = 1 + 3;// 1+3 is called an expression and refers to a calculation to get the return value
       

Ii. if structure

if statement: mainly to judge, first to determine the Boolean value of an expression, and then to execute different statements according to the authenticity of the Boolean value

            Grammar:

                If (expression) {

                    Code block;

                }

// 1. If a>b, output pairs;
        var a = 30,b = 20;
        if(a > b){
            console.log('Yes');
        }


// 2. Ask an adult?       
        var user = 'So-and-so',age = 20;
        if(age >= 18){
            console.log('So-and-so' + age + 'Aged Adult')
        }
        var userless = prompt("Please enter your name"),ages = parseInt(prompt("Please enter your age"));
        if(ages >= 18){
            alert(userless + ages + 'Aged Adult')
        }
        

3. if-else statements

if-else: two branches, only one

            Grammar:

                If (expression) {

                    Code block 1;

                }else {

                    Code block 2;

                }

// 1. Ask an adult?
        var user = prompt("Please enter your name"),age = parseInt(prompt("Please enter your age"));
        console.log(typeof(user));
        if(age >= 18){
            alert(user + age + 'As an adult, he can take charge of himself');
        }else{
            alert(user + age + 'Juvenile, can't go to Internet cafe')
        }
        
// 2. Judging the maximum value of two numbers and outputting a larger value requires manual input by the user
        var num1 = Number(prompt()),num2 = Number(prompt());
        console.log(typeof(num1,num2));
        if(num1 > num2){
            alert('The larger value is' + num1);
        }else{
            alert('The larger value is' + num2);
        }
        

4. Ternary Expressions

Both branches execute only one, in which case a ternary expression can be used

// To determine the maximum value of two numbers and output the larger value, the user needs to input it manually
        var num1 = Number(prompt('Please enter a number'));
        var num2 = Number(prompt('Please enter another number'));
        var result =  num1 > num2 ? num1 : num2;
        alert('The larger number is:' + result);

//Or var result = num1 > num2? Alert ('larger number is: '+ num1'): alert ('larger number is:' + num2);


// Judging adulthood
        var user = prompt("Please enter your name"),age = parseInt(prompt("Please enter your age"));
        console.log(typeof(user));
        var cn = age >= 18 ? alert(user + age + 'As an adult, he can take charge of himself') : alert(user + age + 'Juvenile, can't go to Internet cafe');

5. if-else if-else statement

if statement: a branch

            if-else statement: two branches

            if-else if...-else: multiple branches

            Ultimately, only one branch will be executed

            Grammar:

                If (condition 1) {

                    Condition 1 establishes block 1 of code being executed;

                } Else if (condition 2) {

                    Condition 2 establishes block 2 of code being executed;

                } Else if (condition 3) {

                    Condition 3 establishes block 3 of code being executed;

                }...

                else {

                    None of the above conditions are valid for the code block n being executed;

                }

// Grade: <60E 60-69D 70-79C 80-89B 90-99A 100S

        var degree = Number(prompt('Please enter your score (100 full)'));
        if(!isNaN(degree)){
            if(degree === 100){
                alert('Your rating is: S');
            }else if(degree >= 90 && degree <= 99){
                alert('Your rating is: A');
            }else if(degree >= 80 && degree <= 89){
                alert('Your rating is: B');
            }else if(degree >= 70 && degree <= 79){
                alert('Your rating is: C');
            }else if(degree >= 60 && degree <= 69){
                alert('Your rating is: D');
            }else if(degree < 60){
                alert('Your rating is: E');
            }
        }else{
                alert('Error Entering Results!')
        }

6. switch-case

switch-case syntax:

                Switch (a specific condition) {

                    case value 1:

                        If the value judged is equal to the value 1, the code block 1 is executed.

                        break;

                    case value 2:

                        If the value judged is equal to 1, the code block 2 is executed.

                        break;

                    case value 3:

                        If the value judged is equal to 1, the code block 3 is executed.

                        break;

                    ... ...

                    default: Code block executed when values are not equal;

                }

// Known rank, judge score interval

        var degree = prompt('Please enter your rating( S/A/B/C/D/E)'); 
        switch(degree){
            case 'S':alert('100');
            break;
            case 'A':alert('90-99');
            break;
            case 'B':alert('80-89');
            break;
            case 'C':alert('70-79');
            break;
            case 'D':alert('60-69');
            break;
            case 'E':alert('60 Following');
            break;
            default:alert('Input error please re-enter!');
        }
        

               -  break: jump out of the whole statement

                - switch-case statement: multiple branches, only one execution

                -  In general, if series is used in range judgment

                - switch when making a judgement on the specific value

7. Practice Work

1. Judging leap years

// Leap years: divisible by 4, but not by 100; or divisible by 400 (if-else)
        var year = parseInt(prompt('Please enter a year'));
        if(!isNaN(year)){
            if(year % 4 == 0 && year % 100 != 100 || year % 400 == 0){
                alert(year + 'Is a leap year');
            }else {
                alert(year + 'Is a normal year');
            }
        }else {
            alert('Input error, please re-enter');
        }
        
        /*Or use if-else-if-else
         if(!isNaN(year)) {
            if(year % 4 == 0 && year % 100 != 0) {
                console.log(year + 'Is a leap year';
            }else if(year % 400 == 0) {
                console.log(year + 'Is a leap year';
            }else {
                console.log(year + 'It is a normal year';
            }
        }else {
            alert('Enter the correct year (positive integer)!'.
        } */

2.   User enters a number to determine if it is odd or even

//User enters a number to determine if it is odd or even
        
        var num = parseInt(prompt('Please enter an integer'));
        if(!isNaN(num)){
            if(num % 2 == 0){
                alert(num + 'Is Even');
            }else{
                alert(num + 'is odd');
            }
        }else{
            alert('Input error, please re-enter!');
        } 

3.   Display the corresponding days based on the month entered by the user (February ignores leap years and outputs 28 days directly)

//Display the corresponding days based on the month entered by the user (February ignores leap years and outputs 28 days directly)

        var month = prompt('Please enter a month');
        console.log(month);
        switch(month){
            case '1':alert(month + 'Month has 31 days');
            break;
            case '2':alert(month + 'Month has 28 days');
            break;
            case '3':alert(month + 'Month has 31 days');
            break;
            case '4':alert(month + 'Month has 30 days');
            break;
            case '5':alert(month + 'Month has 31 days');
            break;
            case '6':alert(month + 'Month has 30 days');
            break;
            case '7':alert(month + 'Month has 31 days');
            break;
            case '8':alert(month + 'Month has 31 days');
            break;
            case '9':alert(month + 'Month has 30 days');
            break;
            case '10':alert(month + 'Month has 31 days');
            break;
            case '11':alert(month + 'Month has 30 days');
            break;
            case '12':alert(month + 'Month has 31 days');
            break;
            default:alert('Input error, please re-enter');
        }

Topics: Javascript Front-end html