当前位置:网站首页>流程判断-三目运算-for循环
流程判断-三目运算-for循环
2022-06-27 17:34:00 【编程奥特曼.】
流程控制:决定逻辑运行的走向 根据条件做出不同的反应
if else if else if else
let a = 59
if(a > 80){
// 如果判断条件为 true 就会运行 {里面的逻辑}
console.log("当前为真");
}else if(a <= 80 && a >= 60){
//去真留假 都得是真 才会返回真 &&取交集部分
console.log("当前为假");
}else{
console.log("成绩不及格");
}
let c = 50
if(c > 80 || c < 60){
//或 去假留真 一个是真即可 ||取不是交集部分
console.log(1);
}
// 只有一个条件 分支内只有一条语句时
if(true)console.log(2);
switch判断变量是否相等,不适用于判断范围 case语句: break:跳出判断
let person = "雀雀1" //判断夏栀老师 漂亮 沐沐 可爱 雀雀 聪明贤惠善良美丽 路遥 帅气
if (person === "夏栀") {
console.log("夏栀真漂亮");
} else if (person === "沐沐") {
console.log(person + "沐沐真可爱啊");
} else if (person === "路遥") {
console.log(person + "路遥真帅气");
} else {
console.log(person + "雀雀聪明贤惠善良美丽");
}
// 判断变量相同 是否相等 不适用于范围判断 case:语句 break 跳出判断
let age = "18"
switch (person) {
//()里面放 判断的东西
case "夏栀":
console.log("夏栀真漂亮");
break; // 手动的跳出去
case "沐沐":
console.log(person + "沐沐真可爱啊");
break;
case "路遥":
console.log(person + "路遥真帅气");
break;
case "雀雀":
console.log(person + "雀雀聪明贤惠善良美丽");
break;
default:
// console.log("不满足上面的条件就会走向我");
switch(age){
case "16":
console.log("16岁了");
break;
case "18":
console.log("18岁了");
break;
default:
console.log("不知道多少岁");
break;
}
}
三目运算
判断条件 ? 真 : 假
仅有两条的分支,且分支中仅有简单的逻辑
let x = 1;
let a;
if(x > 0){
//true
a = x
}else{
//false
a = 0
}
x > 0 ? a = x : a = 0
a = x > 0 ? x : 0;
x > 0 ? console.log(x) : console.log(0);
console.log(a);
流程练习
/* 年龄 年龄小于11岁 青铜小学生 年龄 12~15 白银初中生 年龄 16~18 黄金高中生 18~21 荣耀大学生 22+ 社会我大哥 typeof 返回的值 "number" 输出 这是一个字符串类型 "string" 输出 这是一个字符串类型 "boolean" "undefined" "object" 年龄 小于18岁 输出未成年禁止进入 等于大于18岁 输出 我有个朋友想问问你有没有>>> 判断 一个变量 是否赋值 如果赋值了 则不变 如果未赋值 就让他的值等于 [] */
// 第一题
let age = 18;
if(age <=11){
console.log("小学生");
}else if(age >= 12 && age <= 14){
console.log("白银小学生");
}else if(age >= 15 && age <= 17){
console.log("高中生");
}else if(age >= 18 && age <= 21){
console.log("大学生");
}else{
console.log("社会我大哥");
}
// 第二题
let info = typeof "1";
switch(info){
case "number":
console.log("这是一个数值类型");
break;
case "string":
console.log("这是一个字符串类型");
break;
case "boolean":
console.log("这是一个boolean");
break;
case "undefined":
console.log("这是一个undefined");
break;
case "object":
console.log("这是一个object类型");
break;
}
// 第三题
let age = 14
if(age < 18){
console.log("输出未成年禁止进入");
}else if(age > 18){
console.log("我有个朋友想问问你有没有");
}
// 三目
let age = 20
age > 18 ? console.log("我有个朋友想问问你有没有") : console.log("输出未成年禁止进入");
// 第四题
let num = 1; //如果是undefined 则是真
num = num === undefined ? [] : num
console.log(num);
for循环 重复做某件次数
for循环内有三条语句
声明变量:声明一个变量,并初始化值,用来计次
判断条件:条件查询,绝对循环的次数,结束条件,条件为真,则继续执行,条件为假,则结束循环
变化语句:计量变量,递增多少,递减多少
for(声明变量1;判断条件2;变化语句3){
代码内4
}
先后顺序是:先1 => 2 => 4(进行第一次循环) => 3
2 => 4 => 3
2 => 4 => 3
最后2不满足条件 则结束循环
break和continue的区别
break;跳出循环;
continue;只跳出本次循环,继续下次循环
// break
for(let index = 1;index <= 100;index++){
// console.log(1);
console.log(index);
// index == 50 停止
if(index == 50){
console.log("50次了不能继续使用了");
break;
}
}
// continue
for(let index = 1;index <= 100;index++){
if(index == 50){
console.log("第 50 次跳过");
continue;
}
console.log(index);
}
// 嵌套for 循环
for(let i = 1; i <= 10; i++){
for(let b = 1; b < 10; b++){
if(b == 5){
// break;
continue; //只会跳出第五列的循环
}
console.log(`这是第${
i}行 第${
b}列`);
}
}
// 给for循环 标记一个名字
lable1:for (let i = 1; i <= 10; i++) {
for (let b = 1; b < 10; b++) {
if (b == 5) {
break lable1; //跳出对应的for循环
}
console.log(`这是第${
i}行 第${
b}列`);
}
}
一张纸的厚度 1mm 珠穆拉玛峰高度 是 8848m 请问这张纸需要折叠多少次 才能超过珠穆拉玛峰的高度?
let num = 1
let height = 8848000
for(let i = 1; i < 100; i++){
num *= 2
if(num > height){
// 折叠次数
console.log(i);
// 当前折叠的高度
console.log(num);
break;
}
}
边栏推荐
- 电脑安全证书错误怎么处理比较好
- Camera calibration with OpenCV
- 脉脉热帖:为啥大厂都热衷于造轮子?
- 金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
- Hi,你有一份Code Review攻略待查收!
- Stored procedures of PostgreSQL
- Making single test so simple -- initial experience of Spock framework
- Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders
- Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
- Online text batch inversion by line tool
猜你喜欢

