Chapter 4 Performing calculations and manipulating data Expressions. The following will be discussed in this chapter 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.
Trang 1CHAPTER FOUR
Performing Calculations and Manipulating Data: Expressions
Trang 3McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
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
Trang 44.1 Using Expressions, Operators, and
Functions in Visual Basic NET Statements
• An expression tells the computer to manipulate
Trang 5McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
4.1 Using Expressions, Operators, and
Functions in Visual Basic NET Statements
(cont.)
• Statements
– Perform various tasks
• Evaluate expressions one at a time.
• Carry out tasks specified in expressions.
Trang 64.1 Using Expressions, Operators, and
Functions in Visual Basic NET Statements
(cont.)
• Expressions
– Single values or a combination of values,
operators, and/or functions that reduce to a single value.
– Evaluated by calculation or manipulation.
– Result in numeric, string, or Boolean values.
Trang 7McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
4.1 Using Expressions, Operators, and
Functions in Visual Basic NET Statements
Trang 84.1 Using Expressions, Operators, and
Functions in Visual Basic NET Statements
(cont.)
• Functions
– Descriptive names that specify a more complex
operation than that performed by an operator.
– Names are always followed by parentheses.
– The value between the parentheses is called the argument.
– The resulting value is called the return value.
Trang 9McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
4.1 Using Expressions, Operators, and
Functions in Visual Basic NET Statements
(cont.)
• More Complicated Expressions
– Operators and functions can be composed into
complicated expressions.
– A function’s argument may be complex.
• Ex X = Math.Sqrt(10 + Math.Sqrt(Y + 29))
Trang 104.1 Using Expressions, Operators, and
Functions in Visual Basic NET Statements
(cont.)
• Expressions as a Part of Statements
– An expression is always a part of a statement.
• Ex Z = Math.Sqrt(X + 2 + Y) + Math.Sqrt(4)
Trang 11McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
4.2 Arithmetic Expressions
• Arithmetic operators and functions must be
written according to Visual Basic NET syntax
Trang 124.2 Arithmetic Expressions (cont.)
• Arithmetic Operators and Operator Precedence
– Arithmetic operations must adhere to the
Trang 13McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
Trang 144.2 Arithmetic Expressions (cont.)
Trang 15McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
4.2 Arithmetic Expressions (cont.)
• Type Conversion Functions
– A number of functions will convert values from
one type to another.
Trang 164.2 Arithmetic Expressions (cont.)
• Avoiding Arithmetic Errors
– We must write arithmetic expressions carefully.
– We must avoid certain pitfalls.
Trang 17McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
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.
Trang 184.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()
Trang 19McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
• LTrim(), Rtrim(), and Trim()
– Remove leading and trailing spaces.
Trang 21McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
4.4 The Try/Catch Block Revisited
• Used to detect and respond to error conditions
• Can handle multiple error conditions
Trang 224.6 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
Trang 23McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
Trang 244.6 Logical Expressions (cont.)
– Logical Functions
• Common ones are IsNumeric() and IIf().
• IsNumeric() is True if the argument is a valid number.
• IIf() chooses between two alternate paths.
Trang 25McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
Chapter Summary
• Expressions are part of a statement
• Expressions can be numeric, string, or logical
• Expressions consist of constants, variables,
operators, and functions
• An operator is a symbol that specifies a simple
operation
Trang 26Chapter Summary (cont.)
• A function is a name that represents a complex
operation
• Function names always end in parentheses
• Arithmetic expressions consists of numeric
constants, variables, arithmetic operators, and
functions
• Arithmetic expressions are evaluated using
order of precedence rules
Trang 27McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights
reserved.
Chapter Summary (cont.)
• Errors may occur when evaluating expressions
• String expressions consist of string constants
and variables and string operators and functions
• Chr() and Asc() are used with strings to access
the ANSI table
• Logical expressions are composed of variables, constants, comparison operators, logical
operators, and logical functions
Trang 28Chapter Summary (cont.)
• A logical expression has the form of a
True/False question
• Comparison of numeric values is
straightforward
• Comparison of string values is more complex
• The logical function IIf() is used to choose one of two expressions