JavaScript (4): process control

if statement

  • Grammatical structures

if (conditional expression) { //The code statement executed when the condition is established }

For example:

var a = 10;
if(a > 1 ){
    
    
alert('Shao Yu is more handsome than Zheng Yu');
}

result:
Please add a picture description

if else statement (double branch statement)

  • Grammatical structures

//The condition is true, execute the code in if, otherwise execute the code in else
if (conditional expression) {     //[If] the condition is true to execute the code } else{     //[Otherwise] execute the code }



For example:

 var year = prompt('请输入年份:');
 if (year%4==0 && year%100!=0 || year%400==0){
    
    
    alert('闰年');
 }
else{
    
    
    alert('平年');
 }

if -else if statement (multi-branch statement)

  • Grammatical structures

if (conditional expression 1) {      statement 1; } else if (conditional expression 2) {       statement 2; }else if (conditional expression 3) {       statement 3; } ... }else {  //The above conditions are not true to execute this code }









For example:

   var course = prompt('请输入成绩:');
        if (course > 90) {
    
     console.log('A'); }
        else if (course > 80) {
    
     console.log('B'); }
        else if (course > 70) {
    
     console.log('C'); }
        else if (course > 60) {
    
     console.log('D'); }
        else {
    
     console.log('不及格'); }

ternary expression

  • grammatical format

Conditional expression? expression1 : expression2

If the result of the conditional expression is true, return the value of expression1; if the result of the conditional expression is false, return the value of expression2

For example:

var num = 10;
var  result = num > 5 ? 'YES' : 'NO';
console.log(result;)//结果为YES

switch statement

  • Grammatical structures

switch(expression){ case value 1: //The code to be executed when the expression is equal to value1 break; case value 2: //The code to be executed when the expression is equal to value2 break; default: //The expression is not equal to any The code to execute when value }








The switc statement stops when it encounters a break. If there is no break in the case, it will continue to execute the next case until it stops when it encounters a break.

For example:
Please add a picture description
the result is 3

Please add a picture description
The result is 3 4

for loop

In a program, a group of statements that are repeatedly executed is called a loop body, and whether it can continue to be repeatedly executed depends on the termination condition of the loop. A statement consisting of a loop body and a loop termination condition is called a loop statement.

  • Grammatical structures

for (initialization variable; condition expression; operation expression) { //loop body }

  1. Initialized variable: an ordinary variable declared with var, usually used as a counter;
  2. Conditional expression: used to determine whether each loop continues to execute, and is the condition for termination;
  3. Operation expression: The code executed at the end of each loop, often used for counter variable update.

For example:

      for (var i = 1; i <= 3; i++) {
    
    
            console.log('你好');
        }
        //用户控制输出次数
        var num = prompt('请输入你次数:');
        for (var m = 1; m <= num; m++) {
    
    
            console.log('hello');
        }

Please add a picture description
Example 2:

var num=prompt('请输入人数:');
var sum=0;
var ave=0;
for(var i=1;i<=num;i++){
    
    

s=parseFloat(prompt('请输入第'+i+'个学生成绩')) ;
sum+=s;
}
avg=sum/num;
console.log('学生总成绩为'+sum);
console.log('学生平均分为'+avg);

Please add a picture description

Please add a picture description

Example 3: Print five five-pointed stars in a row

Please add a picture description

If written like this, the five stars cannot be arranged in the same row, only one number will be displayed

Correct solution: method: take the method of appending a string, so that it can be printed to the console
Please add a picture description

while loop

The while statement can loop through a specified piece of code on the premise that a conditional expression is true until the expression is not true.

  • Grammatical structures

while (conditional expression) { loop body code }

  • Execution idea
    1. Execute the conditional expression first, if it is true, then execute the loop body code; if it is false, exit the loop and execute the following code; 2. Execute the
    loop body code;
    3. After the loop body code is executed, the program It will continue to judge the execution condition expression, if it is still true, execute the first step
    Please add a picture description

do while loop

The do...while statement is actually a variant of the while statement. This loop will execute the code block once, and then judge the conditional expression. If the condition is true, the loop body will be executed repeatedly, otherwise the loop will exit.

  • Grammatical structures

do { loop body code } while (conditional expression);

For example:

        var i = 1;
        do {
    
    
            console.log(i);
            i += 1;
        } while (i > 10);
        //结果为1,先执行do,在执行while

Note: execute the loop body once first, and then judge, the do...while loop statement will execute the loop body code at least once.

continue keyword

The continue keyword is used to jump out of this loop immediately and continue to the next loop

for(var i = 1;i<=5;i++){
    
    
    if ( i == 3){
    
    
        continue;
    }
    console.log(i);
}
//结果为1 2 4 5,当i==3时,continue跳出本次i==3的循环,继续执行i++

break keyword

The break keyword is used to immediately break out of the entire loop (end of the loop)

for(var i = 1;i<=5;i++){
    
    
    if ( i == 3){
    
    
        break;
    }
    console.log(i);
}
//结果为1 2,当i==3时,break结束本次for循环

Guess you like

Origin blog.csdn.net/weixin_53912712/article/details/128321885