04- JavaScript flow control statements: selection structure (if and switch)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-NC-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Yuanriver/article/details/102535999

Block

With {}surrounded code is the code block.

JS code block, only a packet effect, no other use.

Content code block, is fully visible on the outside. For example:

  {
      var a = 2;
      alert("smyhvae");
      console.log("永不止步");
  }
  console.log("a = " + a);

:( print result can be seen, although the variable is defined in a block of code, but still can be accessed externally)

永不止步
a = 2

Flow control statements

During a program execution, the order of execution of the statement of the program is the result of a direct impact. So, we have to understand the flow of execution for each statement. And, many times we want to achieve our functions to be performed by the execution order control statements.

Flow control statements Classification

  • Sequence Structure

  • Select structure: if statements, switch statements

  • Looping constructs: while statement, for statement

Sequence Structure

According to the order code, followed by the implementation. Structure is as follows:

Here Insert Picture Description

The if statement

if the following three statements.

1, conditional statement

Conditions are met before execution. If the condition is not satisfied, then do nothing.

format:

	if (条件表达式) {
		// 条件为真时,做的事情
	}

2, conditional branching statements

Format 1:

	if (条件表达式) {
		// 条件为真时,做的事情
	} else {
		// 条件为假时,做的事情
	}

:( format multi-branch if statement)

	if (条件表达式1) {
		// 条件1为真时,做的事情
	} else if (条件表达式2) {
		// 条件1不满足,条件2满足时,做的事情
	} else if (条件表达式3) {
		// 条件1、2不满足,条件3满足时,做的事情
	} else {
		// 条件1、2、3都不满足时,做的事情
	}

All of the above statements in the body, only one execution.

Be subject

	根据BMI(身体质量指数)显示一个人的体型。
	BMI指数,就是体重、身高的一个计算公式。公式是:
	BMI =体重÷身高的平方

	比如,老师的体重是81.6公斤,身高是1.71米。
	那么老师的BMI就是  81.6 ÷ 1.712     等于 27.906022365856163

	过轻:低于18.5
	正常:18.5-24.99999999
	过重:25-27.9999999
	肥胖:28-32
	非常肥胖, 高于32

	用JavaScript开发一个程序,让用户先输入自己的体重,然后输入自己的身高(弹出两次prompt框)。
	计算它的BMI,根据上表,弹出用户的身体情况。比如“过轻” 、 “正常” 、“过重” 、 “肥胖” 、“非常肥胖”。

The answer :

Written 1:

	//第一步,输入身高和体重
	var height = parseFloat(prompt("请输入身高,单位是米"));
	var weight = parseFloat(prompt("请输入体重,单位是公斤"));
	//第二步,计算BMI指数
	var BMI = weight / Math.pow(height, 2);
	//第三步,if语句来判断。注意跳楼现象
	if (BMI < 18.5) {
		alert("偏瘦");
	} else if (BMI < 25) {
		alert("正常");
	} else if (BMI < 28) {
		alert("过重");
	} else if (BMI <= 32) {
		alert("肥胖");
	} else {
		alert("非常肥胖");
	}

2 writing:

	//第一步,输入身高和体重
	var height = parseFloat(prompt("请输入身高,单位是米"));
	var weight = parseFloat(prompt("请输入体重,单位是公斤"));
	//第二步,计算BMI指数
	var BMI = weight / Math.pow(height, 2);
	//第三步,if语句来判断。注意跳楼现象
	if (BMI > 32) {
		alert("非常肥胖");
	} else if (BMI >= 28) {
		alert("肥胖");
	} else if (BMI >= 25) {
		alert("过重");
	} else if (BMI >= 18.5) {
		alert("正常")
	} else {
		alert("偏瘦");
	}

Nested if statements

We nested if statements to elicit the following example.

	一个加油站为了鼓励车主多加油,所以加的多有优惠。
	92号汽油,每升6元;如果大于等于20升,那么每升5.9;
	97号汽油,每升7元;如果大于等于30升,那么每升6.95
	编写JS程序,用户输入自己的汽油编号,然后输入自己加多少升,弹出价格。

Here Insert Picture Description

Code is implemented as follows:

	//第一步,输入
	var bianhao = parseInt(prompt("您想加什么油?填写92或者97"));
	var sheng = parseFloat(prompt("您想加多少升?"));

	//第二步,判断
	if (bianhao == 92) {
		//编号是92的时候做的事情
		if (sheng >= 20) {
			var price = sheng * 5.9;
		} else {
			var price = sheng * 6;
		}
	} else if (bianhao == 97) {
		//编号是97的时候做的事情
		if (sheng >= 30) {
			var price = sheng * 6.95;
		} else {
			var price = sheng * 7;
		}
	} else {
		alert("对不起,没有这个编号的汽油!");
	}
	alert("价格是" + price);

The switch statement (conditional branch statement)

The switch statement also called conditional branching statements.

format:

switch(表达式) {
	case 值1:
		语句体1;
		break;
	case 值2:
		语句体2;
		break;
	...
	...
	default:
		语句体 n+1;
		break;
}

Execution flow switch statement

Flowchart is as follows:

Here Insert Picture Description

The implementation process is as follows:

(1) First, the calculated value of the expression, and sequence comparison case, once the value corresponding to the corresponding statement will be executed, during the execution, encounters a break will end.

(2) Then, if the case were all expressions and values ​​do not match, it will execute the default statement body part.

End condition of a switch statement [very important]

  • Case a: encountered break is over, rather than face default on end. (Because the break in the action here is to exit the switch statement)

  • Case b: execution to the end of the program ends.

We look at the following two examples to understand.

case penetrating questions

switch statement breakcan be omitted, but generally not recommended. Otherwise the result may not be what you want, there will be a phenomenon: Case penetration .

Example 1 : (case where penetration)

    var num = 4;

    //switch判断语句
    switch (num) {
        case 1:
            console.log("星期一");
            break;
        case 2:
            console.log("星期二");
            break;
        case 3:
            console.log("星期三");
            break;
        case 4:
            console.log("星期四");
        //break;
        case 5:
            console.log("星期五");
        //break;
        case 6:
            console.log("星期六");
            break;
        case 7:
            console.log("星期日");
            break;
        default:
            console.log("你输入的数据有误");
            break;
    }

Run the code above the results might surprise you:

星期四
星期五
星期六

Explained above code: Because in case 4 and the case 5 are no break, that statement went case break 6 for them to stop.

Example 2 :

    //switch判断语句
    var number = 5;

    switch (number) {
        default:
            console.log("我是defaul语句");
            // break;
        case (2):
          	console.log("第二个呵呵:" + number);
            //break;
        case (3):
          	console.log("第三个呵呵:" + number);
            break;
        case (4):
          	console.log("第四个呵呵:" + number);
            break;
    }

The results of running the code above, you might accidentally:

我是defaul语句
第二个呵呵:5
第三个呵呵:5

Code above explanations: Code Reached default, because there is no encounter break, it will continue to go down, or come to the end until I met break program. From this example we can see: the end of the sequence has nothing to do with the default switch statement.

Guess you like

Origin blog.csdn.net/Yuanriver/article/details/102535999