当前位置:网站首页>流程判断-三目运算-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;
}
}
边栏推荐
- 《第五项修炼》(The Fifth Discipline):学习型组织的艺术与实践
- Open source summer 2022 | opengauss project selected and announced
- Online text batch inversion by line tool
- Making single test so simple -- initial experience of Spock framework
- 如何封裝調用一個庫
- 如何实现IM即时通讯“消息”列表卡顿优化
- 信息学奥赛一本通 1333:【例2-2】Blah数集 | OpenJudge NOI 3.4 2729:Blah数集
- Informatics Olympiad 1333: [example 2-2] blah data set | openjudge noi 3.4 2729:blah data set
- Erreur Keil de Huada Single Chip Computer La solution de Weak
- MySQL读取Binlog日志常见错误和解决方法
猜你喜欢

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

Bit. Store: long bear market, stable stacking products may become the main theme

华大单片机KEIL报错_WEAK的解决方案

Technology sharing | introduction to kubernetes pod

A simple calculation method of vanishing point

Teach you how to install Oracle 19C on Windows 10 (detailed picture and text with step on pit Guide)

Two methods of MySQL database login and logout

实战回忆录:从Webshell开始突破边界

Buzzer experiment based on stm32f103zet6 library function

binder hwbinder vndbinder
随机推荐
Market status and development prospect forecast of global functional polyethylene glycol (PEG) industry in 2022
一位平凡毕业生的大学四年
数仓的字符截取三胞胎:substrb、substr、substring
网络传输是怎么工作的 -- 详解 OSI 模型
Solution of adding st-link to Huada MCU Keil
Exporting coordinates of points in TXT format in ArcGIS
Error reported by Huada MCU Keil_ Weak's solution
Photoshop layer related concepts layercomp layers move rotate duplicate layer compound layer
International School of Digital Economics, South China Institute of technology 𞓜 unified Bert for few shot natural language understanding
2022年第一季度消费金融APP用户洞察——总数达4479万人
Current market situation and development prospect forecast of global 3,3 ', 4,4' - biphenyltetracarboxylic dianhydride industry in 2022
Win10 LTSC 2021 wsappx CPU usage high
什么是SSR/SSG/ISR?如何在AWS上托管它们?
Technology sharing | introduction to kubernetes pod
原创 | 2025实现“5个1”奋斗目标!解放动力全系自主非道路国四产品正式发布
过关斩将,擒“指针”(下)
Where to look at high-yield bank financial products?
Market status and development prospect forecast of global active quality control air sampler industry in 2022
Open source summer 2022 | opengauss project selected and announced
华大单片机KEIL添加ST-LINK解决方法