一、流程控制结构:
- 顺序结构:默认:从上向下依次执行每一句话
- 分支结构:通过条件判断,选择部分代码执行
- 循环结构:通过条件判断,选择要不要重复执行某块代码
二、分支结构
- if...else... :
- 一个条件,一件事,满足就做,不满足就不做
if(条件){ 操作; }
- 一个条件,两件事,满足就做第一件,不满足就做第二件
if(条件){ 操作; }else{ 默认操作; }
- 多个条件,多件事,满足谁,就做谁
if(条件1){ 操作1; }else if(条件2){ 操作2; }else{ 默认操作; }
- switch...case:
switch(变量/表达式){
case 值1:
操作1;
break;
case 值2:
操作2;
break;
default:
默认操作;
}
1. case在做比较的时候是不带有隐式转换的
2. default可以省略不写的,但是不推荐,如果不写,条件都不满足的情况,则什么事儿都不会发生
if 和 switch的区别:
-
switch...case:
- 优点:执行效率较高,速度快(他比较时,case做的不是范围查找,而是等值比较)
- 缺点:必须要知道最后的结果是什么才可以使用
-
if...else...:
- 优点:可以做范围判断
- 缺点:执行效率较慢,速度慢(他做的是范围查找)
建议:代码开发完过后,要做代码优化,要尽量的少用if...else...,多用switch...case和三目运算
-
三目运算:简化分支的
语法:
- 条件?操作1:默认操作; === if...else...
- 条件1?操作1:条件2?操作2:默认操作 === if...else if..else
注意:
- 默认操作不能省略,省略了会报错
- 如果操作复杂,不能使用三目运算:操作只能有一句话,如果操作有多句话还是推荐使用switch或if
页面上一切数据js获取到后都是字符串类型
三、强制(显示)数据类型转换:
- 转字符串:2种方法
- var str=x.toString();//x不能undefined和null,会报错,undefined和null不能使用.做任何操作,因为他们不是对象
- var str=String(x);//万能的,任何人都可以转为字符串,但是绝对不要手动使用,完全等效于隐式转换,还不如+""
// .toString()方法 undefined和null不能转 会报错 因为它们不是对象
var num = 11
console.log(num);
console.log(num.toString());
console.log(true);
console.log(true.toString());
console.log(undefined);
// console.log(undefined.toString());//会报错
// console.log(null.toString());//会报错
// String()方法 任何人都可以 还不如+""
console.log(String(2));
console.log(String(true));
console.log(String(null));
console.log(String(undefined));
- 转数字:3种方法
- parseInt(str/num); - parse解析 Int整型 - 专门用于将【字符串转为整数】,执行原理:从左向右依次读取转换每个字符,碰到非数字字符就停止转换,如果一来就不认识则为NaN,不认识小数点。
- parseFloat(str); - parse解析 Float浮点型 - 专门用于将【字符串转为小数】,执行原理:几乎和parseInt一致,认识第一个小数点
- Number(x);万能的,任何人都可以转为数字,但是绝对不要手动使用,完全等效于隐式转换,还不如 -0 *1 /1
// parseInt(str/num) 转整数
console.log(parseInt(20.2));//20
console.log(parseInt("20.2"));//20
console.log(parseInt("20p"));//20
console.log(parseInt("p9"));//NaN
console.log(parseInt(".5"));//NaN
// parseFloat(str) 转小数
console.log(parseFloat(20));//20
console.log(parseFloat(20.65555));//20.65555
console.log(parseFloat("2.3"));//2.3
console.log(parseFloat("2.3p"));//2.3
console.log(parseFloat("o7"));//NaN
console.log(parseFloat(".9"));//0.9
// Number(x) 万能的 不如-0 *1 /1
console.log(Number("8.2"));//8.2
console.log(Number("8.2p"));//NaN
console.log(Number(true));//1
console.log(Number(false));//0
console.log(Number(undefined));//NaN
console.log(Number(null));//0
- 转布尔:
- Boolean(x);万能的,任何人都可以转为布尔,但是绝对不要手动使用,完全等效于隐式转换,还不如 !!x
- 只有6个为false:0,"",undefined,NaN,null,false,其余全部都是true。
- 在分支或循环的条件之中,不管以后写什么,他都会悄悄的转为一个布尔值,自带此方法
console.log(Boolean(1));
// 只有以下六种情况为false 其余都为true
console.log(Boolean(0));
console.log(Boolean(""));
console.log(Boolean(null));
console.log(Boolean(undefined));
console.log(Boolean(false));
console.log(Boolean(NaN));
四、案例
// 判断不同分数
var score = prompt("请输入分数:")
if (!isNaN(score)) {
if (score <= 100 && score >= 0) {
if (score >= 90) {
alert("大于等于90")
} else if (score >= 80) {
alert("大于等于80")
} else if (score >= 70) {
alert("大于等于70")
} else if (score >= 60) {
alert("大于等于60")
} else {
alert("不及格")
}
} else {
alert("请输入0-100的数字!")
}
} else {
alert("请输入数字!")
}
// 计算BMI标准体重
var weight = prompt("请输入体重:(kg)")
var height = prompt("请输入身高:(m)")
var BMI = weight/(height*height)
if (!isNaN(weight) && !isNaN(height)) {
if (BMI > 30) {
alert("肥胖")
} else if (BMI > 25) {
alert("超重")
} else if (BMI < 20) {
alert("偏廋")
} else {
alert("正常")
}
} else {
alert("恶意输入!")
}
console.log(weight);
// 计算应缴纳的税 方法一
var salary = prompt("请输入工资:")
if (!isNaN(salary)) {
var s = 0
if (salary >= 10000) {
s += (salary - 10000)*0.1
salary = 10000
}
if (salary >= 5000) {
s += (salary - 5000)*0.05
salary = 5000
}
if (salary >= 3500) {
s += (salary - 3500)*0.025
salary = 3500
}
alert("应缴纳的税为:" + s)
} else {
alert("恶意输入!")
}
// 方法二
/* if (salary >= 10000) {
s = (salary - 10000)*0.1 + (10000 - 5000)*0.05 + (5000 - 3500)*0.025
alert("应缴纳的税为:" + s)
} else if (salary >= 5000) {
s = (salary - 5000)*0.05 + (5000 - 3500)*0.025
alert("应缴纳的税为:" + s)
} else if (salary >= 3500) {
s = (salary - 3500)*0.025
alert("应缴纳的税为:" + s)
} */
// 用户输入年月,判断每月有多少天 方法一
var year = parseInt(prompt("请输入年份:"))
var month = parseInt(prompt("请输入月份:"))
/* if (!isNaN(year) && !isNaN(monyh)) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
alert("31天")
} else if (month == 4 || month == 6 || month == 9 || month == 11) {
alert("30天")
} else if (month == 2) {
if ((year%4==0 && year%100!=0) || (year%400==0)) {
alert("29天")
} else {
alert ("28天")
}
}
} else {
alert("恶意输入!")
} */
// 方法二
switch(month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
alert("31天")
break
case 4:
case 6:
case 9:
case 11:
alert("30天")
break
case 2:
switch((year%4==0&&year%100!=0) || (year%400==0)) {
case true:
alert("29天")
break
case false:
alert("28天")
}
}