5
(1)

[PRACTICAL] WRITE, TEST AND DEBUG LOOP, ARRAY AND OPERATOR BASED VB.NET PROGRAMS

Are you looking for a step-by-step guide to Write, test and debug loop, array and operator based vb.net programs? here are the steps to write, test and debug loop, array and operator based vb.net programs.

Do Loop

Do { While | Until } condition
    [ statements ]
    [ Continue Do ]
    [ statements ]
    [ Exit Do ]
    [ statements ]
Loop
-or-
Do
    [ statements ]
    [ Continue Do ]
    [ statements ]
    [ Exit Do ]
    [ statements ]
Loop { While | Until } condition

Program

Module loops
   Sub Main()
      ' local variable definition 
      Dim a As Integer = 10
      'do loop execution 
      Do
          Console.WriteLine("value of a: {0}", a)
          a = a + 1
      Loop While (a < 20)
      Console.ReadLine()
   End Sub
End Module

Output

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

For…Next Loop

For counter [ As datatype ] = start To end [ Step step ]
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ counter ]

Program

Module loops
   Sub Main()
      Dim a As Byte
      ' for loop execution 
      For a = 10 To 20
          Console.WriteLine("value of a: {0}", a)
      Next
      Console.ReadLine()
   End Sub
End Module

Output

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20

For Each…Next Loop

For Each element [ As datatype ] In group
    [ statements ]
    [ Continue For ]
    [ statements ]
    [ Exit For ]
    [ statements ]
Next [ element ]

Program

Module loops
   Sub Main()
      Dim anArray() As Integer = {1, 3, 5, 7, 9}
      Dim arrayItem As Integer
     'displaying the values
      For Each arrayItem In anArray
          Console.WriteLine(arrayItem)
      Next
      Console.ReadLine()
   End Sub
End Module

Output

1
3
5
7
9

While… End While Loop

While condition
    [ statements ]
    [ Continue While ]
    [ statements ]
    [ Exit While ]
    [ statements ]
End While

Program

Module loops
   Sub Main()
      Dim a As Integer = 10
      ' while loop execution '
      While a < 20
          Console.WriteLine("value of a: {0}", a)
          a = a + 1
      End While
      Console.ReadLine()
   End Sub
End Module

Output

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

Arrays

All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.

Dim intData(30)	  ' an array of 31 elements
Dim strData(20) As String	' an array of 21 strings
Dim twoDarray(10, 20) As Integer	'a two dimensional array of integers
Dim ranges(10, 100)	 'a two dimensional array

Program

Module arrayApl
   Sub Main()
      Dim n(10) As Integer  ' n is an array of 11 integers '
      Dim i, j As Integer
      ' initialize elements of array n '         
      For i = 0 To 10
          n(i) = i + 100 ' set element at location i to i + 100 
      Next i
      ' output each array element's value '
      For j = 0 To 10
          Console.WriteLine("Element({0}) = {1}", j, n(j))
      Next j
      Console.ReadKey()
   End Sub
End Module

Output

Element(0) = 100
Element(1) = 101
Element(2) = 102
Element(3) = 103
Element(4) = 104
Element(5) = 105
Element(6) = 106
Element(7) = 107
Element(8) = 108
Element(9) = 109
Element(10) = 110

Example: Write a Program to find the prime numbers from 2 to 100 Using for loop.

Module loops
   Sub Main()
      ' local variable definition 
      Dim i, j As Integer
      For i = 2 To 100
          For j = 2 To i
              ' if factor found, not prime
              If ((i Mod j) = 0) Then
                  Exit For
              End If
          Next j
          If (j > (i \ j)) Then
              Console.WriteLine("{0} is prime", i)
          End If
      Next i
      Console.ReadLine()
   End Sub
End Module

Output

2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime

2.a Write a Console Program to find the sum of 10 number using Do While Loop.

Program:

Module Module1

    Sub Main()
        Dim i As Integer
        i = 1
        Dim sum As Integer
        Do While i <= 10
            sum = sum + i
            i = i + 1
        Loop
        Console.WriteLine("Sum of 10 Num is " & sum)
        Console.ReadLine()
    End Sub

End Module

Output:

2.b Write a Console Program to find the Average of 10 number using Do..Loop While Loop.

Program:

Module Module1

    Sub Main()
        Dim i As Integer
        i = 1
        Dim sum As Integer
        Dim avg As Integer
        Do While i <= 10
            sum = sum + i
            i = i + 1
        Loop
        avg = sum / 10
        Console.WriteLine("Avg of 10 Num is " & avg)
        Console.ReadLine()
    End Sub

End Module

Output:

2.c Write a Console Program to find the prime numbers from 2 to 100 Using for loop.

Program:

Module Module1

    Sub Main()
        Dim i, j As Integer
        For i = 2 To 100
            For j = 2 To i
                ' if factor found, not prime
                If ((i Mod j) = 0) Then
                    Exit For
                End If
            Next j
            If (j > (i \ j)) Then
                Console.WriteLine("{0} is prime", i)
            End If
        Next i
        Console.ReadLine()

    End Sub

End Module

Output:

2.d Write a Console Program to take 10 input values from user and print 10 values using Array.

Program:

Module Module1

    Sub Main()
        Dim a(10) As Integer
        Console.WriteLine("Enter 10 Number")
        For i As Integer = 0 To 10
            a(i) = CInt(Console.ReadLine())
        Next
        For i As Integer = 0 To 10
            Console.WriteLine("Num is " & a(i))
        Next
        Console.ReadLine()
    End Sub

End Module

Output:

2.e Write a Console Program for Calculator to perform operation like +,-,*,/,%.

Program:

Module Module1

    Sub Main()
        Dim a As Double
        Dim b As Double
        Dim c As Double

        Console.WriteLine("Enter 2 Values")
        a = CDbl(Console.ReadLine())
        b = CDbl(Console.ReadLine())
        Console.WriteLine("Select Operation")
        Console.WriteLine("Press 1 for Sum")
        Console.WriteLine("Press 2 for Substraction")
        Console.WriteLine("Press 3 for Multiplication")
        Console.WriteLine("Press 4 for Division")
        Console.WriteLine("Press 5 for Modulo")
        c = CInt(Console.ReadLine())
        Select Case c
            Case 1 : Console.WriteLine("Sum of 2 Num is " & (a + b))
            Case 2 : Console.WriteLine("Substraction of 2 Num is " & (a - b))
            Case 3 : Console.WriteLine("Multiplication of 2 Num is " & (a * b))
            Case 4 : Console.WriteLine("Division of 2 Num is " & (a / b))
            Case 5 : Console.WriteLine("Modulo of 2 Num is " & (a Mod b))
        End Select
        Console.ReadLine()
    End Sub

End Module

Output:

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

As you found this post useful...

Share this post on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?