Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions.

Similar presentations


Presentation on theme: "CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions."— Presentation transcript:

1 CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions

2 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 2 Expressions Arithmetic expressions –To perform arithmetic operations. String expressions –To manipulate string data. Logical expressions –To select an appropriate action.

3 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 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. lblGreeting.Text = “Hello “ & InputBox(“Enter Name:”) value operator function

4 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 4 4.1 Using Expressions, Operators, and Functions in VB.NET Statements (cont.) Statements –Perform various tasks Evaluate expressions one at a time. Carry out tasks specified in expressions. 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.

5 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 5 4.1 Using Expressions, Operators, and Functions in VB.NET Statements (cont.) Operators –Symbols for a common operations such as addition or multiplication. –Most operators are binary operators. 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.

6 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 6 4.1 Using Expressions, Operators, and Functions in VB.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)) 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)

7 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 7 4.2 Arithmetic Expressions Arithmetic operators and functions must be written according to Visual Basic.NET syntax. 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

8 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 8 4.2 Arithmetic Expressions (cont.) Integer Division –Calculates the number of times one integer goes into another, discarding the reminder. –Mod calculates the remainder when one integer is divided by another.

9 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 9 4.2 Arithmetic Expressions (cont.) Arithmetic Functions –Math.Sqrt() –Math.Abs() –Math.Sign() –Fix() –Math.Exp() –Math.Log() –Rnd() Type Conversion Functions –CBool() –CLng() –CDbl() –CShort() –CDec() –CSng() –CInt() –CStr()

10 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 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. –Problems Caused by Limited Significant Digits Limitations on the number of significant digits. Digits may be loss at run time.

11 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 11 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.

12 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 12 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.

13 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 13 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.

14 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 14 4.4 The Try/Catch Block Revisited Used to detect and respond to error conditions. Can handle multiple error conditions.

15 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 15 4.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. –Comparison Operators < <= > >= = <>

16 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 16 4.6 Logical Expressions (cont.) –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. –Logical Functions Common ones are IsNumeric() and IIf(). IsNumeric() is True if the argument is a valid number. IIf() chooses between two alternate paths.

17 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 17 Quick Review What are the three type of expressions in VB? What four things can be contained within an expression? What are the symbols/keyword for exponentiation, multiplication, division, modulus arithmetic operators? What order would the operations in the following expression be evaluated? –A ^ B + (D / C) ^ E

18 McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc. All rights reserved. 4- 18 Quick Review (cont.) What are three types of arithmetic errors that may occur? What functions translate a character into it’s corresponding ACSII value and vice versa? What is the symbol for the concatenation string operator? What are the symbols for the equality, inequality, and less than or equal to logical operators? What two values do all logical expressions evaluate to?


Download ppt "CHAPTER FOUR Performing Calculations and Manipulating Data: Expressions."

Similar presentations


Ads by Google