X



Home>Tutorial>

Operators Overview:Logical, Arithmetic, Comparison in Visual Basic.Net

Nuelson Penuel Sunday, September 6, 2020 Visual Basic.Net

Summary

In this tutorial, you will learn What is an operator?, Arithmetic Operators, Comparison Operators, Logical/Bitwise Operators, Bit Shift Operators, Assignment Operators and Miscellaneous Operators

What is Operator?

An operator refers to a symbol that instructs the compiler to perform a specific logical or mathematical manipulation. The operator performs the operation on the provided operands. Visual Basic.Net comes with various types of operators. We will be discussing these in this tutorial.

Arithmetic Operators

You can use arithmetic operators to perform various mathematical operations in VB.NET. They include:
  • ^ This is used for raising an operand to the power of another operand
  • + This is used for adding two operands.
  • - This is used for subtracting the second operand from the first operand.
  • * It is used for multiplying both operands.
  • / It is used for dividing an operand against another. It returns a floating point result.
  • \ It is used for dividing an operand against another. It returns an integer result.
  • MOD known as the modulus operator. It returns the remainder after division.
Let us demonstrate how to use these operators using console application in VB.Net. So create a console application and type or copy the following codes For steps on how to create console application visit here

Module Module1
    Sub Main()

        Dim num1 As Integer 
        Dim num2 As Integer 

Console.WriteLine("Enter first number")
num1=Int(Console.ReadLine())
Console.WriteLine("Enter second number")
num2=Int(Console.ReadLine())

Console.WriteLine("Your inputs are num1=" & num1 & " num2=" & num2)

'for addition operator
Console.WriteLine("num1 + num2=" & num1+ num2)

'for subtraction operator
Console.WriteLine("num1 - num2=" & num1 - num2)
 
'for multiplication operator
Console.WriteLine("num1 * num2=" & num1 * num2)

'for division operator
Console.WriteLine("num1 / num2=" & num1 / num2)

'for \ operator. This returns only integer value
Console.WriteLine("num1 \ num2=" & num1\ num2)

'for power (^) operator
Console.WriteLine("num1 ^ num2=" & num1 ^ num2)

'for MoD operator
Console.WriteLine("num1 MOD num2=" & num1 MOD num2)
        Console.ReadLine()

    End Sub

End Module
Click the Start button to execute the code. You should get your results based on your inputs

Comparison Operators

Like every other programming languages; in Visual Basic.Net, these operators are used for making comparisons between variables. They include the following:
  • = for checking whether the two operands have equal values or not. If yes, the condition will become true.
  • <> for checking if the value of the left operand is greater than that of the right operand. then condition will become true.
  • > for checking whether the value of the left operand is less than that of the right operand. If yes, the condition will become true.
  • < for checking whether the value of the left operand is greater than or equal to that of the right operand. If yes, the condition will become true.
  • >= for checking whether the two operands have equal values or not. If yes, the condition will become true.
  • <= for checking whether the value of the left operand is less than or equal to that of the right operand. If yes, the condition will become true.
Let us demonstrate how to use these using console application. In the example given, we are going to demonstrate how to accept age as input from users then output if eligible to vote or not.

Module Module1

    Sub Main()
        Dim age As Integer
        Console.WriteLine("Welcome! Please enter your age to check vote eligibility")
        age = Int(Console.ReadLine())
        If age = 18 Then
           Console.WriteLine("You are eligible to vote")
        ElseIf age > 18 Then
Console.WriteLine("You are eligible to vote")
ElseIf age >= 18 Then
Console.WriteLine("You are eligible to vote")
ElseIf age < 18 Then
Console.WriteLine("You are not eligible to vote")
ElseIf age <= 17 Then
Console.WriteLine("You are eligible to vote. ")
End If

        Console.ReadLine()

    End Sub

End Module	
Click the Start button from the toolbar to run the code. You should get your result based on your input

Logical/Bitwise Operators

