CoffeeScript-条件



在编程时,我们遇到某些情况,我们必须从一组给定的路径中选择一条路径。在这种情况下,我们需要条件语句。条件语句有助于我们做出决定并采取正确的行动。

以下是大多数编程语言中常见的典型决策结构的一般形式。

决策结构

JavaScript支持if语句(包括其变体)和switch语句。除了JavaScript中可用的条件外,CoffeeScript还包含除非语句,否定if等等。

以下是CoffeeScript提供的条件语句。

S.No. Statement & Description
1 if statement

An if statement consists of a Boolean expression followed by one or more statements. These statements execute when the given Boolean expression is true.

2 if…else statement

An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

3 unless statement

An unless statement is similar to if with a Boolean expression followed by one or more statements except. These statements execute when a given Boolean expression is false.

4 unless…else statement

An unless statement can be followed by an optional else statement, which executes when a Boolean expression is true.

5 switch statement

A switch statement allows a variable to be tested for equality against a list of values.

CoffeeScript中的关键字

如果除非语句是用多行编写的块语句。 CoffeeScript提供了then关键字,使用该关键字我们可以在一行中编写ifexclude语句。

以下是Coffee语句中使用then关键字编写的语句。

S.No. Statement & Description
1 if-then statement

Using the if-then statement we can write the if statement of CoffeeScript in a single line. It consists of a Boolean expression followed by then keyword, which is followed by one or more statements. These statements execute when the given Boolean expression is true.

2 if-then…else statement

The if-then statement can be followed by an optional else statement, which executes when the Boolean expression is false. Using if-then…else statement, we can write the if…else statement in a single line.

3 unless-then statement

Using the unless-then statement, we can write the unless statement of CoffeeScript in a single line. It consists of a Boolean expression followed by then keyword, which is followed by one or more statements. These statements execute when the given Boolean expression is false.

4 unless…then else statement

The unless-then statement can be followed by an optional else statement, which executes when the Boolean expression is true. Using unless-then…else statement, we can write the unless…else statement in a single line.

postfix if和postfix除非声明

在CoffeeScript中,您还可以编写ifnon语句,该语句首先具有一个代码块,然后是ifnon关键字,如下所示。这是这些语句的后缀形式。使用CoffeeScript编写程序时非常方便。

#Postfix if
Statements to be executed if expression

#Postfix unless
Statements to be executed unless expression

显示例子

Made with ❤️ in Chengdu. Copyright reserved 2019-2022.

蜀ICP备20006366号-1