Breaking News
You are here: Home / Notes / SEM-4 / .NET PROGRAMMING / conditional statements in vb.net

conditional statements in vb.net

Different types of conditional statements

Statement Description
If … Then statement An If…Then statement consists of a boolean expression followed by one or more statements.
If…Then…Else statement An If…Then statement can be followed by an optional Else statement, which executes when the boolean expression is false.
nested If statements You can use one If or Else if statement inside another If or Else if statement(s).
Select Case statement Select Case statement allows a variable to be tested for equality against a list of values.
nested Select Case statements You can use one select case statement inside another select case statement(s).

 

If…Then Statement

if_statement

It is the simplest form of control statement, frequently used in decision making and changing the control flow of the program execution. Syntax for if-then statement is:

If condition Then 
[Statement(s)]
End If

 

Where, condition is a Boolean or relational condition and Statement(s) is a simple or compound statement. Example of an If-Then statement is:

If (a <= 20) Then
   c= c+1
End If

 

If…Then…Else Statement

if_else_statement

An If statement can be followed by an optional Else statement, which executes when the Boolean expression is false.

Syntax:

The syntax of an If…Then… Else statement in VB.Net is as follows:

If(boolean_expression)Then
   'true statements 
Else
  'false statements 
End If

 

Example:

Module decisions
   Sub Main()
       'local variable definition '
      Dim a As Integer = 100

      ' check the boolean condition using if statement 
      If (a < 20) Then
          ' if condition is true then print the following 
          Console.WriteLine("a is less than 20")
      Else
          ' if condition is false then print the following 
          Console.WriteLine("a is not less than 20")
      End If
      Console.WriteLine("value of a is : {0}", a)
      Console.ReadLine()
   End Sub
End Module

 

The If…Else If…Else Statement

An If statement can be followed by an optional Else if…Else statement, which is very useful to test various conditions using single If…Else If statement.

When using If… Else If… Else statements, there are few points to keep in mind.

  • An If can have zero or one Else’s and it must come after an Else If’s.
  • An If can have zero to many Else If’s and they must come before the Else.
  • Once an Else if succeeds, none of the remaining Else If’s or Else’s will be tested.

Syntax:

If(boolean_expression 1)Then
   ' Executes when the boolean expression 1 is true 
ElseIf( boolean_expression 2)Then
   ' Executes when the boolean expression 2 is true 
ElseIf( boolean_expression 3)Then
   ' Executes when the boolean expression 3 is true 
Else 
   ' executes when the none of the above condition is true 
End If

Example:

Module decisions
   Sub Main()
      'local variable definition '
      Dim a As Integer = 100
      ' check the boolean condition '
      If (a = 10) Then
          ' if condition is true then print the following '
          Console.WriteLine("Value of a is 10") '
      ElseIf (a = 20) Then
          'if else if condition is true '
          Console.WriteLine("Value of a is 20") '
      ElseIf (a = 30) Then
          'if else if condition is true  
          Console.WriteLine("Value of a is 30")
      Else
          'if none of the conditions is true 
          Console.WriteLine("None of the values is matching")
      End If
      Console.WriteLine("Exact value of a is: {0}", a)
      Console.ReadLine()
   End Sub
End Module

 

Nested If Statements

It is always legal in VB.Net to nest If-Then-Else statements, which means you can use one If or ElseIf statement inside another If ElseIf statement(s).

Syntax:

If( boolean_expression 1)Then
   'Executes when the boolean expression 1 is true 
   If(boolean_expression 2)Then
         'Executes when the boolean expression 2 is true 
   End If
End If

Example:

Module decisions
   Sub Main()
      'local variable definition
      Dim a As Integer = 100
      Dim b As Integer = 200
      ' check the boolean condition 
      If (a = 100) Then
          ' if condition is true then check the following 
          If (b = 200) Then
              ' if condition is true then print the following 
              Console.WriteLine("Value of a is 100 and b is 200")
          End If
      End If
      Console.WriteLine("Exact value of a is : {0}", a)
      Console.WriteLine("Exact value of b is : {0}", b)
      Console.ReadLine()
   End Sub
End Module

 

Select Case Statement

switch_statement

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

Select [ Case ] expression
    [ Case expressionlist
        [ statements ] ]
    [ Case Else
        [ elsestatements ] ]
End Select

Example

Module decisions
   Sub Main()
      'local variable definition
      Dim grade As Char
      grade = "B"
      Select grade
          Case "A"
              Console.WriteLine("Excellent!")
          Case "B", "C"
              Console.WriteLine("Well done")
          Case "D"
              Console.WriteLine("You passed")
          Case "F"
              Console.WriteLine("Better try again")
          Case Else
              Console.WriteLine("Invalid grade")
      End Select
      Console.WriteLine("Your grade is  {0}", grade)
      Console.ReadLine()
   End Sub
End Module

 

 

Download Paper Solutions from Below Link

About admin

Scroll To Top