CoffeeScript - Loops



While coding, you may encounter a situation where you need to execute a block of code over and over again. In such situations, you can use loop statements.

In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages

Loop Architecture

JavaScript provides while, for and for..in loops. The loops in CoffeeScript are similar to those in JavaScript.

while loop and its variants are the only loop constructs in CoffeeScript. Instead of the commonly used for loop, CoffeeScript provides you Comprehensions which are discussed in detail in later chapters.

The while loop in CoffeeScript

The while loop is the only low-level loop that CoffeeScript provides. It contains a Boolean expression and a block of statements. The while loop executes the specified block of statements repeatedly as long as the given Boolean expression is true. Once the expression becomes false, the loop terminates.

Syntax

Following is the syntax of the while loop in CoffeeScript. Here, there is no need of the parenthesis to specify the Boolean expression and we have to indent the body of the loop using (consistent number of) whitespaces instead of wrapping it with curly braces.

while expression
   statements to be executed

Example

The following example demonstrates the usage of while loop in CoffeeScript. Save this code in a file with name while_loop_example.coffee

console.log "Starting Loop "
count = 0  
while count < 10
   console.log "Current Count : " + count
   count++;
   
console.log "Set the variable to different value and then try"

Open the command prompt and compile the .coffee file as shown below.

c:\> coffee -c while_loop_example.coffee

On compiling, it gives you the following JavaScript.

// Generated by CoffeeScript 1.10.0
(function() {
  var count;

  console.log("Starting Loop ");

  count = 0;

  while (count < 10) {
    console.log("Current Count : " + count);
    count++;
  }

  console.log("Set the variable to different value and then try");

}).call(this); 

Now, open the command prompt again and run the CoffeeScript file as shown below.

c:\> coffee while_loop_example.coffee

On executing, the CoffeeScript file produces the following output.

Starting Loop
Current Count : 0
Current Count : 1
Current Count : 2
Current Count : 3
Current Count : 4
Current Count : 5
Current Count : 6
Current Count : 7
Current Count : 8
Current Count : 9
Set the variable to different value and then try 

Variants of while

The While loop in CoffeeScript have two variants namely the until variant and the loop variant.

S.No. Loop Type & Description
1 until variant of while

The until variant of the while loop contains a Boolean expression and a block of code. The code block of this loop is executed as long as the given Boolean expression is false.

2 loop variant of while

The loop variant is equivalent to the while loop with true value (while true). The statements in this loop will be executed repeatedly until we exit the loop using the Break statement.

Advertisements