当前位置:网站首页>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>");
}边栏推荐
- rem适配
- Foundry教程:使用多种方式编写可升级的智能代理合约(下)
- JDBC的入门使用
- 第一章——线性表(顺序表和链表)
- 三维数字孪生引擎与实景互动,案例解析
- "Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
- MySQL8.0.28 installation tutorial
- 7-42 整型关键字的散列映射 (25 分)
- I will give you a chance to interview in a big factory. Can you interview?Come in and see!
- 第 304 场力扣周赛
猜你喜欢
随机推荐
2022年最新一篇文章教你青龙面板拉库,拉取单文件,安装依赖,设置环境变量,解决没有或丢失依赖can‘t find module之保姆教程(附带几十个青龙面板脚本仓库)
第 304 场力扣周赛
运维理想和现实,你是?
MySQL中的时区设置
JDBC的入门使用
(转帖)HashCode总结(1)
Invalid bound statement (not found)出现的原因和解决方法
5.nodejs--跨域、CORS、JSONP 、Proxy
R16 Type II量化反馈码本的产生
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
(forwarded) HashCode summary (2)
PHP WebSehll 后门脚本与检测工具
ModuleNotFoundError: No module named ‘openpyxl‘
#{}和${}的区别
线性代数学习笔记3-1:矩阵与线性变换、常见矩阵(逆矩阵、伴随矩阵、正交矩阵等)
(转帖)HashCode总结(2)
Go语学习笔记 - gorm使用 - 原生sql、命名参数、Rows、ToSQL Web框架Gin(九)
面试必备!TCP协议经典十五连问!
OD-Model【4】:SSD
线性代数学习笔记3-3:逆矩阵的理解








