9 Replies - 20735 Views - Last Post: 14 October 2014 - 04:45 AM

#1 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Challenge: The 10 Ways to Loops (Easy)

Post icon  Posted 25 March 2012 - 08:51 PM

Challenge: The 10 Ways To Loop

Challenge:

From my experimentation and exploration of the language I believe that are only 10 ways different ways in VB.Net to perform looping and iteration.

See if you can discover those 10 ways.
Each one must output to the Console Window the numbers 1 to 10, each on a separate line.

Place each way you find into it's own method (Sub) and call it from Sub Main

Put your submissions inside of spoiler and code tags.

Note: (.net 3.5 used)
Bonus: Find ways not on my list, if there is any.

This post has been edited by AdamSpeight2008: 26 March 2012 - 01:28 PM


Is This A Good Question/Topic? 2
  • +

Replies To: Challenge: The 10 Ways to Loops (Easy)

#2 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 26 March 2012 - 05:30 PM

Here are 12 methods. It depends on how different they have to be to be different. Some of these are pretty similar. You reduce this to about 5 if you wanted to. Also, replace write for writeline to print the numbers on separate lines.

I was disappointed that there was no GOSUB in VB. Also, I couldn't find out how to convert a variable's contents to a line label, if it's even possible. That would allow for a strange method of looping.

Adam, how does this compare with your list?

Spoiler

Was This Post Helpful? 1
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,499
  • Joined: 29-May 08

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 26 March 2012 - 07:44 PM

cfoley: You use For .. Loop twice, but that offset by the two additional ones of using errors and exceptions.

This post has been edited by AdamSpeight2008: 26 March 2012 - 07:45 PM

Was This Post Helpful? 0
  • +
  • -

#4 cfoley  Icon User is online

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 27 March 2012 - 03:36 AM

Does that mean you have one that I didn't get?
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4577
  • View blog
  • Posts: 8,019
  • Joined: 08-June 10

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 27 March 2012 - 06:18 AM

I guess this is sorta cheating, because you can use basically any of the LINQ methods for this:

Spoiler


I haven't kept up with the differences between VB.NET and C#. I wanted to use an iterator method, but apparently I'll have to wait for that. I could have sworn I had seen it, but it must have been async ctp code.
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,199
  • Joined: 25-September 09

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 28 March 2012 - 08:36 AM

cfoley covered a lot of them but I guess you could get as obscure as a timer for a "Timed Loop"
Spoiler


This of course would not loop in the sense that the loop has to finish before processing the remaining code but would loop WHILE still processing any remaining code.

This post has been edited by CharlieMay: 28 March 2012 - 08:38 AM

Was This Post Helpful? 0
  • +
  • -

#7 demausdauth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 182
  • View blog
  • Posts: 663
  • Joined: 03-February 10

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 02 April 2012 - 07:11 PM

Forgot the Untils


Public Sub DoUntil()

     Dim n As Integer = 0
     Do Until n = 11          
          n += 1
          Console.WriteLine("{0}", n)
     Loop
End Sub

Public Sub LoopUntil()
     Dim n As Integer = 1

     Do
          Console.WriteLine("{0}",n)
          n += 1
     Loop Until n = 10
End Sub



Was This Post Helpful? 0
  • +
  • -

#8 Braber01  Icon User is offline

  • D.I.C Regular

Reputation: 5
  • View blog
  • Posts: 252
  • Joined: 29-November 08

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 06 April 2012 - 07:00 PM

Spoiler

Was This Post Helpful? 0
  • +
  • -

#9 thava  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 180
  • View blog
  • Posts: 1,606
  • Joined: 17-April 07

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 15 April 2014 - 07:53 AM

well check this one
Spoiler

Was This Post Helpful? 0
  • +
  • -

#10 sujithkarivelil  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 3
  • Joined: 25-June 13

Re: Challenge: The 10 Ways to Loops (Easy)

Posted 14 October 2014 - 04:45 AM

