0

Is that possible for me to do nested loop in VB with same counter

The code is somehow like this

For a As Integer = 1 to Console.ReadLine
    For a = 1 to a
        Console.WriteLine("*")
    Next
    Console.WriteLine()
Next

The program is designed for drawing a triangle of * with just a single variable at all

VB just disallow me to use a in nested loop again
Error: ...Variable 'a' is alreay used by a independent loop.

I have my own usage, can only use 1 variable.

3
  • 1
    That is odd. I didn't realize VB stopped you from doing that. Leopold Stotch provided an excellent idea for a work around to technically get it to work, but you still haven't explained why you need to do this. Everyone is asking you why because it is extremely unlikely that such a construct would ever be a good idea. Everyone is asking you why because they want to help you to understand a better way of doing it. Commented Sep 26, 2013 at 11:51
  • I just edited it and You can see the reason I am doing it while the error prevents me from doing it. Logically, it is alright. Commented Sep 26, 2013 at 11:56
  • You're confusing everyone. Are you saying that this is for some sort of learning exercise where you are challenged to see if you can find a way to draw the triangle using only one iterator variable? If so, that changes our advice considerably. Commented Sep 26, 2013 at 12:42

5 Answers 5

1

What changing the second FOR loop to a WHILE loop?

For a As Integer = 1 to Console.ReadLine
    Do While a <=5
            Console.WriteLine("Line: " & a)
            Exit Do    
    Loop
Next
Sign up to request clarification or add additional context in comments.

8 Comments

Didn't you mean to increment a inside the While loop (and set it to 1 before the While loop. Otherwise, good answer :)
No, this is an infinite loop. And if you'd set it to 1 before/afterwards it would still be an infinite loop.
@TimSchmelter Granted, but technically it does get around the limitation in VB. "Solving" it may be a stretch, though :)
@StevenDoggart: Not really. If you want the same functionality as the two nested for loops you need also another variable with the While to store the old value. Then you gain nothing but you have more code.
@TimSchmelter All I'm saying is that I can imagine there are some situations out there where it may be precisely what you intend to do--to have a nested For loop which uses the same iterator and alters the outer For loop's iterator. If so, this would be the way to do it. But still, it would be utterly confusing and I would still recommend that there must be a more logical way to write the logic than to do something like that.
|
1

Here's a different idea. You may consider splitting your integer variable into 2 parts 16-bit parts, keep user's input in the upper 16-bits, and current iteration value in the lower 16-bits (you'll need to use WHILE instead of FOR).

Comments

0

In fact, what you need is to start your inner counter by the value of a, if I've understand. And what you are doing is create another loop inside starting by 1.

For a As Integer = 1 to Console.ReadLine
    For b As Integer = a to 5
        Console.WriteLine("Line: " & a)
    Next
Next

5 Comments

I mean I must only use 1 variable
Why that? You can't since they are not the same counter. In your case, when the inner loop ends, the value of a will be 5 and the 1st loop will resume with a = 5 infinetly
But i want them to share the same storage space.
Wow! You think an integer will cost you so much storage space?
No, I am in task where i can only use 1 variable
0

You cannot declare another variable with the same name in the same scope. But the inner loop is in the same scope as the outer loop. That's why you get that compiler error.

You can use a different name:

Dim i As Int32 
If Int32.TryParse(Console.ReadLine, i) AndAlso i > 0 Then
    For a As Integer = 1 To i
        For aa = 1 To i
            Console.WriteLine("Line: {0} {1}", a, aa)
        Next
    Next
End If

8 Comments

What if i want them to share the same variable(not only name but value)
@Echo: What do you want to share? You can access and use the outer loop's variable from within the inner loop as i've shown above (lok at the Console.WriteLine). In general, you have two loops, therefore you have two loop variables. You cannot use one for both.
I want them as a single variable, not two independent integers.
No, I can only use 1 variable. And so the same variable.
@Echo: I've already edited my last comment. An integer costs nothing, why do you want only one variable? Maybe you think that you need one int for every loop iteration. But you need only one for the loop itself.
|
0

To draw a triangle, as you describe, you need two variables, like this:

For a As Integer = 1 to Console.ReadLine
    For b As Integer = 1 to a
        Console.Write("*")
    Next
    Console.WriteLine()
Next

If you used the same variable in the inner loop, the inner loop would change the value of the variable, which in most cases would not be what you want, and in all cases would be incredibly confusing. For that reason, VB forces you to use a different iterator in each nested For loop.

3 Comments

No, if my code pass compile, it can work normally as it is specially designed not to be affected of its value. Try run it in you mind and see!
@Echo, I do realize, that in your specific scenario, the inner loop will always end with the iterator being equal to what it was when it entered the inner loop, so in that sense it would work. My point is that, to avoid confusion and bugs, the makers of VB.NET decided that they would disallow that, so even though it may logically work in your head, it cannot in VB. Despite all that, it's pointless to expend so much effort to save 4 bytes. If you are so concerned about it, use two Short variables instead of one Integer and you'll still be using only 4 bytes.
That is a challenging question actually

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.