Hi I have a program in java that outputs 1000 hello world without using iterators (for, do while etc)..
I tried to translate my code into vb.net and here's my code:

`Module Module1

Sub Main()
    printToThousand(1)
End Sub

Public Function printToThousand(ByVal i As Integer)
    Dim r As Boolean

    Console.WriteLine("Hello World")
    Console.ReadKey()

    r = (i < 1000) Or labas()
    printToThousand(i + 1)
    Return r


End Function

Public Function labas()
    System.Environment.Exit(1)
    Return True
End Function

End Module
`

But it wouldn't output 1000 hello world, only one. Please help.

Here is a tiny little recursive sub procedure I have written to do what you are needing it to do.

Public Sub PlusOne(int As Integer)
    If int <= 1000 Then
        ListBox1.Items.Add("HelloWorld")
        int += 1
        PlusOne(int)
    End If
End Sub

Just need to pass 1 in as int when you first call the sub, and change ListBox1 to Console.WriteLine.

Public Sub PlusOne(int As Integer)
If int <= 1000 Then
ListBox1.Items.Add("HelloWorld")
int += 1
PlusOne(int)
End If
End Sub

Thanks for the idea, I changed mine to

Module Module1

    Sub Main()

        printToThousand(1)
        Console.Read()
    End Sub

    Public Sub printToThousand(ByVal i As Integer)

        If i < 1000 Then
            Console.WriteLine("Hello World")

            printToThousand(i + 1)

        End If

    End Sub


End Module

No problem friend, any other issues you have?

I would use count down style without fixed number to be able to do any given number of repeats, and do Hello(1000) to do thousand of them, probably something like (I have not VB installed in my computer):

Public Sub Hello(ByVal howmany As Integer)
    If howmany > 0 Then
        ListBox1.Items.Add("HelloWorld")
        Hello(howmany-1)
    End If
End Sub

The verbalization as pseudocode makes more sense for me like this: if you have any hellos not done do one hello and do one less hellos. Which becomes for 1000: Do hello and 999 hellos.

Keep in mind that just because you can solve a problem with recursion it doesn't mean that you should. Traversing a binary tree is a good example of where recursion is desirable, and actually produces clearer code than alternatives. Calculating factorials is a usual example to teach recursion. But that's all it is useful for. It is not a good idea to calculate factorials using recursion. Too much overhead for too little gain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.