The CoffeeScript programming language is used to write a simpler syntax that compiles into JavaScript before getting executed. In any programming language, conditions play an important role in the execution of code in a particular sequence. In this article, we will see various conditional Statements such as If, Else, Else If, etc. that CoffeeScript supports along with the syntax of conditional statements.
In CoffeeScript, there is no use of parenthesis and curly brackets. Either its functions or multi-line conditional statements are delimited by indentation.
If Statement: If we want to test a condition before executing other statements, then we use the If statement. In CoffeeScript, the “If” statement is written by using the “if” keyword.
Syntax:
if condition statement
Example:
CoffeeScript
number = 10; if number>0 console.log( "Number is positive" ) console.log( "Outside if block" ) |
Output: In the above code, the “if” statement gets executed as it holds true value, all the instructions inside it will be executed. Once the “if” block is executed, it will execute further conditions outside the conditional block.
Number is positive Outside if block
If Else Statement: We cannot use the If statement for every other condition. There can be an alternative condition than the If condition. The other instruction needs to get executed if the “if” condition does not hold true. Here, comes “else” in the picture. Let’s see how we use if and else together.
Syntax:
if condition1 statement 1 else statement 2
Example:
CoffeeScript
number = -10; if number>=0 console.log( "Number is positive" ) else console.log( "Number is negative" ) |
Output: In the above code, the “if” condition will be executed, as it is not true, it will execute the next statement which is “else”.
Number is negative
Else If Statement: Sometimes there are multiple statements to get checked in a code. We can check multiple conditions to reach to conclusion using the “Else If” statement as many times according to our requirement.
Syntax:
if condition-1 statement 1 else if condition-2 statement 2 else statement 3
Example:
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" ) |
Output: The first “if number>0” condition will be checked. Since it is not true, the “else if” statement will be get executed which is true, and will print “Number is zero” in the console. After that, it will not check further conditions and will come out of the conditional block and execute further statements. In the above code, it will print “Outside the conditional block”.
Number is Zero Outside the conditional block
Nested If Statement: We use nested “if” statements when we want to check more than one condition and execute particular lines of code.
Syntax:
if condition-1 statement-1 if condition-2 statement-2 else if statement-3 else statement-4 else statement
Example:
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" ) |
Output: In the above code, it checks if the number is positive, and then executes the next “If” statement to check if the number is also even or odd. After checking the conditions, and printing the output, it executes instructions after the conditional block and prints the statement.
Number is positive Number is also even Outside the conditional block
What happens if we use the “If” instead of the “Else if” statement?
When we use multiple “if” statements, every “if” statement gets executed whether they are true or not. The statements which are true further executes instructions inside them. But when we use the “Else if” statement, it gets executed only if the “If” statement is false.
Example 1:
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" ); |
Output:
Number is positive Number is greater than One Number is even greater than Two Outside the conditional block
There is a problem with multiple “If” statements, if the last “If” statement does not hold true value, then else statement will get executed after that. Let’s understand this with an example.
Example 2:
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" ) |
Output: In the above code, it is checking all the “if” conditions, since the last “if” statement is false, it executes the “else” statement instead of breaking out of the conditional block. That’s where nested statements come into the picture. Let’s see how it solves our problem.
Number is positive Number is less than or Equal to Three
Example 3:
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" ) |
Output: After checking if conditions, it will break through the conditional block instead of executing the “else” statement as in the previous case.
Number is positive