arraysvisual-studio-2010numbersvb.net-2010skip

Why is my first set of numbers on the closed parenthesis stuck on 21?


I want the output to have the numbers on the first set to go from 0-20 and the second set to 0-100 but not 0,1,2,3,4,5 but 0,5,10 basically skipping each 5. Here's a screenshot of my code and output.

Code:

enter image description here

Output:

enter image description here


Solution

  • A couple options for you...

    Option 1:

    For a As Integer = 0 To 20
        Dim b As Integer = a * 5
        Console.WriteLine("index number:({0}) = {1}", a, b)
    Next
    

    Option 2:

    Dim b As Integer = 0
    For a As Integer = 0 To 20
        Console.WriteLine("index number:({0}) = {1}", a, b)
        b = b + 5
    Next
    

    Option 3:

    Dim a As Integer = 0
    Dim b As Integer = 0
    While a <= 20
        Console.WriteLine("index number:({0}) = {1}", a, b)
        a = a + 1
        b = b + 5
    End While
    

    In C#, you could do:

    for(int a=0,b=0; a<=20; a++,b+=5) {
      Console.WriteLine("index number:({0}) = {1}", a, b);
    }