Translate

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



Wednesday 5 June 2013

If Else in VB.NET with Odd Even Checking Example


If else Control Structure in VB.NET with Odd Even Checking Examples



In this blog we another example program of  If Else control structure in VB.NET.

Module Example7
    Sub main()
        Dim Num As Integer
        Console.Write("Enter a Number: ")
        Num = Console.ReadLine()
        If Num Mod 2 = 0 Then
            Console.WriteLine(" Even Number")
        Else
           Console.WriteLine(" Odd Number")
        End If
    End Sub
 End Module

 


In the above program a new thing we saw Mod use of this operater is it returns remainder  in the division and all others are same as in the previous example program.  The above program checks whether the entered number is Odd number or Even number. Even number is a number which is divisible by 2 . Odd number is opposite of even number i.e  number not divisible by2.

Tuesday 4 June 2013

Checking Positive or Negative Number using VB.NET


Control Structure

In the previous blog we discussed if else control structure now we will some example programs.

Module Example6
    Sub main()
        Dim Num As Integer
        Console.Write("Enter a number greater than 0 or less than 0 : ")
        Num = Console.ReadLine

        If Num > 0 Then
            Console.WriteLine(" Positive Number ")
        Else
            Console.WriteLine(" Negative Number ")
        End If
        Console.ReadKey()


    End Sub
End Module

The above program check whether the given number is positive or negative. Positive number is one which is greater than 0. Negative number is one which is less than 0. Using above program we can find out given number is Positive or Negative.


 
check google pagerank

Monday 3 June 2013

Decision Making using VB.NET


Control Structure
Let us SEE control structure in VB.NET

Consider you go to fruit shop and ask for whether the shop sell apple or not just by asking the you get the answer similarly you are going to this in VB.NET.  First let us see an example .

Module Example5
    Sub main()
        Dim Asked As String = "apple"
        If Asked = "apple" Then
            Console.WriteLine("Apple Avalable!")
        Else
           Console.WriteLine("Not Avalable!")
        End If
        Console.ReadKey()
    End Sub
End Module

 
In the above example a string variable Asked is assigned with “apple” . One important thing I forgot to mention in the previous blog that string values are assigned with double quotes “ ” all other variables like Integer , Single (we have not seen yet ) are simple assign without quotes.

The statement
        If Asked = "apple" Then

Checks if  Asked variable value is “apple”  if it is statement in the if block is executed otherwise statement in the  Else block is executed.

Syntax of If Else Control  Structure
If condition then
Statements
Else
Statements
End If

We will see Examples in  If Else  Control Structure in the upcoming blogs.

Sunday 2 June 2013

String Data type in VB.NET



String Data type

In this blog we will the another datatype called 'String' this datatype stores texts .

Dim str as String =" Hello"

The variable str contains 'Hello' like this we can store any text in String datatype.

Let us see an example.

Module Example4
    Sub main()
        Dim Greet As String = "Hello, "
        Dim Msg As String
      
       Console.Write("Enter Your Name: ")
        Msg = Console.ReadLine()

        Console.WriteLine()
        Console.WriteLine(Greet & Msg)
        Console.ReadKey()
    End Sub
End Module




The above example have two variable Greet and Msg, Greet is assigned with the text " Hello" and Msg will get value during runtime output will display Hello with value containing in Msg variable.

In the next blog we will see Control Structure we mentioned in first blog.