Reputation: 57
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?
Upvotes: 1
Views: 474
Reputation: 43574
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
Upvotes: 1
Reputation: 701
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.
Upvotes: 0