1 / 20

Data Types and Expressions

2. Data Types and Expressions. C# Programming: From Problem Analysis to Program Design 4th Edition. Compound Operations. Accumulation Variable on left side of equal symbol is used once the entire expression on right is evaluated. Table 2-13 Compound arithmetic operators.

dysis
Download Presentation

Data Types and Expressions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 2 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 4th Edition C# Programming: From Problem Analysis to Program Design

  2. Compound Operations • Accumulation • Variable on left side of equal symbol is used once the entire expression on right is evaluated Table 2-13 Compound arithmetic operators C# Programming: From Problem Analysis to Program Design

  3. Basic Arithmetic Operations (continued) • Order of operations • Order in which the calculations are performed • Example • answer = 100; • answer += 50 * 3 / 25 – 4; • 50 * 3 = 150 • 150 / 25 = 6 • 6 – 4 = 2 • 100 + 2 = 102 C# Programming: From Problem Analysis to Program Design

  4. Order of Operations Table 2-14 Operator precedence • Associatively of operators • Left • Right C# Programming: From Problem Analysis to Program Design

  5. Order of Operations (continued) Figure 2-13 Order of execution of the operators C# Programming: From Problem Analysis to Program Design

  6. Mixed Expressions • Implicit type coercion • Changes int data type into a double • No implicit conversion from double to int • double answer; • answer = 10 / 3; // Does not produce 3.3333333 • int value1 = 440, • anotherNumber = 70; • double value2 = 100.60; • value2 = value1; // ok here – 440.0 stored in value2 C# Programming: From Problem Analysis to Program Design

  7. Mixed Expressions • int value1 = 440; • double value2 = 100.60; • value1 = value2; // syntax error as shown in Figure 2-14 Figure 2-14 Syntax error generated for assigning a double to an int C# Programming: From Problem Analysis to Program Design

  8. Mixed Expressions (continued) • Explicit type coercion • Cast • (type) expression • examAverage = (exam1 + exam2 + exam3) / (double) count; int value1 = 0, anotherNumber = 75; double value2 = 100.99, anotherDouble = 100; value1 = (int) value2; // value1 = 100 value2 = (double) anotherNumber; // value2 = 75.0 C# Programming: From Problem Analysis to Program Design

  9. Formatting Output • You can format data by adding dollar signs, percent symbols, and/or commas to separate digits • You can suppress leading zeros • You can pad a value with special characters • Place characters to the left or right of the significant digits • Use format specifiers C# Programming: From Problem Analysis to Program Design

  10. Formatting Output (continued) Table 2-15 Examples using format specifiers C# Programming: From Problem Analysis to Program Design

  11. Numeric Format Specifiers Table 2-16 Standard numeric format specifiers C# Programming: From Problem Analysis to Program Design

  12. Numeric Format Specifiers(continued) Table 2-16 Standard numeric format specifiers (continued) C# Programming: From Problem Analysis to Program Design

  13. Custom Numeric Format Specifiers Table 2-17 Custom numeric format specifiers C# Programming: From Problem Analysis to Program Design

  14. Custom Numeric Format Specifiers (continued) Table 2-17 Custom numeric format specifiers (continued) C# Programming: From Problem Analysis to Program Design

  15. Width Specifier • Useful when you want to control the alignment of items on multiple lines • Alignment component goes after the index ordinal followed by a comma (before the colon) • If alignment number is less than actual size, it is ignored • If alignment number is greater, pads with white space • Negative alignment component places spaces on right Console.WriteLine("{0,10:F0}{1,8:C}", 9, 14); 9 $14.00 //Right justifies values C# Programming: From Problem Analysis to Program Design

  16. Coding Standards • Naming conventions • Identifiers • Spacing conventions • Declaration conventions C# Programming: From Problem Analysis to Program Design

  17. Resources Naming Guidelines for .NET – http://msdn.microsoft.com/en-us/library/xzf533w0(VS.71).aspx Writing Readable Code – http://software.ac.uk/resources/guides/writing-readable-source-code#node-131 C# Video tutorials – http://www.programmingvideotutorials.com/csharp/csharp-introductionVisual Studio 2012 – C# – http://msdn.microsoft.com/en-us/library/kx37x362(V=VS.110).aspx C# Programming: From Problem Analysis to Program Design

  18. Chapter Summary • Memory representation of data • Bits versus bytes • Number system • Binary number system • Character sets • Unicode C# Programming: From Problem Analysis to Program Design

  19. Chapter Summary (continued) • Memory locations for data • Relationship between classes, objects, and types • Predefined data types • Integral data types • Floating-point types • Decimaltype • Boolean variables • Strings C# Programming: From Problem Analysis to Program Design

  20. Chapter Summary (continued) • Constants • Assignment statements • Order of operations • Formatting output C# Programming: From Problem Analysis to Program Design

More Related