Google Earth Engine(GEE)——ImageCollection (Error)遍历影像集合产生的错误

Solution of adding st-link to Huada MCU Keil

OpenSSL client programming: SSL session failure caused by an obscure function

国际数字经济学院、华南理工 | Unified BERT for Few-shot Natural Language Understanding(用于小样本自然语言理解的统一BERT)

Industry university cooperation cooperates to educate people, and Kirin software cooperates with Nankai University to complete the practical course of software testing and maintenance

Vscode suggests that you enable gopls. What exactly is it?

别焦虑了,这才是中国各行业的工资真相

Bit.Store:熊市漫漫,稳定Staking产品或成主旋律

金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东

Usage of rxjs mergemap
随机推荐
图扑数字孪生智慧能源一体化管控平台
Market status and development prospect forecast of global handheld ventilator industry in 2022
Common errors and solutions of MySQL reading binlog logs
Running lantern experiment based on stm32f103zet6 library function
[elt.zip] openharmony paper Club - memory compression for data intensive applications
实战回忆录:从Webshell开始突破边界
Comment encapsuler un appel à une bibliothèque
金源高端IPO被终止:曾拟募资7.5亿 儒杉资产与溧阳产投是股东
华大单片机KEIL添加ST-LINK解决方法
華大單片機KEIL報錯_WEAK的解决方案
Gartner聚焦中国低代码发展 UniPro如何践行“差异化”
信息学奥赛一本通 1335:【例2-4】连通块
Don't worry. This is the truth about wages in all industries in China
Hi,你有一份Code Review攻略待查收!
Core dynamic Lianke rushes to the scientific innovation board: with an annual revenue of 170million yuan, Beifang Electronics Institute and Zhongcheng venture capital are shareholders
NVIDIA Clara-AGX-Developer-Kit installation
openssl客户端编程:一个不起眼的函数导致的SSL会话失败问题
Market status and development prospect forecast of global 4-methyl-2-pentanone industry in 2022
Add in address of idea official website
Hikvision tools manager Hikvision tools collection (including sadp, video capacity calculation and other tools) a practical tool for millions of security practitioners