Change a For Loop to a Do While statement? - vb.net

Module Module1
Sub Main()
For value As Integer = 9 To 0 'Step 1'
System.Console.WriteLine(value)
Next
End Sub
End Module
This should be simple id imagine, how could this easily be changed from a "For Loop" to a "Do While" statement?

You can use the following Do While loop instead:
Dim value As Integer = 9
Do While value <= 9 And value >= 0
Console.WriteLine(value)
value -= 1 'this count from 9 to 0
Loop
But your For doesn't work this way. At the moment you For loop doesn't output any value. It looks like you set the wrong steps:
For value As Integer = 9 To 0 Step -1
Console.WriteLine(value) 'this count from 9 to 0
Next
The Do While loop solution to count from 0 to 9:
Dim value As Integer = 0
Do While value <= 9 And value >= 0
Console.WriteLine(value)
value += 1 'this count from 0 to 9
Loop
The For loop solution to count from 0 to 9:
For value As Integer = 0 To 9 Step 1
Console.WriteLine(value) 'this count from 0 to 9
Next

This will never enter the loop, as the start value is higher than the end, and the step is not negative. See here:
https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/for-next-statement#technical-implementation
To turn this into a Do-While, you would have to first check if the start and end conditions are valid, otherwise it would be stuck in an infinite loop.

Related

Visual Basic dividing

