Data Types and Expressions

Slides:



Advertisements
Similar presentations
Neal Stublen C# Data Types Built-in Types  Integer Types byte, sbyte, short, ushort, int, uint, long, ulong  Floating Point Types.
Advertisements

Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
Data Types and Expressions
C# Programming: From Problem Analysis to Program Design1 3 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 2 nd Edition.
C# Programming: From Problem Analysis to Program Design1 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 3rd Edition.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Objectives You should be able to describe: Data Types
By: Md Rezaul Huda Reza UNIVERSITY of South Asia 2 Md Rezaul Huda Reza UNIVERSITY of South Asia 2  Brief.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Introduction to Programming in C# John Galletly. The Basics.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
CPS120: Introduction to Computer Science
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Microsoft Visual C#.NET: From Problem Analysis to Program Design1 Chapter 3 Data Types and Expressions Microsoft Visual C#.NET: From Problem Analysis to.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1.2 Primitive Data Types and Variables
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Data Types and Expressions
Chapter # 2 Part 2 Programs And data
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
BASIC ELEMENTS OF A COMPUTER PROGRAM
Variable Declarations
UNIT 1.
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Computing with C# and the .NET Framework
Identifiers - symbolic names
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Data Types and Expressions
Chapter 2: Introduction to C++
C# Programming: From Problem Analysis to Program Design
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
2.1 Parts of a C++ Program.
With Assignment Operator
Basics of ‘C’.
Data Types and Expressions
Chapter 2: Basic Elements of Java
Numbers.
Chapter 2 Variables.
C++ Data Types Data Type
Data Types and Expressions
Chapter # 2 Part 2 Programs And data
Chapter 2: Introduction to C++.
Primitive Types and Expressions
C++ Programming Basics
Unit 3: Variables in Java
Module 2 Variables, Data Types and Arithmetic
Chapter 2 Variables.
Data Types and Expressions
Variables and Constants
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

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

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

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

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

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 .00176 OR in standard decimal notation Default type is double Table 2-10 Values and sizes for floating-point types C# Programming: From Problem Analysis to Program Design

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

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 2-11 Value and size for decimal data type Examples decimal endowmentAmount = 33897698.26M; decimal deficit; C# Programming: From Problem Analysis to Program Design

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

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

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 = 0.0675; const int SPEED = 70; const char HIGHEST_GRADE = 'A'; 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; numberOfMinutes = 45; count = 0; minIntValue = -2147483648; C# Programming: From Problem Analysis to Program Design

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

Examples of Assignment Statements (continued) double accountBalance, weight; bool isFinished; accountBalance = 4783.68; 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

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

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 with @ 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 Table 2-12 Basic arithmetic operators C# Programming: From Problem Analysis to Program Design

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

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

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

Basic Arithmetic Operations (continued) int x = 100; Console.WriteLine(x++ + " " + ++x); // Displays 100 102 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

C# Programming: From Problem Analysis to Program Design