CoffeeScript Conditionals - CoffeeScript

What is CoffeeScript Conditionals?

Whilst programming, we come across some situations where we have to choose a direction from a given set of paths. In such situations, we need conditional statements. Conditional statements help us take choices and perform right moves
.
Following is the general shape of an ordinary decision-making structure determined in most of the programming languages.

decision_making_structure
JavaScript supports the if statement (such as its variations) and switch assertion. Similarly to the conditionals available in JavaScript, CoffeeScript consists of the until statement, the negation of if, and even greater.


Following are the conditional statements supplied by CoffeeScript.

S.No.

Statement & Description

1

if statement

Anifstatement 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

Anifstatement can be followed by an optionalelsestatement, which executes when the Boolean expression is false.

3

unless statement

Anunlessstatement is similar toifwith a Boolean expression followed by one or more statements except. These statements execute when a given Boolean expression is false.

4

unless...else statement

Anunlessstatement can be followed by an optionalelsestatement, which executes when a Boolean expression is true.

5

switch statement

Aswitchstatement allows a variable to be tested for equality against a list of values.

The then Keyword in CoffeeScript

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.

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

S.No.

Statement & Description

1

if-then statement

Using the if-then statement we can write theifstatement 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 optionalelsestatement, 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 theunlessstatement of CoffeeScript in a single line. It consists of a Boolean expression followed bythenkeyword, 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 optionalelsestatement, 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

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.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

CoffeeScript Topics