js Grammar Basics (5.1)

5. Process control

5.1. Select structure

Program flow chart

legend:

椭圆: 开始/结束
矩形: 操作
菱形: 判断
连接线: 走向
可以根据程序流程图,理清楚程序执行的流程

5.2.1.if statement

//if语句语法结构:
if(条件表达式){
`   这里是表达式成立后执行的代码
}

Program flow chart

Example:

var num = prompt('请输入分数:'); //prompt函数的功能是弹出一个输入框,收集用户输入的数据
if(num<60){
    console.log("同学你考了"+num+"分,不及格哦");
}

Summary: program execution process, when the code is running, perform var num = prompt (); receiving a user input to the data, if that is 50, then the 50 was assigned to num, program execution to the next if statement , get to do the determination num = 50, num = 50 <60, this case condition is satisfied, it will run the code inside the parentheses, the console output passage. If the user input 90 to the digital, and assigned to num, get the next if statement determines which do num = 90 <60, at this time the result is false, it will not execute the code inside the brackets, the end of the run

5.2.2.if ... else statement

if(条件表达式){
    表达式成立执行这里的代码
}else{
    表达式不成立执行这里的代码
}

Program flow chart

Example:

var num = prompt("请输入分数:");//获得分数
if(num<60){//如果小于60 执行里面的代码
      console.log("同学,你考了"+num+“分,没有及格哦”);
}else{//如果大于等于60 执行里面的语句
        console.log("同学,恭喜你,考了"+num+“分,成功晋级”);
}

Summary: if ... else statement and if statements like, but more a response plan, if the statement which, if the condition is not satisfied, then do nothing, but if ... else statement is one more feature that If that condition is not met if the conditions inside, then the implementation of the contents else behind the brackets. Therefore, with if ... else statement, or if in brackets after the execution of the code, executable code or else in brackets behind, a second election

5.2.3.if..elseif...elseif..else语句

//语法结构:
if(条件1){
        代码(进程)1
}elseif(条件2){
        代码(进程)2
}elseif(条件N){
        代码(进程)N
}else{
        以上条件都不满足,执行这里代码
}

Program flow chart

Example:

/**
 * 60分及格,70分中等,80分良好,90分优秀
     对话框输入一个分数,在页面上输出级别
 */
var score = prompt('请录入分数');
var result;
if(score>=90){  //当分数大于等于90的时候执行这里的代码
    result = '优';
}else if(score>=80){
   //当分数大于等于80并且小于90的时候执行这里的代码
    result = '良';
}else if(score>=60){
    //当分数大于等于60并且小于80的时候执行这里的代码
    result = '及格';
}else{
    //当前面几个条件都不满足的情况下执行这里的代码
    result = '不及格';
}
document.write(result);

5.2.4.switch statement

//语法结构:
switch(状态值){
    case 状态值1:
        code..1
        break;
    case 状态值2:
        code..2
        break;
    case 状态值N:
        code..3
        break;
    default:
        code..4
       break;
       //(放在最后面可以省略,放在前面不可以省略)
}

FIG flow control

Example:

/**
 * 输入1--4分别再对应输出   春夏秋冬  ,其他的输出  “未知季节”
 */
var num = prompt('请输入:');
switch (num){
    case 1:
        //当用户输入1的时候,执行这里的代码
        document.write('春');
        break;
    case 2:
        //当用户输入2的时候,执行这里的代码
        document.write('夏');
        break;
    case 3:
        //当用户输入3的时候,执行这里的代码
        document.write('秋');
        break;
    case 4:
        //当用户输入4的时候执行这里的代码
        document.write('冬');
        break;
    default:
        //以上情况都不满足的时候,执行这里的代码
        document.write('未知季节');
        break;
}

switch features

After 1.swith must be a case or default

/**
 * 输入1--4分别再对应输出   春夏秋冬  ,其他的输出  “未知季节”
 */
var num = prompt('请输入:');
switch (num){
    document.write('错误的写法');//这里写代码会报错
    case 1:
        //当用户输入1的时候,执行这里的代码
        document.write('春');
        break;
    case 2:
        //当用户输入2的时候,执行这里的代码
        document.write('夏');
        break;
    case 3:
        //当用户输入3的时候,执行这里的代码
        document.write('秋');
        break;
    case 4:
        //当用户输入4的时候执行这里的代码
        document.write('冬');
        break;
    default:
        //以上情况都不满足的时候,执行这里的代码
        document.write('未知季节');
        break;
}

2.break can not write, but the back of the case is no longer judge, until it encounters a break or other procedures is finished

/**
 * 输入1--4分别再对应输出   春夏秋冬  ,其他的输出  “未知季节”
 */
var num = prompt('请输入:');
switch (num){
    case 1:
        //当用户输入1的时候,执行这里的代码
        document.write('春');
        //break;  如果这里不要break,用户输入1以后,最后输出等结果会是春和夏,遇到break结束
    case 2:
        //当用户输入2的时候,执行这里的代码
        document.write('夏');
        break;
    case 3:
        //当用户输入3的时候,执行这里的代码
        document.write('秋');
        break;
    case 4:
        //当用户输入4的时候执行这里的代码
        document.write('冬');
        break;
    default:
        //以上情况都不满足的时候,执行这里的代码
        document.write('未知季节');
        break;
}

3.default can appear in any position of the switch, but still no match for the other before the implementation of default

/**
 * 输入1--4分别再对应输出   春夏秋冬  ,其他的输出  “未知季节”
 */
var num = prompt('请输入:');
switch (num){
    default://如果default放在这里,必须加上break
        //以上情况都不满足的时候,执行这里的代码
        document.write('未知季节');
        break;

    case 1:
        //当用户输入1的时候,执行这里的代码
        document.write('春');
        break;
    case 2:
        //当用户输入2的时候,执行这里的代码
        document.write('夏');
        break;
    case 3:
        //当用户输入3的时候,执行这里的代码
        document.write('秋');
        break;
    case 4:
        //当用户输入4的时候执行这里的代码
        document.write('冬');
        break;

}

Summary: Here choose to have several sentence structure completion, can be roughly divided into two categories if statement and the switch statement, if the statement also called conditional branching statements, you know that one when using the if statement, the main scenario is conditions do judgment, it is a main switch statement to make specific reaction under certain conditions, also called the state branch statement, when conditions do not know, but know the state of user input when the switch with the state branch will be better. Written statements above are fixed, we must strictly follow the rules of grammar to write

Screw classroom video lessons Address: http://edu.nodeing.com

Guess you like

Origin www.cnblogs.com/dadifeihong/p/12027566.html