Translate

Tuesday, 2 July 2013

Logical Operator And in Visual Basic.NET




 In this blog we will see Logical operator 'And ' in If Else Statement. Let us see an example progam using 'And' Operator.



 Module Example14
    Sub Main()
        Dim age As Integer
        Dim gender As String
        Console.WriteLine("Enter Your Age:")
        age = Console.ReadLine()
        Console.WriteLine("Enter your Gender M/F:")
        gender = Console.ReadLine
        If age >= 65 And gender = "M" Then
            Console.WriteLine("Senior Citizen Male")
        End If
        Console.ReadKey()
    End Sub
End Module

The above example shows use of  'And' operator.  If both age and gender  conditions are true then statements follows this condition will execute otherwise not.

Monday, 17 June 2013

Skip numbers to generate Odd Numbers


In the previous blogs we discussed about For loop and examples using it. Now we will see how to skip  numbers to generate Odd number  in for Loop.


Module Example13
    Sub main()
        Dim i As Integer
        For i = 1 To 25 Step 2
            Console.WriteLine(i)
        Next
        Console.ReadKey()
    End Sub
End Module

The above example shows how to generate Odd numbers just by adding Step keyword with For loop.

Next we will see another example to generate numbers in descending order.

Module Example14
    Sub main()
        Dim i As Integer
        For i = 25 To 1 Step -1
            Console.WriteLine(i)
        Next
        Console.ReadKey()
    End Sub

End Module

Thursday, 13 June 2013

For Loop Example Program


Let us see an example program using for loop . This example show how to add sequence numbers using for loop.

Module Example12
    Sub main()
        Dim StartNum, EndNum As Integer
        Dim sum As Integer = 0
        Console.Write("Enter Start Number:")
        StartNum = Console.ReadLine()
        Console.Write("Enter End Number:")
        EndNum = Console.ReadLine()
        For I As Integer = StartNum To EndNum
            sum = sum + I
        Next
        Console.WriteLine("Sum of " & StartNum & " to " & EndNum & " is :" & sum)
        Console.ReadKey()
    End Sub

End Module
The above Example add numbers from StartNum to EndNum  and produce sum of these sequence  numbers.



Wednesday, 12 June 2013

For Loop in VB.NET

In this Blog I will show you loop. If want to do an action repeatedly you can use loop. VB.NET have 3 types of loop first we will see For loop in this blog other two loops will see later.


 Module Example11
    Sub main()
        Dim val As Integer
        For val = 1 To 10
            Console.WriteLine(val)
        Next
        Console.ReadKey()
    End Sub
End Module

The above example shows how For loop is used to display numbers from 1 to 10 the value starts from 1 displays using Console.Writeline statement  and moves to Next statement the next statement increase the val from 1 to 2 likewise it keeps on increasing the val  by 1 if the val reaches 11 the loop finishes and Console.Readkey() will execute.

Monday, 10 June 2013

Select Case In VB.NET

In this blog we will see alternate to If Else If  Structure i.e Select Case statement

Syntax

Select Case  Text Expression

Case  Expression1
Statment1

Case Expression2
Statement2

Else
Statement
End Select

Let us see an example using Select Case Statement.
Module Example10

    Sub Main()
        Dim MonthNum As Integer

        Console.WriteLine("Enter Month Name:")
        MonthNum = Console.ReadLine()

        Select Case MonthNum
            Case 1
                Console.WriteLine("January")
            Case 2
                Console.WriteLine("February")
            Case 3
                Console.WriteLine("March")
            Case 4
                Console.WriteLine("April")
            Case 5
                Console.WriteLine("May")
            Case 6
                Console.WriteLine("June")
            Case 7
                Console.WriteLine("July")
            Case 8
                Console.WriteLine("August")
            Case 9
                Console.WriteLine("September")
            Case 10
                Console.WriteLine("October")
            Case 11
                Console.WriteLine("November")
            Case 12
                Console.WriteLine("December")
            Case Else
                MsgBox("Invalid Input")
        End Select
        Console.ReadKey()
    End Sub

End Module

The above example is rewriteen of If Else If Example. You can use If Else If  or Select Case nothing is difference in performance.





Saturday, 8 June 2013

Get Month Name Example using VB.NET


Get Month Name

 This blog shows an example how to get month name by entering month no in VB.NET

Module Example9
    Sub main()
   Dim MonthNum As Integer
  Console.Write("Enter a Month Number 1-12 :")
  MonthNum = Console.ReadLine()
If MonthNum = 1 Then
            Console.WriteLine("January")
Else If MonthNum = 2 Then
           Console.WriteLine(" February ")
 Else If MonthNum = 3 Then
           Console.WriteLine(" March")
 Else If MonthNum = 4 Then
           Console.WriteLine(" April ")
 Else If MonthNum = 5 Then
           Console.WriteLine(" May ")
 Else If MonthNum = 6 Then
           Console.WriteLine(" June ")
 Else If MonthNum = 7 Then
           Console.WriteLine(" July")
 Else If MonthNum = 8 Then
           Console.WriteLine(" August ")
 Else If MonthNum = 9Then
           Console.WriteLine(" September ")
 Else If MonthNum = 10 Then
           Console.WriteLine(" October ")
 Else If MonthNum = 11 Then
           Console.WriteLine(" November ")
 Else If MonthNum = 12 Then
           Console.WriteLine(" December ")

 Else
 Console.WriteLine("Invalid Input")
        End If
    End Sub
 End Module

The above example is same as getting day name by entering day number which we have seen in previous blog

Friday, 7 June 2013

Nested If Else in VB.NET with Get Day Name Example



Nested If Else

In this blog we are going to see Nested If Else in VB.NET. Let us see the syntax


If Condition1 Then
Statements
Esle If Condition2 Then
Statements
Else If Condition3 Then
Statements
Else
Statements
End If

The above syntax first check the If Condition1 if it is true the statements followint the Condition1 will be executed and goes to End if , If it is false then check the Condition2 and it is true then Statements follows that Condition will be executed If it is false then checks the Condition3 and statements follows that condition will be executed if all the 3 Conditions occur false then Statements in the Else will be executed.

Let us see an example of finding day by inputing day number.

Module Example8
    Sub main()
        Dim DayNum As Integer
        Console.Write("Enter a Day Number1-7 :")
        DayNum = Console.ReadLine()
        If DayNum = 1 Then
            Console.WriteLine("Sunday")
        Else If DayNum = 2 Then
           Console.WriteLine(" Monday")
Else If DayNum = 3 Then
           Console.WriteLine(" Tuesday)
Else If DayNum = 4 Then
           Console.WriteLine(" Wednesday")
Else If DayNum = 5 Then
           Console.WriteLine(" Thursday")
Else If DayNum = 6 Then
           Console.WriteLine(" Friday")
Else If DayNum = 7Then
           Console.WriteLine(" Saturday")
Else
Console.WriteLine("Invalid Input")
        End If
    End Sub
 End Module