Saturday, April 11, 2020

VB.Net- Lecture 4


MARWARI COLLEGE, RANCHI
(AN AUTONOMOUS UNIT OF RANCHI UNIVERSITY FROM 2009)

- Prakash Kumar, Dept. of CA
-Raju Manjhi, Dept of CA
__________________________________________________________________________________

A Select Case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each select case.
Syntax:
The syntax for a Select Case statement in VB.Net is as follows:
Select [ Case ] expression
    [ Case expressionlist
        [ statements ] ]
    [ Case Else
        [ elsestatements ] ]
End Select

e.g.

Module Module1

    Sub Main()
        Dim x As Integer
        x = 1
        Select Case x
            Case 1
                Console.WriteLine("CA")
            Case 2
              
Console.WriteLine("IT")
            Case Else
                Console.WriteLine("Invalid choice")
        End Select
        Console.WriteLine("Your choice is {0} ", x)
        Console.Read()
    End Sub
End Module

VB.Net - Loops

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages:


VB.Net provides following types of loops to handle looping requirements.
Loop Type
Description
Do Loop
It repeats the enclosed block of statements while a Boolean condition is True or until the condition becomes True. It could be terminated at any time with the Exit Do statement.
For...Next
It repeats a group of statements a specified number of times and a loop index counts the number of loop iterations as the loop executes.
For Each...Next
It repeats a group of statements for each element in a collection. This loop is used for accessing and manipulating all elements in an array or a VB.Net collection.
While... End While
It executes a series of statements as long as a given condition is True.
With... End With
It is not exactly a looping construct. It executes a series of statements that repeatedly refer to a single object or structure.
Nested loops
You can use one or more loops inside any another While, For or Do loop.

  Note:
For Each...Next
With... End With
Nested loops
Will be discussed later.


B. Loop
     i.  For ………Next
1. Module Module1

    Sub Main()
        Dim i As Integer
        For i = 1 To 5 Step 1
            Console.WriteLine(i)
        Next
       
        Console.Read()
    End Sub
End Module
2.
Module Module1

    Sub Main()
        Dim i As Integer
        For i = 5 To 1 Step -1
            Console.WriteLine(i)
        Next i
       
        Console.Read()

    End Sub

End Module
ii. While ….. End While
 Module Module1

    Sub Main()
        Dim i As Integer
        i = 1
        While i <= 5
            Console.WriteLine(i)
            i = i + 1
        End While
        Console.Read()
    End Sub
End Module
iii. Do ….. Loop
Module Module1
    Sub Main()
        Dim i As Integer
        i = 1
        Do
            Console.WriteLine(i)
            i = i + 1
            If (i > 5) Then
                Console.Read()
                Exit Sub
            End If
        Loop
    End Sub
End Module

Go through the video for execution of the programs and practice more questions:



Practice programs:
1. Nested Loop :Pyramid of asterix
Module Module1

    Sub Main()
        Dim i, j, k As Integer
        For i = 1 To 5
            For k = 5 To i Step -1
                Console.Write(" ")
            Next
            For j = 1 To i
                Console.Write("* ")
            Next
            Console.WriteLine()
        Next
        Console.Read()
    End Sub

End Module

2. Reverse of a number
Module Module1

    Sub Main()
        Dim num, re, reve As Integer
        Console.Write("Enter a number to reverse: ")
        num = Console.ReadLine
        While (num > 0)
            re = num Mod 10
            reve = reve * 10 + re
            num = num \ 10 ' Integer Division
        End While
        Console.WriteLine("Reverse=" & reve)

        Console.Read()
    End Sub


End Module

2. Matrix
Module Module1

    Sub Main()
        Dim a(,) As Integer = {{1, 2}, {3, 4}}

        Dim i, j As Integer
        For i = 0 To 1
            For j = 0 To 1
                Console.Write("   " & a(i, j))
            Next
            Console.WriteLine()
        Next
        Console.Read()
    End Sub


End Module

Note:
array is declared as 
a(n)- single dimension
a(m,n)- double dimension






No comments:

Post a Comment