0% found this document useful (0 votes)
49 views

Arithmetic Operators: The Following Is A Table of Arithmetic Operators in Visual Basic

Operators are used to process data in programming languages. Common arithmetic operators include addition, subtraction, multiplication, and division. Relational operators compare values and return Boolean results. The If/Then statement executes code conditionally based on a Boolean test. It can include Else and ElseIf clauses to handle different outcomes.

Uploaded by

Satyam Gahatraj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Arithmetic Operators: The Following Is A Table of Arithmetic Operators in Visual Basic

Operators are used to process data in programming languages. Common arithmetic operators include addition, subtraction, multiplication, and division. Relational operators compare values and return Boolean results. The If/Then statement executes code conditionally based on a Boolean test. It can include Else and ElseIf clauses to handle different outcomes.

Uploaded by

Satyam Gahatraj
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

An operator is a special symbol which indicates a certain process is carried out.

Operators in programming languages are taken from mathematics. Programmers work with data. The operators are used to process data.

Arithmetic operators
The following is a table of arithmetic operators in Visual Basic.
Symbol Name

+ * / \ Mod ^

Addition Subtraction Multiplication Division Integer Division Modulo Exponentiation

The following example shows arithmetic operations.


Option Strict On Module Example Dim a As Byte Dim b As Byte Dim c As Byte Dim Dim Dim Dim add As Byte sb As Byte mult As Byte div As Byte

Sub Main() a = 10 b = 11 c = 12 add = a + b + c sb = c - a mult = a * b div = CType(c / 3, Byte) Console.WriteLine(add) Console.WriteLine(sb)

Console.WriteLine(mult) Console.WriteLine(div) End Sub End Module

In the preceding example, we use addition, subtraction, multiplication and division operations. This is all familiar from the mathematics.
$ ./arithmetic.exe 33 2 110 4

Output of the example. Next we will show the distinction between normal and integer division.
Option Strict On Module Example Dim a As Single = 5 Dim b As Single = 2 Dim c As Single Sub Main() c = 5 / 2 Console.WriteLine(c) c = 5 \ 2 Console.WriteLine(c) End Sub End Module

In the preceding example, we divide two numbers using normal and integer division operator. Visual Basic has two distinct operators for division.

Dim a As Single = 5

We use floating point data types.


c = 5 / 2 Console.WriteLine(c)

This is the 'normal' division operation. It returns 2.5, as expected.


c = 5 \ 2 Console.WriteLine(c)

This is integer division. The result of this operation is always and integer. The c variable has value 2.
$ ./division.exe 2.5 2

Result of the division.exe program.

The last two operators that we will mention are modulo operator and exponentiation operator.
Console.WriteLine(9 Mod 4) ' Prints 1

The Mod operator is called the modulo operator. It finds the remainder of division of one number by another. 9 Mod 4, 9 modulo 4 is 1, because 4 goes into 9 twice with a remainder of 1. Modulo operator can be handy for example when we want to check for prime numbers. Finally, we will mention exponentiation operator.
Console.WriteLine(9 ^ 2) ' Prints 81

9 ^ 2 = 9 * 9 = 81

Relational Operators
Relational operators are used to compare values. These operators always result in a boolean value.
Symbol Meaning

< <= > >= == <> Is

less than less than or equal to greater than greater than or equal to equal to not equal to compares references

Relational operators are also called comparison operators.


Console.WriteLine(3 < 4) ' Prints True Console.WriteLine(3 = 4) ' Prints False Console.WriteLine(4 >= 3) ' Prints True

As we already mentioned, the relational operators return boolean values. Note that in Visual Basic, the comparison operator is (=). Not (==) like in C and C influenced languages. Notice that the relational operators are not limited to numbers. We can use them for other objects as well. Although they might not always be meaningful.
Option Strict On Module Example Sub Main() Console.WriteLine("six" = "six") ' Prints True ' Console.WriteLine("a" > 6) this would throw

' an exception Console.WriteLine("a" < "b") ' Prints True End Sub End Module

We can compare string objects too. Comparison operators in a string context compare the sorting order of the characters.
Console.WriteLine("a" < "b") ' Prints True

