CoffeeScript 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.
if condition statement
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.
if condition1 statement 1 else statement 2
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.
if condition-1 statement 1 else if condition-2 statement 2 else statement 3
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.
if condition-1 statement-1 if condition-2 statement-2 else if statement-3 else statement-4 else statement
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.
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.
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
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