Presentation is loading. Please wait.

Presentation is loading. Please wait.

C# Programming: From Problem Analysis to Program Design1 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition.

Similar presentations


Presentation on theme: "C# Programming: From Problem Analysis to Program Design1 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition."— Presentation transcript:

1 C# Programming: From Problem Analysis to Program Design1 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition 2

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

3 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

4 C# Programming: From Problem Analysis to Program Design4 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

5 C# Programming: From Problem Analysis to Program Design5 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;

6 C# Programming: From Problem Analysis to Program Design6 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

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

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

9 C# Programming: From Problem Analysis to Program Design9 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

10 C# Programming: From Problem Analysis to Program Design10 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;

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

12 C# Programming: From Problem Analysis to Program Design12 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

13 C# Programming: From Problem Analysis to Program Design13 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

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

15 C# Programming: From Problem Analysis to Program Design15 Compound Operations Accumulation –+=

16 C# Programming: From Problem Analysis to Program Design16 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

17 C# Programming: From Problem Analysis to Program Design17 Order of Operations Associatively of operators –Left –Right

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

19 C# Programming: From Problem Analysis to Program Design19 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

20 C# Programming: From Problem Analysis to Program Design20 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

21 C# Programming: From Problem Analysis to Program Design21 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

22 C# Programming: From Problem Analysis to Program Design22 Formatting Output ( continued )

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

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

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

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

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

28 C# Programming: From Problem Analysis to Program Design28 Nonchanging Definitions for the CarpetCalculator

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

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

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

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

33 C# Programming: From Problem Analysis to Program Design33 /* CarpetCalculator.csAuthor:Doyle */ using System; namespace CarpetExample { class CarpetCalculator { static void Main( ) { const int SQ_FT_PER_SQ_YARD = 9; const int INCHES_PER_FOOT = 12; const string BEST_CARPET = "Berber"; const string ECONOMY_CARPET = "Pile"; int roomLengthFeet = 12, roomLengthInches = 2, roomWidthFeet = 14, roomWidthInches = 7; double roomLength, roomWidth, carpetPrice, numOfSquareFeet, numOfSquareYards, totalCost;

34 C# Programming: From Problem Analysis to Program Design34 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(); } } }

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

36 Coding Standards Naming conventions –Identifiers Spacing conventions Declaration conventions C# Programming: From Problem Analysis to Program Design36

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 Design37

38 C# Programming: From Problem Analysis to Program Design38 Chapter Summary ( continued ) Memory locations for data Relationship between classes, objects, and types Predefined data types –Integral data types –Floating-point types –Decimal type –Boolean variables –Strings

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


Download ppt "C# Programming: From Problem Analysis to Program Design1 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition."

Similar presentations


Ads by Google