What exactly happens here? Computers do not know characters or strings. For them, everything is just a number. Characters are special numbers stored in specific tables. Like ASCII.
Option Strict On Module Example Sub Main() Console.WriteLine("a" < "b") Console.WriteLine("a is: {0}", Console.WriteLine("b is: {0}", End Sub End Module Asc("a")) Asc("b"))

Internally, the a and b characters are numbers. So when we compare two characters, we compare their stored numbers. The built-in Asc function returns the ASCII value of a single character.
$ ./compare.exe True a is: 97 b is: 98

In fact, we compare two numbers. 97 with 98.

Console.WriteLine("ab" > "aa") ' Prints True

Say we have a string with more characters. If the first characters are equal, we compare the next ones. In our case, the b character at the second position has a greater value than the a character. That is why "ab" string is greater than "aa" string. Comparing strings in such a way does not make much sense, of course. But it is technically possible. Finally, we will mention the Is operator. The operator checks if two object references refer to the same object. It does not perform value comparisons.
Option Strict On Module Example Sub Main() Dim o1 As Object = New Object Dim o2 As Object = New Object Dim o3 As Object o3 = o2 Console.WriteLine(o1 Is o2) Console.WriteLine(o3 Is o2) End Sub End Module

We create three objects and compare them with the Is operator.


Dim o1 As Object = New Object Dim o2 As Object = New Object

We declare and initialize two Object instances. The Object class is a base class for all classes in the .NET framework. We will describe it later in more detail.
Dim o3 As Object

The third variable is only declared.


o3 = o2

The o3 now refers to the o2 object. They are two references to the same object.
Console.WriteLine(o1 Is o2) Console.WriteLine(o3 Is o2)

In the first case, we get False. o1 and o2 are two different object. In the second case, we get True. o3 and o2 refer to the same object.

Explain IF THEN statement in Visual Basic?


Answer: To properly use the If/Then statement you must first use an If statement. If the If statement is true (True/False Boolean) then you would use a Then statement to tell it what to do when the If shows as true. Finally an End If statement. Example:

If TextBox1.Text = "Hello World!" Then 'Checks if "Hello World!" is written in the textbox. ListBox1.Items.Add("Hello") 'If "Hello World!" is written then it enters "Hello" into the listbox. End If 'Ends the If statement.

You can also use an Else statement after the Then statement. The Else statement tells it what to do if the statement is false. Example: If TextBox1.Text = "Hello World!" Then 'Checks if "Hello World!" is written in the textbox. ListBox1.Items.Add("Hello") 'If "Hello World!" is written then it enters "Hello" into the listbox. Else : ListBox1.Items.Add("How are you?") 'If "Hello World" is not written then it adds "How are you?" to the list box instead. End If 'Ends the If statement.

You can also use the ElseIf statement to string multiple If statements together. ElseIf statements are very

handy when you are using many If statements as it preserves space and is much easier to read.

If/Then/Else statements with ElseIf: If TextBox1.Text.Length = 0 Then TextBox1.Text = "01" ElseIf TextBox2.Text.Length = 0 Then TextBox2.Text = "02" ElseIf TextBox3.Text.Length = 0 Then TextBox3.Text = "03" ElseIf TextBox4.Text.Length = 0 Then TextBox4.Text = "04" ElseIf TextBox5.Text.Length = 0 Then TextBox5.Text = "05" ElseIf TextBox6.Text.Length = 0 Then TextBox6.Text = "06" ElseIf TextBox7.Text.Length = 0 Then TextBox7.Text = "07" End If

If/Then/Else statements without ElseIf: If TextBox1.Text.Length = 0 Then TextBox1.Text = "01" Else If TextBox2.Text.Length = 0 Then TextBox2.Text = "02" Else If TextBox3.Text.Length = 0 Then TextBox3.Text = "03" Else If TextBox4.Text.Length = 0 Then TextBox3.Text = "04" Else If TextBox5.Text.Length = 0 Then

TextBox3.Text = "05" Else If TextBox6.Text.Length = 0 Then TextBox3.Text = "06" Else If TextBox7.Text.Length = 0 Then TextBox2.Text = "07" End If End If End If End If End If End If End If

You might also like