Matakuliah Tahun : T0063 – Pemrograman Visual : 2009 Performing Calculations and Manipulating Data: Expressions CHAPTER FOUR Expressions • Arithmetic expressions – To perform arithmetic operations. • String expressions – To manipulate string data. • Logical expressions – To select an appropriate action. Bina Nusantara University 2 Objectives • Describe the operators and functions used to create arithmetic, string, and logical expressions. • Explain why errors occur and how to avoid them. • Write logical expressions using comparison operators and logical operators. Bina Nusantara University 3 4.1 Using Expressions, Operators, and Functions in Visual Basic .NET Statements • An expression tells the computer to manipulate data. • The statement tells the computer what to do with the results of an expression. • Expressions are composed of values, operators, and functions. Bina Nusantara University 4 4.1 Using Expressions, Operators, and Functions in Visual Basic .NET Statements (cont.) • • • • Statements Expressions Operators Functions Bina Nusantara University 5 4.2 Arithmetic Expressions • Arithmetic operators and functions must be written according to Visual Basic .NET syntax. Bina Nusantara University 6 4.2 Arithmetic Expressions (cont.) • Arithmetic Operators and Operator Precedence – Arithmetic operations must adhere to the following order: • 1. Exponentiations • 2. Unary negations • 3. Multiplications and Divisions • 4. Integer divisions • 5. Modulus operations • 6. Additions and Subtractions Bina Nusantara University 7 4.2 Arithmetic Expressions (cont.) • Integer Division – Calculates the number of times one integer goes into another, discarding the reminder. – Mod calculates the reminder when one integer is divided by another. Bina Nusantara University 8 4.2 Arithmetic Expressions (cont.) • Arithmetic Functions – Math.Sqrt() – Math.Abs() – Math.Sign() – Fix() – Math.Exp() – Math.Log() – Rnd() Bina Nusantara University 9 4.2 Arithmetic Expressions (cont.) • Type Conversion Functions – A number of functions will convert values from one type to another. • CBool() CLng() • CDbl() CShort() • CDec() CSng() • CInt() CStr() Bina Nusantara University 10 4.2 Arithmetic Expressions (cont.) • Avoiding Arithmetic Errors – We must write arithmetic expressions carefully. – We must avoid certain pitfalls. – Overflow Errors • Occur when computer attempts to store a number that is too large for its data type. – Roundoff Errors • Occur when floating-point values or certain variables are used in a program. Bina Nusantara University 11 4.2 Arithmetic Expressions (cont.) – Problems Caused by Limited Significant Digits • Limitations on the number of significant digits. • Digits may be loss at run time. Bina Nusantara University 12 4.3 String Expressions • Manipulate strings using string operators and functions. – String Operators • & is the only string operator in Visual Basic .NET. • Joining two string values is called concatenation. – Functions Used with Strings • Format() – Converts a numeric value to a string and formats it. • Left(), Right(), and Mid() – Selects specific character(s) in a string. Bina Nusantara University 13 4.3 String Expressions – Functions Used with Strings (cont.) • StrConv() – Converts a string into a new string using conversion rules. • LTrim(), Rtrim(), and Trim() – Remove leading and trailing spaces. • Len() – Determines the number of characters in a string. • Space() – Indicates the number of spaces to create. Bina Nusantara University 14 4.3 String Expressions – Functions Used with Strings (cont.) • InStr() – Conducts a string search. • Asc() and Chr() – Access values in the ANSI table. • Replace() – Perform a search and replace routine. Bina Nusantara University 15 4.4 The Try/Catch Block Revisited • Used to detect and respond to error conditions. • Can handle multiple error conditions. Bina Nusantara University 16 4.5 Logical Expressions • Used to select an action from alternative actions. • Results in a True or False answer to a test. • Also known as Boolean expressions. Bina Nusantara University 17