ES6 条件语句
ES6 条件语句
ES6 条件语句用于执行不同的操作基于各种条件。条件语句在执行指令之前评估条件。
在编写代码时,您需要针对不同的决策执行不同的操作。您可以使用条件语句轻松地执行它。
条件语句的类型
JavaScript 中的条件语句如下:
if 语句
if....else 语句
if....else if....statement
嵌套 if 语句
switch 语句
让我们试着详细说明这些条件语句。
if 语句
这是最简单的决策语句之一用于决定在某个条件为真时是否执行一段 JavaScript 代码。
语法
if (condition) {
// block of code will execute if the condition is true
}
如果条件为真,if 语句中的代码将执行,但如果条件为假,则if 语句结束后的代码(结束后)花括号) 将执行。
注意: if 语句必须以小写字母书写。使用大写字母(If 或 IF)会导致 JavaScript 错误。
流程图
例如
var x = 78;
if (x>70) {
console.log("x is greater")
}
输出
if....else 语句
if....else 语句 包括两个块,分别是 if 块 和else 块。 这是控制语句的下一种形式,它允许以更可控的方式执行 JavaScript。当您需要检查两个不同的条件并执行一组不同的代码时使用它。 else 语句用于指定在条件为假时执行代码块。
语法
if (condition)
{
// block of code will execute if the condition is true
}
else
{
// block of code will execute if the condition is false
}
如果条件为真,则if块中的语句将被执行,但如果条件为假,则else块中的语句将被执行
流程图
例如
让我们试着通过下面的例子来理解if....else语句:
var x = 40, y=20;
if (x < y)
{
console.log("y is greater");
}
else
{
console.log("x is greater");
}
输出
if....else if.....else 语句
用于测试多个条件。 if 语句可以有多个或零个 else if 语句,并且必须在使用 else 语句之前使用它们。您应该始终牢记else 语句 必须在 else if 语句之后。
语法
if (condition1)
{
// block of code will execute if condition1 is true
}
else if (condition2)
{
// block of code will execute if the condition1 is false and condition2 is true
}
else
{
// block of code will execute if the condition1 is false and condition2 is false
}
示例
var a = 10, b = 20, c = 30;
if( a > b && a > c) {
console.log("a is greater");
} else if( b > a && b > c ) {
console.log("b is greater");
} else {
console.log("c is greater");
}
输出
嵌套的 if 语句
它是 if 语句中的 if 语句。
语法
if (condition1)
{
Statement 1; //It will execute when condition1 is true
if (condition2)
{
Statement 2; //It will execute when condition2 is true
}
else
{
Statement 3; //It will execute when condition2 is false
}
}
示例
var num = 20;
if (num > 10)
{
if (num%2==0)
console.log( num+ " is greater than 10 and even number");
else
console.log(num+ " is greater than 10 and odd number");
}
else
{
console.log(num+" is smaller than 10");
}
console.log("After nested if statement");
输出
20 is greater than 10 and even number
After nested if statement
switch 语句
它是一个多路分支语句,也用于决策目的。在某些情况下,switch 语句 比 if-else 语句更方便。它主要用于所有分支都依赖于单个变量的值的情况。它根据不同的情况执行一段代码。
switch 语句 使用 break 或 default 关键字,但它们都是可选的。让我们定义这两个关键字:
break: 它在 switch 语句 中用于终止语句的序列。使用是可选的。如果它被省略,那么每个语句都会继续执行。当它被使用时,它将停止块内的执行。
default: 它指定一些代码在没有大小写匹配时运行。开关中只能有一个默认关键字。它也是可选的,但建议使用它,因为它可以处理意外情况。
如果传递给switch的条件与case中的任何值都不匹配,则执行default下的语句。
几点记住
一个 switch 表达式可以有一个或多个 case 值。
break 和 default 关键字的使用是可选的。
case 语句只能包含常量和文字。它不能是表达式或变量。
除非您在每个块的代码后放置一个中断,否则执行将不断流入下一个块。
没有必要最后将 default case 放在 switch 块中。
语法
switch(expression){
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......
default:
code to be executed if all cases are not matched;
}
流程图
示例
var num = 5;
switch(num) {
case 0 : {
console.log("Sunday");
break;
}
case 1 : {
console.log("Monday");
break;
}
case 2 : {
console.log("Tuesday");
break;
}
case 3 : {
console.log("Wednesday");
break;
}
case 4 : {
console.log("Thursday");
break;
}
case 5 : {
console.log("Friday");
break;
}
case 6 : {
console.log("Saturday");
break;
}
default: {
console.log("Invalid choice");
break;
}
}
输出