How to resolve the algorithm Loops/With multiple ranges step by step in the Visual Basic .NET programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Loops/With multiple ranges step by step in the Visual Basic .NET programming language

Table of Contents

Problem Statement

Some languages allow multiple loop ranges, such as the PL/I example (snippet) below.

Simulate/translate the above PL/I program snippet as best as possible in your language,   with particular emphasis on the   do   loop construct. The   do   index must be incremented/decremented in the same order shown. If feasible, add commas to the two output numbers (being displayed). Show all output here.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Loops/With multiple ranges step by step in the Visual Basic .NET programming language

Source code in the visual programming language

Partial Module Program
    ' Stop and Step are language keywords and must be escaped with brackets.
    Iterator Function Range(start As Integer, [stop] As Integer, Optional [step] As Integer = 1) As IEnumerable(Of Integer)
        For i = start To [stop] Step [step]
            Yield i
        Next
    End Function
End Module


Imports System.Globalization

Partial Module Program
    Sub Main()
        ' All variables are inferred to be of type Integer.
        Dim prod = 1,
            sum = 0,
            x = +5,
            y = -5,
            z = -2,
            one = 1,
            three = 3,
            seven = 7

        ' The exponent operator compiles to a call to Math.Pow, which returns Double, and so must be converted back to Integer.
        For Each j In Range(-three,       CInt(3 ^ 3),        3     ).
               Concat(Range(-seven,       +seven,             x     )).
               Concat(Range(555,          550 - y                   )).
               Concat(Range(22,           -28,                -three)).
               Concat(Range(1927,         1939                      )).
               Concat(Range(x,            y,                  z     )).
               Concat(Range(CInt(11 ^ x), CInt(11 ^ x) + one        ))

            sum = sum + Math.Abs(j)
            If Math.Abs(prod) < 2 ^ 27 AndAlso j <> 0 Then prod = prod * j
        Next

        ' The invariant format info by default has two decimal places.
        Dim format As New NumberFormatInfo() With {
            .NumberDecimalDigits = 0
        }

        Console.WriteLine(String.Format(format, " sum= {0:N}", sum))
        Console.WriteLine(String.Format(format, "prod= {0:N}", prod))
    End Sub
End Module


    
    Function ConcatRange(source As IEnumerable(Of Integer), start As Integer, [stop] As Integer, Optional [step] As Integer = 1) As IEnumerable(Of Integer)
        Return source.Concat(Range(start, [stop], [step]))
    End Function


        For Each j In Range(-three,       CInt(3 ^ 3),        3     ).
                ConcatRange(-seven,       +seven,             x     ).
                ConcatRange(555,          550 - y                   ).
                ConcatRange(22,           -28,                -three).
                ConcatRange(1927,         1939                      ).
                ConcatRange(x, y,         z                         ).
                ConcatRange(CInt(11 ^ x), CInt(11 ^ x) + one        )
        Next


    Function Range(ParamArray ranges() As (start As Integer, [stop] As Integer, [step] As Integer)) As IEnumerable(Of Integer)
        ' Note: SelectMany is equivalent to bind, flatMap, etc.
        Return ranges.SelectMany(Function(r) Range(r.start, r.stop, r.step))
    End Function


        For Each j In Range((-three,       CInt(3 ^ 3),        3        ),
                            (-seven,       +seven,             x        ),
                            (555,          550 - y,            1        ),
                            (22,           -28,                -three   ),
                            (1927,         1939,               1        ),
                            (x,            y,                  z        ),
                            (CInt(11 ^ x), CInt(11 ^ x) + one, 1        ))
        Next


    Function Range(ParamArray ranges As Integer()()) As IEnumerable(Of Integer)
        Return ranges.SelectMany(Function(r) Range(r(0), r(1), If(r.Length < 3, 1, r(2))))
    End Function


        For Each j In Range({-three,       CInt(3 ^ 3),        3        },
                            {-seven,       +seven,             x        },
                            {555,          550 - y                      },
                            {22,           -28,                -three   },
                            {1927,         1939                         },
                            {x,            y,                  z        },
                            {CInt(11 ^ x), CInt(11 ^ x) + one           })
        Next


  

You may also check:How to resolve the algorithm Currying step by step in the Python programming language
You may also check:How to resolve the algorithm Canny edge detector step by step in the Tcl programming language
You may also check:How to resolve the algorithm Count the coins step by step in the OCaml programming language
You may also check:How to resolve the algorithm Harshad or Niven series step by step in the K programming language
You may also check:How to resolve the algorithm Dominoes step by step in the Perl programming language