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.
CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions 4- Expressions • Arithmetic expressions – To perform arithmetic operations • String expressions – To manipulate string data • Logical expressions – To select an appropriate action McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4- 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4.1 Using Expressions, Operators, and Functions in Visual Basic NET Statements 4- • An expression tells the computer to manipulate data • The statement tells the computer what to with the results of an expression • Expressions are composed of values, operators, and functions McGraw 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4- 4.1 Using Expressions, Operators, and Functions in Visual Basic NET Statements (cont.) 4- • 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4.1 Using Expressions, Operators, and 4- Functions in Visual Basic NET Statements (cont.) • Operators – Symbols for a common operations such as addition or multiplication – Most operators are binary operators McGraw 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.) • 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4- 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)) McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4- 4.1 Using Expressions, Operators, and 4-10 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 + + Y) + Math.Sqrt(4) McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4.2 Arithmetic Expressions (cont.) • Arithmetic Functions – – – – – – – Math.Sqrt() Math.Abs() Math.Sign() Fix() Math.Exp() Math.Log() Rnd() McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 414 4.2 Arithmetic Expressions (cont.) • Type Conversion Functions – A number of functions will convert values from one type to another • • • • CBool() CDbl() CDec() CInt() McGraw Hill/Irwin CLng() CShort() CSng() CStr() ©2002 by The McGraw-Hill Companies, Inc All rights reserved 415 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 416 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 417 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 418 4.3 String Expressions 419 – 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 420 4.4 The Try/Catch Block Revisited • Used to detect and respond to error conditions • Can handle multiple error conditions McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 421 4.6 Logical Expressions 422 • Used to select an action from alternative actions • Results in a True or False answer to a test • Also known as Boolean expressions – Comparison Operators • • • • • • < >= = McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4.6 Logical Expressions (cont.) 423 – Comparison of Strings • Strings are ranked alphabetically – Logical Operators • Combine simple logical expressions to create more complex expressions • Common operators are Not, And, and Or McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4.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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 424 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 425 Chapter 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 426 Chapter Summary (cont.) 427 • 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved Chapter Summary (cont.) 428 • 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 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved ... reserved 4- 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. .. in expressions McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All rights reserved 4- 4. 1 Using Expressions, Operators, and Functions in Visual Basic NET Statements (cont.) 4- • Expressions. .. reserved 4. 1 Using Expressions, Operators, and 4- Functions in Visual Basic NET Statements (cont.) • Operators – Symbols for a common operations such as addition or multiplication – Most operators