How to resolve the algorithm Loops/Foreach step by step in the Ecstasy programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/Foreach step by step in the Ecstasy programming language

Table of Contents

Problem Statement

Loop through and print each element in a collection in order. Use your language's "for each" loop if it has one, otherwise iterate through the collection in order with some other loop.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/Foreach step by step in the Ecstasy programming language

Source code in the ecstasy programming language

module LoopForEach {
    @Inject Console console;
    void run() {
        val vals = [10, 20, 30, 40];
        console.print("Array of values:");
        Loop: for (val val : vals) {
            console.print($"  value #{Loop.count + 1}: {val}");
        }

        Map pairs = ["x"=42, "y"=69];
        console.print("\nKeys and values:");
        for ((String key, Int val) : pairs) {
            console.print($"  {key}={val}");
        }
        console.print("\nJust the keys:");
        Loop: for (String key : pairs) {
            console.print($"  key #{Loop.count + 1}: {key}");
        }

        console.print("\nValues from a range:");
        for (Int n : 1..5) {
            console.print($"  {n}");
        }
    }
}


  

You may also check:How to resolve the algorithm Exceptions step by step in the blz programming language
You may also check:How to resolve the algorithm Command-line arguments step by step in the vbScript programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Perl programming language
You may also check:How to resolve the algorithm Anagrams/Deranged anagrams step by step in the Factor programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Delphi programming language