当前位置:网站首页>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>");
}边栏推荐
- OD-Model【4】:SSD
- MySQL8.0.26 installation and configuration tutorial (windows 64-bit)
- I will give you a chance to interview in a big factory. Can you interview?Come in and see!
- Hit the programmer interview scene: What did Baidu interviewers ask me?
- WebShell连接工具(中国菜刀、WeBaCoo、Weevely)使用
- 5.nodejs--cross domain, CORS, JSONP, Proxy
- 基于分布式随机森林的火电厂燃烧系统设备建模方法
- MySQL8--Windows下使用msi(图形界面)安装的方法
- 2W字!详解20道Redis经典面试题!(珍藏版)
- MySQL8.0.28安装教程
猜你喜欢

黑马案例--实现 clock 时钟的web服务器

IPIDEA的使用方式

Go语学习笔记 - gorm使用 - 事务操作 Web框架Gin(十一)

线性代数学习笔记3-1:矩阵与线性变换、常见矩阵(逆矩阵、伴随矩阵、正交矩阵等)
![CV-Model [4]: MobileNet v3](/img/a1/fc3901d55b28aa080235f093b94cb4.png)
CV-Model [4]: MobileNet v3

5. Hezhou Air32F103_LCD_key

关于跨域问题

STM32——LCD—TFTLCD原理与配置介绍

Hit the programmer interview scene: What did Baidu interviewers ask me?

ModuleNotFoundError: No module named ‘openpyxl‘
随机推荐
OD-Model [4]: SSD
LeetCode:1374. 生成每种字符都是奇数个的字符串【签到题】
脚手架安装
输入延迟切换系统的预测镇定控制
【LeetCode】20. Valid parentheses
Kubernetes 基本概念
深度自编码网络的集成学习ICPS入侵检测模型
Invalid bound statement (not found)出现的原因和解决方法
DOM破坏及复现实验
浏览器的工作原理(dns域名服务器,tcp握手,ssl/tls安全协议,关键渲染路径,重绘及回流,防抖和节流)
MySQL8--Windows下使用压缩包安装的方法
JSP WebSehll backdoor script
一种基于行为空间的回声状态网络参数优化方法
5. Hezhou Air32F103_LCD_key
7、MySQL Workbench 导出导入数据库
第七周复习
Istio微服务治理网格的全方面可视化监控(微服务架构展示、资源监控、流量监控、链路监控)
【LeetCode】1374. Generate a string with an odd number of each character
Hit the programmer interview scene: What did Baidu interviewers ask me?
基于时延估计的动力型下肢假肢分段控制策略研究