I'm on visual basic and I'm reading through some piece of code my teacher wrote, he had this chunck of code:
Private Sub btnDividing_Click(sender As Object, e As EventArgs) Handles btnDividing.Click
Dim number As Integer = InputBox("Divide number by 2:")
Dim result As Integer = 0
Do While (number <> 0)
result += 1
number = number - 2
Loop
MsgBox("The result is: " & result, MsgBoxStyle.Exclamation)
End Sub
So my teacher typed the result += 1 and number = number -2 I didn't really understand that part so i tried simplifying it by changing it to:
Dim number As Integer = InputBox("Divide number by 2:")
Dim result As Integer = 0
Do While (number <> 0)
result = number / 2
Loop
MsgBox("The result is: " & result, MsgBoxStyle.Exclamation)
End Sub
but it keeps freezing after I click "OK"
Any suggestions?
It freezes because you made it an infinite loop:
Do While (number <> 0)
result = number / 2
Loop
The loop checks the value of number, but your modified code in the loop never modifies the value of number. So if the condition is true the first time it's checked, it will always be true. The original code modified the value:
Do While (number <> 0)
result += 1
number = number - 2
Loop
Since number is decremented by 2 with each iteration of the loop, it will eventually (assuming it's even) be equal to 0, making the loop condition false and the loop will end.
Basically, a loop needs to in some way modify the values being checked in the condition (or have some other control statement to exit the loop) or the loop will infinitely run.

skip over a value in a loop and then continue to loop?

I need to be able to run a loop starting at 0, have it identify a certain value (in this case piecenumblack) and then skip that value, and then continue the loop until it hits 11. I'm really unsure what type of loop to use and I've had no success with a Do, While, or For loop.
Dim piecenumblack As Integer
For i = 0 To piecenumblack
Next
For i = 11 To piecenumblack Step -1
Next
You could add an If to the inside of the loop:
Dim piecenumblack As Integer
piecenumblack = 3
For i = 0 To 11
If i <> piecenumblack then
'Do Code
End If
Next
This would then skip doing any code when i = 3 then continue on with 4,5,6..11.

VBA Override Error 6 Overflow (Create Infinite Loop)?

I want an infinite loop in VBA (I have done this in Java and C++ before). I keep getting "Overflow" with the VBCritical red circle X.
Here is my code. The Error <>0 is supposed to recognize the overflow and ignore it to let the macro continue looping infinitely, but I still get the overflow VBCritical MsgBox.
I want to print the numbers out in column A. This part works right now: it prints "2".
Here is my code:
Sub InfiniteLoop()
Dim counter As Integer
counter = 1
Do While counter > 0
counter = counter + 1
Loop
If Error <> 0 Then
Do While counter > 0
counter = counter + 1
Cells(counter, "A").Value = counter
Loop
End If
End Sub
For a loop that will run from 0..32767 repeatedly (the max an integer can hold)
Do While True
counter = (counter + 1) Mod 32768
...
Loop
If you dim counter as a Long the maximum will be 2147483647.
Could you not just do this? and not increment counter at all?
Sub InfiniteLoop()
Dim counter As Integer
counter = 1
Do While counter > 0
Loop
End Sub

For Loop Step -1

I want to know why this loop doesn't show anything in VB.NET.
I think this code will create an infinite loop. But it doesn't show anything.
Dim i as Integer
For i = 1 to 3 Step - 1
MessageBox.Show(i)
Next
Is that loop different with this code (in java/c#) ?
for(int i = 1;i <= 3;i--)
{
// print i
}
http://msdn.microsoft.com/en-us/library/5z06z1kb.aspx
With a negative step size the loop only executes if counter >= end. So in this case with i = 1, that is less than the ending value so the loop doesn't execute at all.
It doesn't show anything because you are running the counter backwards without reversing the start and end conditions.
Think of the loop like this:
Dim counter As Int32 = 1
Do
If counter <= 1 Then
Exit Do
End If
Console.WriteLine("The counter is at " & counter)
counter +=1
Loop
Obviously this won't work properly.
You need to reverse the start and end conditions:
For counter = 3 To 1 Step -1
Console.WriteLine("counter: " & counter)
Next
For i = 1 to 3 Step - 1
It won't create an infinite loop. The loop will simply be skipped, because you can't get from 1 to 3 with a step value of -1.
Is that loop different with this code (in java/c#) ?
This loop will also end immediately, because the initial value (i = 1) meets the exit condition (i <= 3).

Find Common Values From Datagridview

I Have about 10 (DatagridView Count may varies As per User Selected Files From 2 to 10) Datagridview ,So How can i find common value from all Datagridviews ??
Comment If you need more brief details
Below is mine but It find common from 2 -2 datagridviews
For i As Integer = 1 To dgvCont
For j As Integer = 0 To Main.DGVM(i).Rows.Count - 1
For Each Val As DataGridViewRow In Main.DGVM(i + 1).Rows
If Val.Cells(0).Value = Main.DGVM(i).Rows.Item(j).Cells(0).Value Then
Dim cm As String = Val.Cells(0).Value
If cm = "" Then
Else
Analysis.lvCmn.Items.Add(Val.Cells(0).Value)
End If
End If
Next
Next
Next
I understand that you want to set two nested loops accounting for an undetermined number of elements (items in an array of DataGridView, I presume), performing the checks you want:
For count1 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont
For row1 As Integer = 0 To Main.DGVM(count1).Rows.Count - 1
If (Main.DGVM(count1).Rows(row1).Cells(0).Value Is Nothing) Then Continue For
Dim val1 As String = Main.DGVM(count1).Rows(row1).Cells(0).Value
Dim found As Boolean = False
For count2 As Integer = 1 To dgvCont 'Assuming indices from 1 to dgvCont
If (count2 = count1) Then Continue For
For row2 As Integer = 0 To Main.DGVM(count2).Rows.Count - 1
If (Main.DGVM(count2).Rows(row2).Cells(0).Value Is Nothing) Then Continue For
Dim val2 As String = Main.DGVM(count2).Rows(row2).Cells(0).Value.ToString()
If val1 = val2 Then
Dim cm As String = val1
If cm = "" Then
Else
Analysis.lvCmn.Items.Add(val1)
End If
found = True
Exit For 'By assuming that you want to stop searching after finding a match
End If
Next
If (found) Then Exit For 'By assuming that you want to stop searching after finding a match
Next
Next
Next
Your code is not too clear (neither what you want); but this should give you a good enough start to carry out the implementation you are looking for. Bear in mind that this code (like yours) only considers one column (first one); in case of wanting to iterate through all the columns, you would have to add further nested loops accounting for that.