This is my first post in this site, so please forgive me if their are mistakes.
Am having 12 different methods to solve this as mentioned in the following code

Module Module1
    Dim x As Integer = 0
    Dim a() As Integer
    Sub Main()
        a = {1, 2, 3, 4, 5}
        Console.WriteLine(" using For" & vbNewLine)
        usingFor() '<----- method 1
        Console.WriteLine(" using while" & vbNewLine)
        usingwhile() '<----- method 2
        Console.WriteLine(" using Do until as exit control loop" & vbNewLine)
        usingDountil() '<----- method 3
        Console.WriteLine(" using Do - while as exit control loop" & vbNewLine)
        usingDowhile() '<----- method 4
        Console.WriteLine(" using Do-loop" & vbNewLine)
        usingDoloop() '<----- method 5
        Console.WriteLine(" recursion" & vbNewLine)
        recursion(x) '<----- method 6
        Console.WriteLine(" Do Until as entry control loop" & vbNewLine)
        DoUntilINentry() '<----- method 7
        Console.WriteLine(" Do while as entry control loop" & vbNewLine)
        DoUntilINentry() '<----- method 8
        Console.WriteLine(" using for - each " & vbNewLine)
        usingForEach() '<----- method 9
        Console.WriteLine(" using string " & vbNewLine)
        usingstring() '<----- method 10
        Console.WriteLine(" using goto label " & vbNewLine)
        gotolabel() '<----- method 11
        Console.WriteLine(" using select " & vbNewLine)
        selectCase() '<----- method 12
        Console.ReadKey()
    End Sub
    Public Sub usingFor()
        For i As Integer = 1 To 5
            Console.WriteLine(i & vbNewLine)
        Next
    End Sub

    Public Sub usingwhile()
        Dim i As Integer = 1
        While i <= 5
            Console.WriteLine(i & vbNewLine)
            i += 1
        End While
    End Sub

    Public Sub usingDountil()
        Dim i As Integer = 1
        Do
            Console.WriteLine(i & vbNewLine)
            i += 1
        Loop Until i = 6
    End Sub


    Public Sub usingDowhile()
        Dim i As Integer = 1
        Do
            Console.WriteLine(i & vbNewLine)
            i += 1
        Loop While i <= 5
    End Sub

    Public Sub usingDoloop()
        Dim i As Integer = 1
        Do
            Console.WriteLine(i & vbNewLine)
            i += 1
            If i > 5 Then
                Exit Do
            End If
        Loop
    End Sub

    Public Sub recursion(ByVal x As Integer)
        x += 1
        If x > 5 Then
            Exit Sub
        Else
            Console.WriteLine(x & vbNewLine)
            recursion(x)
        End If

    End Sub

    Public Sub DoUntilINentry()
        Dim n As Integer = 0
        Do Until n = 5
            n += 1
            Console.WriteLine("{0}", n)
        Loop
    End Sub
    Public Sub DowhileINentry()
        Dim n As Integer = 0
        Do While n > 5
            n += 1
            Console.WriteLine("{0}", n)
        Loop
    End Sub
    Public Sub usingForEach()
        For Each i As Integer In a
            Console.WriteLine(i & vbNewLine)
        Next
    End Sub
    Public Sub usingstring()
        Dim s As String = "12345"
        For Each c As Char In s
            Console.WriteLine(c & vbNewLine)
        Next
    End Sub

    Public Sub gotolabel()
        Dim i As Integer = 1
start:  If i > 5 Then GoTo endlabel
        Console.WriteLine(i & vbNewLine)
        i += 1
        GoTo start
endlabel:
    End Sub
    Public Sub selectCase()
        Dim i As Integer = 1
start:  Select Case i
            Case 1 To 5
                Console.WriteLine(i & vbNewLine)
            Case Else
                GoTo endlabel
        End Select
        i += 1
        GoTo start
endlabel:
    End Sub
End Module

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1