Logical operators help the programmer to make logical decisions. Examples are
  • AND: Only true when both conditions are true.
  • OR: True when any of the conditions is true.
  • Not The logical/bitwise NOT. To reverse operand's logical state. If true, the condition becomes False and vice versa.
  • XOR: Returns False if expressions are all True or False. Otherwise, it returns True.
  • AndAlso It is also known as the logical AND operator. Only works with Boolean data by performing short-circuiting.
  • OrElse It is also known as the logical OR operator. Only works with Boolean data by performing short-circuiting.
  • IsFalse Determines whether expression evaluates to False.
  • IsTrue Determines whether expression evaluates to True.
Let us demonstrate how to use these operators. Below is a typical example Module Module1 Sub Main() Dim var1 As Boolean = True Dim var2 As Boolean = True Dim var3 As Integer = 10 Dim var4 As Integer = 100 If (var1 And var2) Then Console.WriteLine("var1 And var2 - is true") End If If (var1 Or var2) Then Console.WriteLine("var1 Or var2 - is true") End If If (var1 Xor var2) Then Console.WriteLine("var1 Xor var2 - is true") End If If (var3 And var4) Then Console.WriteLine("var3 And var4 - is true") End If If (var3 Or var4) Then Console.WriteLine("var3 Or var4 - is true") End If 'Only logical operators If (var1 AndAlso var2) Then Console.WriteLine("var1 AndAlso var2 - is true") End If If (var1 OrElse var2) Then Console.WriteLine("var1 OrElse var2 - is true") End If var1 = False var2 = True If (var1 And var2) Then Console.WriteLine("var1 And var2 - is true") Else Console.WriteLine("var1 And var2 - is not true") End If If (Not (var1 And var2)) Then Console.WriteLine("var1 And var2 - is true") End If Console.ReadLine() End Sub End Module Run the code by clicking the Start button from the toolbar and you will get your result.

Bit Shift Operators

In Visual basic, bit shift operators are used for performing shift operations on binary values.
  • And Known as the Bitwise AND Operator. It copies a bit to result if it is found in both operands.
  • Or Known as the Binary OR Operator. It copies a bit if found in either operand.
  • Xor The Binary XOR Operator. For copying a bit if set in one of the operands rather than both.
  • Not It is known as the Binary Ones Complement Operator. It is a unary operator that 'flips' the bits.
Let us demonstrate how to use these operators in the below example:

Module Module1

    Sub Main()

        Dim num1 As Integer 
        Dim num2 As Integer 
        Dim result As Integer 
Console.WriteLine("Enter any value for num1")
num1 = Int(Console.ReadLine())

Console.WriteLine("Enter any value for num2")
num2 = Int(Console.ReadLine())

        result = num1 Or num2
        Console.WriteLine(" num1 Or num2= ", result)
        
result = num1 And num2

        Console.WriteLine(" num1 And num2= ", result)
       
result = Not num2

        Console.WriteLine("result= Not num2: ", result)

 result = num1 Xor num2

 Console.WriteLine("result=num1 Xor num2: ", result)
        Console.ReadLine()

    End Sub
End Module

Assignment Operators

Assignment operators are used to assign value to a Variable.
  • = It assigns values from the left side operands to the right side operands.
  • += Is known as the Add AND assignment operator. It will add the right operand to the left operand. Then the result will be assigned to the left operand.
  • -= Is known as the Subtract AND operator. It will subtract the right operand from left operand and assign the result to the left operand.
  • *= Is known as the Multiply AND operator. It will subtract the right operand from left operand and assign the result to the left operand.
Let us demonstrate how to use these operators in the below example:

Module Module1

    Sub Main()

        Dim num1 As Integer      
        Dim num2 As Integer = 4
        Dim result As Double = "0.5"

Console.WriteLine("Enter any numeric value")
num1=Int(Console.ReadLine())
    result  = num1
        Console.WriteLine("result  = num1", result)

        result += num1+num2
        Console.WriteLine(" result += num1 + num2: ", result)

        result -= num1
        Console.WriteLine(" result -= num1: ", result)

        result *= num2
        Console.WriteLine(" result *= num2", result)

        Console.ReadLine()

    End Sub
End Module
Now, run the code by hitting F5 from the keyboard

| | Views(591)

See Demo | Download File


Add your comment. Indicate code using code tag


Words: 0 | Chars: 0 | Reading Duration: 0



Images Attachment




Emmason Integratded Services(2017-2024)
All Rights Reserved
Designed and Maintained By Emmason Integrated Services