当前位置:网站首页>2022.7.30 js notes Operators and flow controllers, loops
2022.7.30 js notes Operators and flow controllers, loops
2022-08-02 03:16:00 【The secret to longevity is sleep.】
一、自增、自减,比较,逻辑
1.自增、自减
前置操作(自减同理):The expression is executed first
var a = 1;
b = 1 + ++a;
console.log(b); // 3
后置操作(自减同理):Executed at the end of the expression
var a = 1;
var b = 1 + a++;
console.log(b); // 2
2.比较运算符
(类型等于)== :强制类型转换,will coerce the values on both sides to 数字或布尔值 类型再进行比较.
(全等于) === :Non-coercive type conversion,Compare the values on both sides without converting them.
3.逻辑运算符
逻辑与&& :当左边为 false Returns the current value immediately,当左边为 true Returns the right-hand value immediately.
逻辑或|| :When there is one on both sides true 立即返回 true ,反之返回 false .
逻辑非! :返回相反值.
优先级:&&优先级最高,So prioritize,The way to increase the priority is to add ().
二、if、if/else、三元表达式
1.if 用法
var person = "boy";
if (person === "girl") {
console.log("hi,girl");
}
console.log("hi,boy");
如果只有一条代码块,可以不用写{}.
2.if/else 用法
var person = "boy";
if (person === "girl") {
console.log("hi,girl");
} else {
console.log("hi,boy");
}
3.三元表达式(非真即假)
var person = "boy";
person === "girl" ? console.log("girl") : console.log("boy");
三、swith 用法
var weather = "下雪";
switch (weather) {
case "晴朗": //case 后的值和 swith() The value contrast is all equal
console.log("去打篮球");
break;
case "下雨":
console.log("收衣服");
break;
case "下雪":
console.log("堆雪人");
break;
default:
console.log("学习");
break; //加不加都行
}
有3,4when the above conditions are met,You can consider using it⽤ switch .
四、for 循环
for(语句1;语句2;语句3){
The block of code to repeat
}
// 语句1声明一个变量
// 语句2Specifies the loop breakout condition
// 语句3Control variable changes
流程:
语句1在循环(代码块)开始前执行
语句2Defines the end loop(代码块)的一个条件
语句3会在循环(代码块)运行完成后执行
遍历数组:
var sum = ["a", "b", "c", "d", "e"];
for (let s = 0; s < sum.length; s++) {
console.log(sum[s]);
}
// a b c d e
计算:
var sum = 0;
for (let a = 0; a < 10; a++) {
sum += a;
}
console.log(sum);
五、while 循环(不常用)
1. while 用法:
var a = 1;
var sum = 0;
while (a < 10) {
sum += a;
a++;
}
console.log(sum);
2. do while 用法(Guaranteed to cycle at least once)
var a = 1;
var sum = 0;
do {
sum += a;
a++;
} while (a < 10);
console.log(sum);
六、练习
1.打印1-100之间7的倍数的个数及总和
var sum = 0;
var t = 0;
for (let a = 1; a <= 100; a++) {
if (a % 7 === 0) {
t++;
sum += a;
}
}
console.log("个数:", t, "总和", sum);
2.使⽤for循环输出⼀下图形
*
* *
* * *
* * * *
* * * * *
for (let a = 0; a < 5; a++) {
for (let b = 0; b < a + 1; b++) {
document.write("* ");
}
document.write("</br>");
}
边栏推荐
猜你喜欢
随机推荐
分布式事务解决方案模型
JSP Webshell free kill
ModuleNotFoundError: No module named ‘openpyxl‘
蓝鲸DevOps荣获平台类工具企业选择率第一
深度学习:目标检测入门知识
深度自编码网络的集成学习ICPS入侵检测模型
MySQL8.0.26 installation and configuration tutorial (windows 64-bit)
EF Core:基于关系的复杂查询 区分IEnumerable和IQueryable
5.nodejs--cross domain, CORS, JSONP, Proxy
(转帖)HashCode总结(1)
MySQL8.0.28安装教程
弹性盒子flex属性
线性代数学习笔记1:何为线性代数
CV-Model【4】:MobileNet v3
(Reposted) The relationship between hashcode and equals
线性代数学习笔记2-2:向量空间、子空间、最大无关组、基、秩与空间维数
01-Node-Express系统框架搭建(express-generator)
Hit the programmer interview scene: What did Baidu interviewers ask me?
JunitTest单元测试
OD-Model [4]: SSD