1 / 39

Data Types and Expressions

2. Data Types and Expressions. C# Programming: From Problem Analysis to Program Design 3rd Edition. Part II. Assignment Statements. Used to change the value of the variable Assignment operator (=) Syntax variable = expression; Expression can be: Another variable

ketan
Télécharger la présentation

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 3rd Edition C# Programming: From Problem Analysis to Program Design

  2. Part II C# Programming: From Problem Analysis to Program Design

  3. Assignment Statements • Used to change the value of the variable • Assignment operator (=) • Syntax • variable = expression; • Expression can be: • Another variable • Compatible literal value • Mathematical equation • Call to a method that returns a compatible value • Combination of one or more items in this list C# Programming: From Problem Analysis to Program Design

  4. Examples of Assignment Statements int numberOfMinutes, count, minIntValue; char firstInitial, yearInSchool, punctuation; numberOfMinutes = 45; count = 0; minIntValue = -2147483648; firstInitial = ‘B’; yearInSchool = ‘1’; enterKey = ‘\n’; // newline escape character C# Programming: From Problem Analysis to Program Design

  5. Examples of Assignment Statements (continued) double accountBalance, weight; decimal amountOwed, deficitValue; bool isFinished; accountBalance = 4783.68; weight = 1.7E-3; //scientific notation may be used amountOwed = 3000.50m; // m or M must be suffixed to // decimal deficitValue = -322888672.50M; C# Programming: From Problem Analysis to Program Design

  6. Examples of Assignment Statements (continued) int count = 0, newValue = 25; string aSaying, fileLocation; aSaying = “First day of the rest of your life!\n "; fileLocation = @”C:\CSharpProjects\Chapter2”; isFinished = false; // declared previously as a bool count = newValue; @ placed before a string literal signals that the characters inside the double quotation marks should be interpreted verbatim C# Programming: From Problem Analysis to Program Design

  7. Examples of Assignment Statements (continued) Figure 2-7 Impact of assignment statement C# Programming: From Problem Analysis to Program Design

  8. Arithmetic Operations • Simplest form of an assignment statement • resultVariable = operand1 operator operand2; • Readability • Space before and after every operator C# Programming: From Problem Analysis to Program Design

  9. Basic Arithmetic Operations • Modulus operator with negative values • Sign of the dividend determines the result • -3 % 5 = -3; 5 % -3 = 2; -5 % -3 = -3; Figure 2-8 Result of 67 % 3 C# Programming: From Problem Analysis to Program Design

  10. Basic Arithmetic Operations (continued) • Plus (+) with string Identifiers • Concatenates operand2 onto end of operand1 string result; string fullName; string firstName = “Rochelle”; string lastName = “Howard”; fullName = firstName + “ “ + lastName; C# Programming: From Problem Analysis to Program Design

  11. Concatenation Figure 2-9 String concatenation C# Programming: From Problem Analysis to Program Design

  12. Basic Arithmetic Operations (continued) • Increment and Decrement Operations • Unary operator • num++; // num = num + 1; • --value1; // value = value – 1; • Preincrement/predecrement versus post int num = 100; System.Console.WriteLine(num++); // Displays 100 System.Console.WriteLine(num); // Display 101 System.Console.WriteLine(++num); // Displays 102 C# Programming: From Problem Analysis to Program Design

  13. Basic Arithmetic Operations (continued) • int num = 100; • System.Console.WriteLine(x++ + “ “ + ++x); // Displays 100 102 Figure 2-11 Change in memory after count++; statement executed C# Programming: From Problem Analysis to Program Design

  14. Basic Arithmetic Operations (continued) Figure 2-12 Results after statement is executed C# Programming: From Problem Analysis to Program Design

  15. Compound Operations • Accumulation • += C# Programming: From Problem Analysis to Program Design

  16. 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

  17. Order of Operations • Associatively of operators • Left • Right C# Programming: From Problem Analysis to Program Design

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

  19. Mixed Expressions • Implicit type coercion • Changes int data type into a double • No implicit conversion from double to int Figure 2-14 Syntax error generated for assigning a double to an int C# Programming: From Problem Analysis to Program Design

  20. 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

  21. 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

  22. Formatting Output C# Programming: From Problem Analysis to Program Design

  23. Numeric Format Specifiers C# Programming: From Problem Analysis to Program Design

  24. Custom Numeric Format Specifiers C# Programming: From Problem Analysis to Program Design

  25. Custom Numeric Format Specifiers C# Programming: From Problem Analysis to Program Design

  26. Programming Example – CarpetCalculator Figure 2-15 Problem specification sheet for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design

  27. Data Needs for the CarpetCalculator C# Programming: From Problem Analysis to Program Design

  28. Non-changing Definitions for the CarpetCalculator C# Programming: From Problem Analysis to Program Design

  29. CarpetCalculator Example Figure 2-16 Prototype for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design

  30. Algorithm for CarpetCalculator Example Figure 2-17 CarpetCalculator flowchart C# Programming: From Problem Analysis to Program Design

  31. Algorithm for the CarpetCalculator Example(continued) Figure 2-18 Structured English for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design

  32. CarpetCalculator Example (continued) Figure 2-19 Class diagram for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design

  33. /* CarpetCalculator.cs Author: Doyle */ using System; namespace CarpetExample { class CarpetCalculator { staticvoid Main( ) { constint SQ_FT_PER_SQ_YARD = 9; constint INCHES_PER_FOOT = 12; conststring BEST_CARPET = "Berber"; conststring ECONOMY_CARPET = "Pile"; int roomLengthFeet = 12, roomLengthInches = 2, roomWidthFeet = 14, roomWidthInches = 7; doubleroomLength, roomWidth, carpetPrice, numOfSquareFeet, numOfSquareYards, totalCost; C# Programming: From Problem Analysis to Program Design

  34. roomLength = roomLengthFeet + (double) roomLengthInches / INCHES_PER_FOOT; roomWidth = roomWidthFeet + (double) roomWidthInches / INCHES_PER_FOOT; numOfSquareFeet = roomLength * roomWidth; numOfSquareYards = numOfSquareFeet / SQ_FT_PER_SQ_YARD; carpetPrice = 27.95; totalCost = numOfSquareYards * carpetPrice; Console.Out.WriteLine("The cost of " + BEST_CARPET + " is {0:C}", totalCost); Console.Out.WriteLine( ); carpetPrice = 15.95; totalCost = numOfSquareYards * carpetPrice; Console.Out.WriteLine("The cost of " + ECONOMY_CARPET + " is " + "{0:C}", totalCost); Console.Read(); } } } C# Programming: From Problem Analysis to Program Design

  35. CarpetCalculator Example (continued) Figure 2-20 Output from the CarpetCalculator program C# Programming: From Problem Analysis to Program Design

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

  37. 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

  38. 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

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

More Related