Experience this series of video tutorials

Click to watch: Bilibili

Flow Control 1

Process control classification

  • Sequential execution (sequential structure)
Relationship between short-term flow and short-term; flowchart TD A --> B; B -->C; C -->D;
  • Conditional execution (branch structure)
Flowchart TD A[weather today] --> B{How is it? }; B - > | | C clear (to play), B - > | | D/knock code at home on rainy days;
  • Loop execution (loop structure)
Relationship between short-term flow and short-term; flowchart TD A --> B; B --> B; B --> B; B --> B; B --> B; B --> C[sleep];

Conditional statements

if

The if structure determines the Boolean value of an expression and then executes different statements based on whether the Boolean value is true or false.

if(Boolean) {statement; }Copy the code
Avoid assignment operators

Note that the assignment expression (=), the congruence operator (===), and the equality operator (==) should not be confused in the expression following if. Because assignment expressions don’t compare.

var x = 1;
var y = 2;
if (x = y) {
  console.log(x);
}
/ / "2"
// The program runs normally, and no errors are reported
Copy the code

To avoid this, some developers write constants to the left of operators, so that if an equality operator is accidentally written as an assignment operator, an error will be reported because constants cannot be assigned.

if (x = 2) { / / is not an error
if (2 = x) { / / an error
Copy the code

else

// If and else can execute only one branch
/* if (! True) {console.log(' condition satisfied '); } else {console.log(' condition not met '); } console.log(' code below '); // all execute */

/* var age = 21 if (age >= 18) {console.log(' adult! '); } else {console.log(' Minor! '); } * /

var day = 5
if ( day == 6 || day == 7 ) {
  console.log('Day of rest');
} else {
  console.log('Working day');
}
Copy the code

else if

/* var age = -1 if (age >= 18) {console.log(' adult '); } else if (age < 18 && age >= 0) {console.log(' minor '); } else {console.log(' invalid age '); } * /

var day = -9
if ( day == 6 || day == 7 ) {
  console.log('Day of rest');
} else if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5 ) {
  console.log('Working day');
} else {
  console.log('Illegal date');
}
Copy the code

If and ternary operators

var result;
var weather = 'sun'
/* if (weather == 'sun') {result = 'sun'; '} else {result = 'can't go out to play'} */
weather == 'rain' ? result = 'Go out and play! ' : result = 'No going out to play'
console.log(result);
Copy the code

Nesting of conditional statements

var num = 9
if ( num % 2= =0) {
  if ( num % 3= =0) {
    console.log('Divisible by 2 and divisible by 3.');
  } else {
    console.log('Divisible by 2, not divisible by 3'); }}else {
  if ( num % 3= =0) {
    console.log('Not divisible by 2, but divisible by 3.');
  } else {
    console.log('Not divisible by 2, not divisible by 3.'); }}Copy the code

practice

  • The rating scores

Enter the score through the prompt box and print the score value separately:

  • Equals 100: prints perfect
  • Greater than or equal to 90, less than 100: excellent printing
  • Greater than or equal to 80, less than 90: good print
  • Greater than or equal to 70, less than 80: print average
  • Greater than or equal to 60, less than 70: print pass
  • Greater than or equal to 0, less than 60: fail to print

Fault tolerance mechanism:

  • Greater than 100: The print score cannot be greater than 100
  • Less than 0: the fraction cannot be negative
  • Those unable to convert to a valid number will be warned: cannot convert to a number
var score = prompt('Please enter the score! ') // The input data is converted to a string
score = Number(score)
if ( score == 100 ) {
  console.log('perfect! ');
} else if ( score >= 90 && score < 100) {
  console.log('good! ');
} else if ( score >= 80 && score < 90) {
  console.log('good! ');
} else if ( score >= 70 && score < 80) {
  console.log('general! ');
} else if ( score >= 60 && score < 70) {
  console.log('to pass the exam! ');
} else if ( score < 60 && score >= 0) {
  console.log('Fail! ');
} else {
  if ( score > 100) {
    console.log('The score cannot be greater than 100');
  } else if ( score < 0) {
    console.log('The fraction can't be negative.');
  } else {
    // Execute the following code if score is not a number
    console.log('Non-numeric'); }}Copy the code