Introduction to Programming 1213114 –Subject 7

Expressions, Operations precedence, math and string functions

Arithmetic Expressions

·  Visual basic supports a number of different math operations that can be used in program statements. Table 5.1 shows these operations and the Visual Basic Symbol for each operation.

operation / Operator Symbol
Addition / +
Subtraction / -
Multiplication / *
Division / /
Integer Division / \
Modules / Mod
Exponentiation / ^

Table 7.1 Visual Basic Operation Symbol

Examples

Z=X+Y, X= Z^2*Y, Z=X*Y+R, Y=X/2

Example1:

Design a flowchart that accept two numbers from user, user can select one of six operations addition, subtraction, Modulus, Exponential, and multiplication, display your result in an output screen, test your design and convert it to a VB.NET program?

Operators Priority

Consider the following algebra statement

X= 9 + 4 * 2

Depending on how we look at it, x could have two values 26 or 17. If we do addition of 9+4 first and then multiply by 2, we get 26. However, if we multiply 4*2 first and then add 9, we get 17.

The answer we get depends on the order in which things happen. This order is referred to as order of precedence. In the precedence example, the default answer is 17, see the table 7.2 precedence order of Visual basic operator.

Operation / Operator Symbol
Exponentiation / ^
Division, Multiplication / /, *
Division Integer / \
Modules / mod
Addition, subtraction / +,-
String concatenation

Table 7.2 precedence order of Visual basic operator

Parentheses are used to control specifically which parts of a statement are performed first.

So precedence order to reach the value of X will be in this order

X= (9+4) * 2

X=13 * 2

X=26

Example2:

Give me the precedence order to reach the Value of Y

A=2 , B=3, X=4, C=2

1)  Y=A*X^2+B*X+C

2)  Y=(A*X)^2+B*(X+C)

Example3:

Z$= “University” & “Sharjah”

Result “UniversitySharjah” store in Z$

Associativity:

When operators of equal precedence appear together in an expression, for example multiplication and division, the compiler evaluates each operation as it encounters it from left to right. The following example illustrates this.

Dim n1 As Integer = 96 / 8 / 4

Dim n2 As Integer = (96 / 8) / 4

Dim n3 As Integer = 96 / (8 / 4)

The first expression evaluates the division96 / 8(which results in 12) and then the division12 / 4, which results in three. Because the compiler evaluates the operations forn1from left to right, the evaluation is the same when that order is explicitly indicated forn2. Bothn1andn2have a result of three. By contrast,n3has a result of 48, because the parentheses force the compiler to evaluate8 / 4first.

Character String Expressions

Visual Basic supports only one string operator, the concatenation operator. This operator combines two or more strings of text, similar to the way the addition operator is used combine two or more number.

The concatenation operator is the ampersand symbol &

Mathmatical Functions:

·  Absolute value: Math.Abs(Number)

·  Ceiling value : Math.Ceiling(Number)

·  Floor value : Math.Floor(Number)

·  Rounding value to a specific decimal digit: Math.Round(Number, digits)

·  The maximum of two numbers: Math.Max(Number1, Number2)

·  The minimum of two numbers: Math.Min(Number1, Number2)

·  Take a random number: Math.Floor(100 * Rnd())

Example 4:

Design the following form to implement the mentioned math functions:

Code:

Public Class Form1

Dim Number1, Number2, Result As Single

Dim digits As Byte

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Number1 = TextBox1.Text

Result = Math.Sqrt(Number1)

TextBox4.Text = Result

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

Number1 = TextBox1.Text

Result = Math.Abs(Number1)

TextBox4.Text = Result

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

Number1 = TextBox1.Text

Result = Math.Ceiling(Number1)

TextBox4.Text = Result

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

Number1 = TextBox1.Text

Result = Math.Floor(Number1)

TextBox4.Text = Result

End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

Number1 = TextBox1.Text

Number2 = TextBox2.Text

Result = Math.Max(Number1, Number2)

TextBox4.Text = Result

End Sub

Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

Number1 = TextBox1.Text

Number2 = TextBox2.Text

Result = Math.Min(Number1, Number2)

TextBox4.Text = Result

End Sub

Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click

Number1 = TextBox1.Text

digits = TextBox3.Text

