当前位置:网站首页>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>");
}
边栏推荐
- CV-Model [4]: MobileNet v3
- 嘉为蓝鲸携手东风集团、上汽零束再获信通院四项大奖
- LeetCode:746. 使用最小花费爬楼梯【动态规划】
- 给你一个大厂面试的机会,你能面试上吗?进来看看!
- 考虑饱和的多智能体系统数据驱动双向一致性
- 【LeetCode】1374. Generate a string with an odd number of each character
- “带薪划水”偷刷阿里老哥的面经宝典,三次挑战字节,终成正果
- 咨询cdc for oracle,增量同步scan.startup.mode只有initial和la
- 弹性盒子flex属性
- MySQL8 - use under Windows package installation method
猜你喜欢
随机推荐
MySQL8.0.28安装教程
蓝鲸DevOps荣获平台类工具企业选择率第一
PHP WebShell Free Kill
TRICK第二弹
聊聊flink的BoundedOutOfOrdernessTimestampExtractor
Difference between #{} and ${}
LeetCode:746. 使用最小花费爬楼梯【动态规划】
MySQL中的时间函数
MySQL中根据日期进行范围查询
【LeetCode】1374. Generate a string with an odd number of each character
just write blindly = feelings
MySQL8 -- use msi (graphical user interface) under Windows installation method
Day34 LeetCode
"Paid paddling" stealthily brushes Brother Ali's face scriptures, challenges bytes three times, and finally achieves positive results
2W字!梳理50道经典计算机网络面试题(收藏版)
直击程序员面试现场:百度面试官都问了我些啥?
5.合宙Air32F103_LCD_key
CV-Model [4]: MobileNet v3
Good Key, Bad Key (思维,临项交换,经典方法)
基于优化的多核局部费舍尔判别分析的故障分类