Coffeescript 简明教程

CoffeeScript - Conditionals

在编程时,我们遇到了一些场景,其中我们必须从给定的一组路径中选择一条路径。在此类情况下,我们需要条件语句。条件语句帮助我们做出决策并执行正确操作。

While programming, we encounter some scenarios where we have to choose a path from a given set of paths. In such situations, we need conditional statements. Conditional statements help us take decisions and perform right actions.

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

Following is the general form of a typical decision-making structure found in most of the programming languages.

decision making structure

JavaScript 支持 if 语句(包括其变体)和 switch 语句。除了 JavaScript 中提供的条件外,CoffeeScript 还包括 unless 语句、if 的否定,甚至更多。

JavaScript supports the if statement (including its variants) and switch statement. In addition to the conditionals available in JavaScript, CoffeeScript includes the unless statement, the negation of if, and even more.

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

Following are the conditional statements provided by CoffeeScript.

S.No.

Statement & Description

1

if statementAn 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 statementAn if statement can be followed by an optional else statement, which executes when the Boolean expression is false.

3

unless statementAn 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 statementAn unless statement can be followed by an optional else statement, which executes when a Boolean expression is true.

5

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

The then Keyword in CoffeeScript

ifunless 语句是使用多行编写的块语句。CoffeeScript 提供 then 关键字,我们可以使用它在一行中编写 ifunless 语句。

The if and unless statements are block statements that are written in multiple lines. CoffeeScript provides the then keyword using which we can write the if and the unless statements in a single line.

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

Following are the statements in CoffeeScript that are written using then keyword.

S.No.

Statement & Description

1

if-then statementUsing 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 statementThe 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 statementUsing 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 statementThe 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 and postfix unless Statements

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

In CoffeeScript, you can also write the if and unless statements having a code block first followed by if or unless keyword as shown below. This is the postfix form of those statements. It comes handy while writing programs in CoffeeScript.

#Postfix if
Statements to be executed if expression

#Postfix unless
Statements to be executed unless expression