Result = Math.Round(Number1, digits)

TextBox4.Text = Result

End Sub

Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click

Number1 = TextBox1.Text

Result = Math.Floor(Number1 * Rnd(Number1))

TextBox4.Text = Result

End Sub

End Class

Example5:

The Pythagorean Theorem describes the relationship between the lengths of the legs and the hypotenuse of a right triangle.

a 2 + b 2 = c 2

Analyze the problem and Design a form to find any leg of a Right Triangle when we input the other two legs using the Pythagorean Theorem.

·  download any Right Triangle image from the internet and design the following form

·  make the back color of the result with yellow color.

·  Be sure that at least two values where entered.

·  The clear button will clear values of textboxes and return the back of textboxes to white.

Analysis:

Inputs / Outputs / Process
a, b / c / c=a2+b2
a, c / b / b=c2-a2
b, c / a / a=c2-b2

The code:

Public Class Form1

Dim a, b, c As Single

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

If TextBox1.Text > "" And TextBox2.Text > "" Then

a = TextBox1.Text

b = TextBox2.Text

c = Math.Sqrt(a ^ 2 + b ^ 2)

TextBox3.Text = c

Else

MsgBox("One or more of the input values are missed", , "Error Input")

End If

End Sub

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

If TextBox2.Text > "" And TextBox3.Text > "" Then

c = TextBox3.Text

b = TextBox2.Text

a = Math.Sqrt(c ^ 2 - b ^ 2)

TextBox1.Text = a

Else

MsgBox("One or more of the input values are missed", , "Error Input")

End If

End Sub

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click

If TextBox1.Text > "" And TextBox3.Text > "" Then

c = TextBox3.Text

a = TextBox1.Text

b = Math.Sqrt(c ^ 2 - a ^ 2)

TextBox2.Text = b

Else

MsgBox("One or more of the input values are missed", , "Error Input")

End If

End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

TextBox1.Text = ""

TextBox2.Text = ""

TextBox3.Text = ""

End Sub

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

End

End Sub

End Class


Strings Functions[1][2]

Stringsstore collections of characters. There are many different functions you can use on Strings in the VB.NET language.

Example

Dim value As String = "dot"

There are many functionsused in strings manipulation. The following are some of:

Ucase and Lcase

This example uses theUCasefunction to return an uppercase version of a string and LCase to return the lower case of a string :

Example:

Dim phrase1 As String = "Hello World 1234"

Dim UpperCase As String = UCase(phrase1)

Dim LowerCase As String = LCase(phrase1)

Debug.WriteLine("Uppercase:" + UpperCase)

Debug.WriteLine("Lowercase:" + LowerCase)

String.Concat

Two or more strings can be concatenated into one. This requires the plus operator, or the String.Concat function in VB.NET. These syntax forms compile into the same code.

This program creates two Strings, value1 and value2 that are assigned to literals. We combine them with the plus-operator “+” and display the results with Debug.WriteLine. This works for two, three or even more strings. Then,we apply the String.Concat function. This receives two or more Strings and combines them into a single string. When we use the plus-operator on Strings, the String.Concat Function is called in the compiled code.


Example:

Dim value1 As String = "Crystal"

Dim value2 As String = "Gold"

Dim value3 = value1 + value2

Debug.WriteLine(value3)

Dim value4 = value1 + " " + value2

Debug.WriteLine(value4)

Dim value5 = String.Concat(value1, " ", value2)

Debug.WriteLine(value5)

Mid

This example uses theMidfunction to return a specified number of characters from a string.

Example:

Dim TestString As String = "Mid Function Demo"

Dim FirstWord As String = Mid(TestString, 1, 3)

Debug.WriteLine(FirstWord)

Dim LastWord As String = Mid(TestString, 14, 4)

Debug.WriteLine(LastWord)

Dim MidWords As String = Mid(TestString, 5)

Debug.WriteLine(MidWords)

Replace

Replace a string by another:

Example:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim TestString As String = "Shopping List"

Dim aString As String = Replace(TestString, "o", "i")

Debug.WriteLine(TestString)

Debug.WriteLine(aString)

End Sub

7

[1] http://www.dotnetperls.com/string-vbnet

[2] http://msdn.microsoft.com/en-us/library/dd789093.aspx