Updated April 3, 2023
Introduction to CoffeeScript for loop
CoffeeScript for loop is used as a substitute for comprehensions in terms of coding. CoffeeScript supports a lot of loops, but it deals with comprehensions that are externally added with some optional clauses when it comes to for loop. It helps to make the comprehensions move into an array iterate with objects and expressions. Iterations performed over the array in a loop can be manipulated with operations on objects present within it that can return them either function or variable. For in and for of comprehensions are some of the basic in the form in CoffeeScript.
Syntax
The Syntax of for loop in CoffeScript represents the loops with scenarios where it is represented with iteration as follows:
K for k in [1…5] // This represents for..in the loop with a declaration of values ranging from 1 to 5.
K for k in [1…..8] by 2 // This represents for k where a counter then applies the loop.
K *k for k in [1….5] // Squaring for performing a loop in elements.
Flowchart
- The flowchart of for loop basically behaves in a similar manner as represented above, but when it comes with respect to coffee-script then, in that case, we have two scenarios that exist one is for..in the loop and another one is for ..of loops both are treated as comprehensions in terms of CoffeeScript.
- The iterating behavior remains the same in both the difference between the storage and other key-value pairs as objects.
- All the elements present in it can be iterated using any of the two comprehensions again, depending upon the requirement.
- The elements get initialized then the condition is evaluated if the condition evaluated comes out to be true, then the cursor will land into the main loop of the program; otherwise, If the evaluated value comes out to be false, then it will come out of the loop and point to the next set of statements for execution.
- If in case the evaluated body, after giving the result, comes out to be true, then it will land into the next set of statements after the body loop and will perform increment or decrement operation before entering the base loop.
How does for loop works in CoffeeScript?
- For loop in CoffeeScript are known as comprehensions that have their own significance when accessing and manipulating the objects or the values present within it.
- The entire for loop works in a way where there is a lot of complex loops present as per requirement; once they are simplified, then the implementation becomes easy.
- CoffeeScript just requires the data as input either in json, yaml, or xml; it will internally convert the entire json in the form of an object that can be defined as part of the function javaScript.
- It basically tries to abstract the important and relevant details by allowing the iteration within the array or the structure.
- For…in the loop helps in making the entire structure work in the manner by providing the detail with initialization and incrementation function CoffeeScript will automatically take care to make it formulate into JavaScript as per control flow mentioned before.
- Something similar is performed in for…of a loop in CoffeeScript with a minor difference that it allows the entry and traversal in the form of key and value pairs.
- List in comprehensions also works but with a difference that it allows the conversion into the object at one go.
Examples
Here are the following examples mention below
Example #1
This example demonstrates the for…in the loop of coffee-script where the set of cars_ex is given with a set of inputs with sedans, suv, and micros post, which gives the ready-made javascript function shown below.
for cars_ex in ['sedans', 'suv', 'micro']
console.log cars_ex
Output:
Explanation: In the above example, an exp_car is taken as a set of input within an object which is then tried to be printed. CoffeeScript internally takes care of creating a javascript based object for iterating it as shown in the output above, using which the values can be called as it will be the part of the function defined with a lot of objects into it. The last values shown are the part of the output for the concerned code called using CoffeeScript.
Example #2
This program demonstrates the for..of loop in CoffeeScript where the car name is given in the form of key and values, and that is passed as an object to be printed the output expected both in the form of javascript or the actual output is shown below.
for key_0,val_0 of { car_name: "Ani", car_no: 34323, owner_no: 93456767565}
console.log key_0+":::"+val_0
Output:
Explanation: The above code consists of the car details to retrieve and travel on the defined set of values as input to the array and then further moving to get the function of the javaScript format with all the key and value pairs defined as an object then the CoffeeScript will take care of it internally giving one entire output of car details possessing name, car_no and owner_no respectively.
Example #3
This program demonstrates the list as comprehensions where the for loop can be given with multiple key and value pairs that will ultimately fetch the javascript function with the output as shown below.
cars_exc = [
car_name: "Limborgini"
car_no: 234565
car_type: "sedan"
,
car_name: "Hectar"
car_no: 12345
car_type: "suv"
,
car_name: "Audi"
car_no: 546736
car_type: "micro"
]
accss_names = (cars.car_name for cars in cars_exc)
console.log accss_names
Output:
Explanation: The entire list of comprehension with for loop will be given as part of a function where the multiple set of keys and values will be part of the CoffeeScript; it will pass the entire list as an object and will return the following javascript with three main outputs as shown.
Conclusion
CoffeeScript for loop is used for various reasons, especially when it comes to ordering and making all the elements into one consolidated form. Moreover, it gives the developers an edge to simplify the entire object to enlist the entities in one or another format to access all the relevant details of javascript as well.
Recommended Articles
We hope that this EDUCBA information on “CoffeeScript for loop” was beneficial to you. You can view EDUCBA’s recommended articles for more information.