Operators for Manipulating Data
This article illustrates how to use operators for manipulating data. Data manipulation involves performing calculations on numbers and manipulating strings. Programs manipulate and use variables in many ways, often depending on the type of the data.
Each data type has a number of operations - things that you can do to it. There are 3 major set of operators in most programming languages - Numeric or Arithmetic operators, Comparison operators and Logical operators. Let's know each set in detail.Numeric or Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
\ Integer division
Mod Modulus arithmetic
& String concatenationArithmetic operators for manipulating data include the familiar addition, subtraction, multiplication and division operators. There are some interesting variations such as the modulus (Mod) and integer division (\) available in visual basic. Take a look at the examples below to understand them better.
An Addition operation, the result here is 120
Number1=100
Number2=20
Result=Number1 + Number2A Subtraction operation, the result here is 10
Number1=20
Number2=10
Result=Number1 - Number2A Multiplication operation, the result here is 400
Number1=20
Number2=20
Result=Number1 * Number2A Division operation, the result here is 55.75
Number1=1115
Number2=20
Result=Number1 / Number2An Integer division operation, the result here is 55
Note that this operation does not round off the result but eliminates the decimal part all together. Number1=1115
Number2=20
Result=Number1 \ Number2A Mod operation, the result here is 15.
This operation returns the remainder after an integer division operation. Number1=1115
Number2=20
Result=Number1 Mod Number2A String concatenation operation, the result here is John Sun
Firstname="John"
Lastname="Sun"
Result=Firstname & " " & LastnameComparison Operators
= Equality
<> Inequality
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal toThese operators do not modify data. The purpose of a comparison is to produce a Boolean true or false value that can be used somewhere else in your program, usually used in conditional branching and conditional looping blocks. Take a look at the examples below.
An Equality check operation, the result here is TRUE
Number1=100
Number2=100
Result=Number1 = Number2An Inequality check operation, the result here is TRUE
Number1=100
Number2=20
Result=Number1 <> Number2A Less than operation, the result here is FALSE
Number1=200
Number2=20
Result=Number1 < Number2A Greater than operation, the result here is TRUE
Number1=200
Number2=20
Result=Number1 > Number2A Less than or equal to operation, the result here is FALSE
Number1=150
Number2=10
Result=Number1 <= Number2A Greater than or equal to operation, the result here is TRUE
Number1=150
Number2=150
Result=Number1 >= Number2Logical Operators
The last set of operators for manipulating data are the logical operators. There are two basic logical operators AND and OR, these operators have their own category because they only operate on boolean variables. Let's take a look at some examples.An AND operation, the result here is TRUE
Number1=100
Number2=100
Result= (Number1 = Number2) AND (Number1 < 200)An OR operation, the result here is TRUE
Number1=100
Number2=200
Result= (Number1 = Number2) OR (Number1 < 200)That covers all the basic operators for manipulating data, the operators mentioned here are some of the commonly used across many languages. There are more operators you will learn about as you start writing programs in visual basic.
Back to Visual Basic for Beginners
- Subtraction
* Multiplication
/ Division
\ Integer division
Mod Modulus arithmetic
& String concatenation
Number2=20
Result=Number1 + Number2
Number2=10
Result=Number1 - Number2
Number2=20
Result=Number1 * Number2
Number2=20
Result=Number1 / Number2
Number2=20
Result=Number1 \ Number2
Number2=20
Result=Number1 Mod Number2
Lastname="Sun"
Result=Firstname & " " & Lastname
<> Inequality
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
Number2=100
Result=Number1 = Number2
Number2=20
Result=Number1 <> Number2
Number2=20
Result=Number1 < Number2
Number2=20
Result=Number1 > Number2
Number2=10
Result=Number1 <= Number2
Number2=150
Result=Number1 >= Number2
Number2=100
Result= (Number1 = Number2) AND (Number1 < 200)
Number2=200
Result= (Number1 = Number2) OR (Number1 < 200)
