Presentation is loading. Please wait.

Presentation is loading. Please wait.

Data Types and Expressions

Similar presentations


Presentation on theme: "Data Types and Expressions"— Presentation transcript:

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

2 Integral Data Types (Integers)
Primary difference How much storage is needed Whether a negative value can be stored Includes number of types byte & sbyte char int & uint long & ulong short & ushort C# Programming: From Problem Analysis to Program Design

3 Data Types Table 2-9 Values and sizes for integral types
C# Programming: From Problem Analysis to Program Design

4 Examples of Integral Variable Declarations
int studentCount; // number of students in the class int ageOfStudent = 20; // age - originally initialized to 20 int numberOfExams; // number of exams int coursesEnrolled; // number of courses enrolled C# Programming: From Problem Analysis to Program Design

5 Floating-Point Types May be in scientific notation with an exponent
n.ne±P 3.2e+5 is equivalent to 320,000 1.76e-3 is equivalent to OR in standard decimal notation Default type is double Table Values and sizes for floating-point types C# Programming: From Problem Analysis to Program Design

6 Examples of Floating-Point Declarations
double extraPerson = 3.50; // extraPerson originally set // to 3.50 double averageScore = 70.0; // averageScore originally set // to 70.0 double priceOfTicket; // cost of a movie ticket double gradePointAverage; // grade point average float totalAmount = 23.57f; // note the f must be placed after // the value for float types C# Programming: From Problem Analysis to Program Design

7 Decimal Types Monetary data items
As with the float, must attach the suffix ‘m’ or ‘M’ onto the end of a number to indicate decimal Float attach ‘f’ or ‘F’ Table Value and size for decimal data type Examples decimal endowmentAmount = M; decimal deficit; C# Programming: From Problem Analysis to Program Design

8 Boolean Variables Based on true/false, on/off logic
Boolean type in C# → bool Does not accept integer values such as 0, 1, or -1 bool undergraduateStudent; bool moreData = true; C# Programming: From Problem Analysis to Program Design

9 Strings Reference type Represents a string of Unicode characters
string studentName; string courseName = "Programming I"; string twoLines = "Line1\nLine2"; C# Programming: From Problem Analysis to Program Design

10 Making Data Constant Add the keyword const to a declaration
Value cannot be changed Standard naming convention Syntax const type identifier = expression; const double TAX_RATE = ; const int SPEED = 70; const char HIGHEST_GRADE = 'A'; C# Programming: From Problem Analysis to Program Design

11 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

12 Examples of Assignment Statements
int numberOfMinutes, count, minIntValue; numberOfMinutes = 45; count = 0; minIntValue = ; C# Programming: From Problem Analysis to Program Design

13 Examples of Assignment Statements
char firstInitial, yearInSchool, punctuation; enterKey, lastChar; firstInitial = 'B'; yearInSchool = '1'; punctuation = '; '; enterKey = '\n'; // newline escape character lastChar = '\u005A'; // Unicode character 'Z' C# Programming: From Problem Analysis to Program Design

14 Examples of Assignment Statements (continued)
double accountBalance, weight; bool isFinished; accountBalance = ; weight = 1.7E-3; //scientific notation may be used isFinished = false; //declared previously as a bool //Notice – no quotes used C# Programming: From Problem Analysis to Program Design

15 Examples of Assignment Statements (continued)
decimal amountOwed, deficitValue; amountOwed = m; // m or M must be suffixed to // decimal data types deficitValue = M; C# Programming: From Problem Analysis to Program Design

16 Examples of Assignment Statements (continued)
string aSaying, fileLocation; aSaying = "First day of the rest of your life!\n"; fileLocation "C:\CSharpProjects\Chapter2"; @ placed before a string literal signals that the characters inside the double quotation marks should be interpreted verbatim --- No need to use escape characters C# Programming: From Problem Analysis to Program Design

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

18 Arithmetic Operations
Simplest form of an assignment statement resultVariable = operand1 operator operand2; Readability Space before and after every operator Table Basic arithmetic operators C# Programming: From Problem Analysis to Program Design

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

20 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

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

22 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; Console.WriteLine(num++); // Displays 100 Console.WriteLine(num); // Display 101 Console.WriteLine(++num); // Displays 102 C# Programming: From Problem Analysis to Program Design

23 Basic Arithmetic Operations (continued)
Figure Declaration of value type variables C# Programming: From Problem Analysis to Program Design

24 Basic Arithmetic Operations (continued)
Figure Change in memory after count++; statement executed C# Programming: From Problem Analysis to Program Design

25 Basic Arithmetic Operations (continued)
int x = 100; Console.WriteLine(x++ + " " + ++x); // Displays C# Programming: From Problem Analysis to Program Design

26 Basic Arithmetic Operations (continued)
Figure Results after statement is executed C# Programming: From Problem Analysis to Program Design

27 C# Programming: From Problem Analysis to Program Design


Download ppt "Data Types and Expressions"

Similar presentations


Ads by Google