Understanding Assignment Statements and Arithmetic Operations in C# Programming
This resource provides a comprehensive overview of assignment statements and arithmetic operations within C# programming. It details the syntax used to change variable values using the assignment operator, as well as various expression types, including compatible literals, mathematical equations, and method calls. The document also discusses arithmetic operations, prioritization using order of operations, and the impact of unary operations, providing examples to enhance understanding. Ideal for students and developers seeking to deepen their knowledge of C# programming constructs.
Understanding Assignment Statements and Arithmetic Operations in C# Programming
E N D
Presentation Transcript
2 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition C# Programming: From Problem Analysis to Program Design
Part II C# Programming: From Problem Analysis to Program Design
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
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
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
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
Examples of Assignment Statements (continued) Figure 2-7 Impact of assignment statement C# Programming: From Problem Analysis to Program Design
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
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
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
Concatenation Figure 2-9 String concatenation C# Programming: From Problem Analysis to Program Design
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
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
Basic Arithmetic Operations (continued) Figure 2-12 Results after statement is executed C# Programming: From Problem Analysis to Program Design
Compound Operations • Accumulation • += C# Programming: From Problem Analysis to Program Design
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
Order of Operations • Associatively of operators • Left • Right C# Programming: From Problem Analysis to Program Design
Order of Operations (continued) Figure 2-13 Order of execution of the operators C# Programming: From Problem Analysis to Program Design
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
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
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
Formatting Output C# Programming: From Problem Analysis to Program Design
Numeric Format Specifiers C# Programming: From Problem Analysis to Program Design
Custom Numeric Format Specifiers C# Programming: From Problem Analysis to Program Design
Custom Numeric Format Specifiers C# Programming: From Problem Analysis to Program Design
Programming Example – CarpetCalculator Figure 2-15 Problem specification sheet for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design
Data Needs for the CarpetCalculator C# Programming: From Problem Analysis to Program Design
Non-changing Definitions for the CarpetCalculator C# Programming: From Problem Analysis to Program Design
CarpetCalculator Example Figure 2-16 Prototype for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design
Algorithm for CarpetCalculator Example Figure 2-17 CarpetCalculator flowchart C# Programming: From Problem Analysis to Program Design
Algorithm for the CarpetCalculator Example(continued) Figure 2-18 Structured English for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design
CarpetCalculator Example (continued) Figure 2-19 Class diagram for the CarpetCalculator example C# Programming: From Problem Analysis to Program Design
/* 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
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
CarpetCalculator Example (continued) Figure 2-20 Output from the CarpetCalculator program C# Programming: From Problem Analysis to Program Design
Coding Standards • Naming Conventions • Identifiers • Spacing Conventions • Declaration Conventions C# Programming: From Problem Analysis to Program Design
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
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
Chapter Summary (continued) • Constants • Assignment statements • Order of operations • Formatting output C# Programming: From Problem Analysis to Program Design