A triplet of expressions if elseif case column switch case breank case columns

grammar:

Conditional expression? Value 1: 2 value;

 

When the conditional expression is true, ternary expressions result is the value 1;

When the conditional expression is false, ternary expressions result is the value 2;

When the result is two ternary expressions of alternatives are easier.

When less than one year work experience, wages of less than 8000, year-end awards to 1 times salary, otherwise it is 1.2 times;

When less than 2 years work experience, salary less than 10,000, year-end awards is 1.5 times wages, otherwise it is 1.7 times;

When work experience equals more than two years, wages of less than 13,000, year-end awards is 2.3 times wages, otherwise it is 3 times;

 

User input age and salary, and told him the corresponding year-end awards

1	// 的得到年限,0。9年 取整0
2	var year = parseInt(prompt("请输入工作年限"));
3	// 工资
4	var salary = parseInt(prompt("请输入工资"));
5	// 倍数
6	var beishu = 0;
7	
8	// 外层 使用多分支if语句,根据年限划分条件
9	if (year === 0) {
10		// 倍数1,1.2
11		if (salary < 8000) {
12			beishu = 1;
13		}else {
14			beishu = 1.2
15		}
16	}else if (year === 1) {
17		beishu = salary < 10000 ? 1.5 : 1.7;
18	}else if (year >= 2) {
19		beishu = salary >= 13000 ? 3 : 2.3 ;
20	}
21	
22	
23	// 输出,字符串拼接
24	console.log("该用户工作年限是" + year + "年,工资是" + salary + "元,对应的年终奖是" + (beishu * salary)  + "元。");

nested if statements

1	/*
2	男性大于等于22岁可以结婚;女性大于等于20可以结婚
3	用户输入年龄,性别判断是否可以结婚
4	*/
5	// 性别
6	var sex = prompt("请输入性别");
7	// 年龄
8	var age = parseInt(prompt("请输入年龄"));
9	
10	// 外层使用性别
11	if (sex === "男性") {
12		if (age >= 22) {
13			// 该结构体执行: 同时满足两个条件 sex===男性 age >= 22
14			console.log("该男性可以结婚");
15		}else {
16			// age <22
17			console.log("该男性不可以结婚");
18		}
19		console.log("该用户是男性");
20	}else if (sex === "女性") {
21		if (age >= 20) {
22			console.log("女性可以结婚");
23		}else {
24			console.log("女性不可以结婚");
25		}
26		console.log("女性查询完毕");
27	}
28	
29	console.log("查询结束");

switch statement

  1. switch (expression) {
  2.    case value 1:
  3.         1 performs;
  4.         break;
  5.    case value of 2:
  6.         2 is performed;
  7.         break;
  8.    case value of 3:
  9.        Implementation of body;
  10.        break;
  11.     ……
  12.     default:
  13.        Implementation of body;
  14.         break;

}

1	// 用户输入星座,输出对应的运势
2	// 获取星座
3	var xingzuo = prompt("请输入星座");
4	switch (xingzuo) {
5		case "白羊座":
6			console.log("白羊座");
7			console.log("白羊座今日运势普通,注意处理好自己的负面情绪,不要让它影响你的生活、工作。感情方面运势一般,你可能会希望得到伴侣更多地关注和关心,但你传递给他/她很大地压力感让人");
8			break;
9		case "金牛座":
10			console.log("金牛座");
11			console.log("金牛座今日运势稍弱,遇事不要慌张,越紧张头脑越不清楚,会影响你的判断力。感情方面运势一般,伴侣的一个无心举动都可能会惹得你不高兴,情绪不佳时不妨独处一会儿。");
12			break;
13		case "双子座":
14			console.log("双子座");
15			console.log("双子座今日运势较好,抗压能力强,面对困难不会轻易气馁的你适合尝试完成高难度的事情。");
16			break;
17		default:
18			console.log("没有你输入的星座换一个吧");
19			break;
20	}
21	
22	console.log("查询结束");

 

break: break statement to perform matching case back, no longer continue down the back of the statement, the switch statement immediately jump out perform other behind the switch statement.

default: When the above case did not match that follows the default statement.

The default statement break may be omitted, when the rear end of the execution of the default statement will jump out of the switch statement

 

 

利用break案例:
1	/*
2	1,3,5,7,8,10,12   31
3	4,6,9,11          30
4	2				  28
5	*/
6	// 获取月份
7	var month = parseInt(prompt("请输入月份"));
8	switch (month) {
9		case 2:
10			console.log(month + "月有28天");
11			break;
12		case 4:
13		case 6:
14		case 9:
15		case 11:
16			console.log(month + "月有30天");
17			break;
18		default:
19			console.log(month + "月有31天");
20	}
21	console.log("查询结束");
另类写法:
switch后面直接书写true,匹配case 后面书写的条件表达式
1	//分数案例
2	// 的到分数
3	var score = parseInt(prompt("请输入分数"));
4	// 计算机遇到所有的表达式,都是先计算
5	switch (true) {
6		case score >= 90:
7			console.log("优秀");
8			break;
9		case score >= 80:
10			console.log("良好");
11			break;
12		case score >= 70:
13			console.log("还行");
14			break;
15		default: 
16			console.log("加油,超越自己");
17	}
18	console.log("查询结束");

 

Guess you like

Origin blog.csdn.net/qq_41328247/article/details/88782908