CoffeeScript 条件语句
CoffeeScript编程语言用于编写更简单的语法,在执行之前编译成 JavaScript。在任何编程语言中,条件在以特定顺序执行代码中都起着重要作用。在本文中,我们将看到 CoffeeScript 支持的各种条件语句,如 If、Else、Else If 等,以及条件语句的语法。
在 CoffeeScript 中,没有使用括号和大括号。它的函数或多行条件语句由缩进分隔。
If 语句:如果我们想在执行其他语句之前测试一个条件,那么我们使用 If 语句。在 CoffeeScript 中,“If”语句是使用“if”关键字编写的。
句法:
if condition
statement
例子:
CoffeeScript
number = 10;
if number>0
console.log("Number is positive")
console.log("Outside if block")
CoffeeScript
number = -10;
if number>=0
console.log("Number is positive")
else
console.log("Number is negative")
CoffeeScript
number = 0;
if number>0
console.log("Number is positive")
else if number == 0
console.log("Number is Zero")
else
console.log("Number is negative")
console.log("Outside the conditional block")
CoffeeScript
number = 2;
if number > 0
console.log("Number is positive")
if number % 2 == 0
console.log("Number is also even")
else
console.log("Number is odd")
else if number == 0
console.log("Number is Zero")
else
console.log("Number is negative")
console.log("Outside the conditional block")
CoffeeScript
number = 3;
if number > 0
console.log("Number is positive");
if number > 1
console.log("Number is greater than One");
if number > 2
console.log("Number is even greater than Two");
else if number == 0
console.log("Number is Zero");
else
console.log("Number is negative");
console.log("Outside the conditional block");
CoffeeScript
number = 3;
if number > 0
console.log("Number is positive")
if number > 3
console.log("Number is greater than Three")
else
console.log("Number is less than or Equal to Three")
CoffeeScript
number = 3;
if number > 0
console.log("Number is positive")
if number > 3
console.log("Number is greater than Three")
else
console.log("Number is less than or Equal to Three")
输出:在上面的代码中,“if”语句在它持有真值时被执行,它里面的所有指令都将被执行。一旦“if”块被执行,它将在条件块之外执行更多的条件。
Number is positive
Outside if block
If Else 语句:我们不能对所有其他条件使用 If 语句。除了 If 条件之外,还可以有一个替代条件。如果“if”条件不成立,则需要执行另一条指令。在这里,图片中出现了“其他”。让我们看看我们如何一起使用if和else 。
句法:
if condition1
statement 1
else
statement 2
例子:
咖啡脚本
number = -10;
if number>=0
console.log("Number is positive")
else
console.log("Number is negative")
输出:在上面的代码中,“if”条件将被执行,如果不成立,它将执行下一条语句“else”。
Number is negative
Else If 语句:有时需要在代码中检查多个语句。我们可以根据我们的要求,多次使用“Else If”语句检查多个条件以得出结论。
句法:
if condition-1
statement 1
else if condition-2
statement 2
else
statement 3
例子:
咖啡脚本
number = 0;
if number>0
console.log("Number is positive")
else if number == 0
console.log("Number is Zero")
else
console.log("Number is negative")
console.log("Outside the conditional block")
输出:将检查第一个“if number>0”条件。由于它不是真的,“else if”语句将被执行,它是真的,并会在控制台中打印“Number is zero”。之后,它将不再检查进一步的条件,而是从条件块中出来并执行进一步的语句。在上面的代码中,它将打印“在条件块之外”。
Number is Zero
Outside the conditional block
嵌套 If 语句:当我们想要检查多个条件并执行特定的代码行时,我们使用嵌套的“if”语句。
句法:
if condition-1
statement-1
if condition-2
statement-2
else if
statement-3
else
statement-4
else
statement
例子:
咖啡脚本
number = 2;
if number > 0
console.log("Number is positive")
if number % 2 == 0
console.log("Number is also even")
else
console.log("Number is odd")
else if number == 0
console.log("Number is Zero")
else
console.log("Number is negative")
console.log("Outside the conditional block")
输出:在上面的代码中,它检查数字是否为正,然后执行下一个“If”语句来检查数字是偶数还是奇数。检查条件并打印输出后,它执行条件块之后的指令并打印语句。
Number is positive
Number is also even
Outside the conditional block
如果我们使用“If”而不是“Else if”语句会发生什么?
当我们使用多个“if”语句时,每个“if”语句都会被执行,无论它们是否为真。正确的语句进一步执行它们内部的指令。但是当我们使用“Else if”语句时,只有当“If”语句为假时才会执行。
示例 1:
咖啡脚本
number = 3;
if number > 0
console.log("Number is positive");
if number > 1
console.log("Number is greater than One");
if number > 2
console.log("Number is even greater than Two");
else if number == 0
console.log("Number is Zero");
else
console.log("Number is negative");
console.log("Outside the conditional block");
输出:
Number is positive
Number is greater than One
Number is even greater than Two
Outside the conditional block
多个“If”语句存在问题,如果最后一个“If”语句不持有真值,则 else 语句将在之后执行。让我们通过一个例子来理解这一点。
示例 2:
咖啡脚本
number = 3;
if number > 0
console.log("Number is positive")
if number > 3
console.log("Number is greater than Three")
else
console.log("Number is less than or Equal to Three")
输出:在上面的代码中,它检查所有的“if”条件,因为最后一个“if”语句是假的,它执行“else”语句而不是跳出条件块。这就是嵌套语句出现的地方。让我们看看它如何解决我们的问题。
Number is positive
Number is less than or Equal to Three
示例 3:
咖啡脚本
number = 3;
if number > 0
console.log("Number is positive")
if number > 3
console.log("Number is greater than Three")
else
console.log("Number is less than or Equal to Three")
输出:检查 if 条件后,它将突破条件块,而不是像前一种情况那样执行“else”语句。
Number is positive