Data Types and Expressions

Slides:



Advertisements
Similar presentations
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Advertisements

Types and Variables. Computer Programming 2 C++ in one page!
C# Programming: From Problem Analysis to Program Design1 3 Data Types and Expressions C# Programming: From Problem Analysis to Program Design 2 nd Edition.
CS1061 C Programming Lecture 4: Indentifiers and Integers A.O’Riordan, 2004.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
JavaScript, Third Edition
String Escape Sequences
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.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
C Tokens Identifiers Keywords Constants Operators Special symbols.
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
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
BASICS CONCEPTS OF ‘C’.  C Character Set C Character Set  Tokens in C Tokens in C  Constants Constants  Variables Variables  Global Variables Global.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Java Programming, Second Edition Chapter Two Using Data Within a Program.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
1.2 Primitive Data Types and Variables
CPS120: Introduction to Computer Science Variables and Constants.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Variables and Expressions Programming Right from the Start with Visual Basic.NET 1/e 7.
Objects Variables and Constants. Our Scuba Problem #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
Types Chapter 2. C++ An Introduction to Computing, 3rd ed. 2 Objectives Observe types provided by C++ Literals of these types Explain syntax rules for.
 Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory. 
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.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 Objects Types, Variables, and Constants Chapter 3.
Introduction C# program is collection of classes Classes are collection of methods and some statements That statements contains tokens C# includes five.
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.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Chapter 2 Variables and Constants. Objectives Explain the different integer variable types used in C++. Declare, name, and initialize variables. Use character.
Fundamentals 2.
Chapter 2 Variables.
Java Variables and Types
Documentation Need to have documentation in all programs
ITEC113 Algorithms and Programming Techniques
Data Types, Identifiers, and Expressions
C# Programming: From Problem Analysis to Program Design
IDENTIFIERS CSC 111.
Data Types and Expressions
Chapter 2 Variables.
C++ Data Types Data Type
Chapter 2: Java Fundamentals
Data Types and Expressions
Data Types and Expressions
Chapter 2: Java Fundamentals
The Data Element.
Chap 2. Identifiers, Keywords, and Types
The Data Element.
Chapter 2 Variables.
C Programming Lecture-3 Keywords, Datatypes, Constants & Variables
Data Types and Expressions
Variables and Constants
Presentation transcript:

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

Chapter Objectives Examine how computers represent data Declare memory locations for data Explore the relationship between classes, objects, and types Use predefined data types Use integral data types C# Programming: From Problem Analysis to Program Design

Chapter Objectives (continued) Use floating-point types Learn about the decimal data type Declare Boolean variables Declare and manipulate strings Work with constants C# Programming: From Problem Analysis to Program Design

Chapter Objectives (continued) Write assignment statements using arithmetic operators Learn about the order of operations Learn special formatting rules for currency Work through a programming example that illustrates the chapter’s concepts C# Programming: From Problem Analysis to Program Design

Data Representation Bits Bytes Bit – "Binary digIT" Binary digit can hold 0 or 1 1 and 0 correspond to on and off, respectively Bytes Combination of 8 bits Represent one character, such as the letter A To represent data, computers use the base-2 number system, or binary number system C# Programming: From Problem Analysis to Program Design

Binary Number System Figure 2-1 Base-10 positional notation of 1326 C# Programming: From Problem Analysis to Program Design

Binary Number System (continued) Figure 2-2 Decimal equivalent of 01101001 C# Programming: From Problem Analysis to Program Design

Data Representation (continued) C# Programming: From Problem Analysis to Program Design

Data Representation (continued) Character sets With only 8 bits, can represent 28, or 256, different decimal values ranging from 0 to 255; this is 256 different characters Unicode – character set used by C# (pronounced C Sharp) Uses 16 bits to represent characters 216, or 65,536 unique characters, can be represented American Standard Code for Information Interchange (ASCII) – subset of Unicode First 128 characters are the same C# Programming: From Problem Analysis to Program Design

Data Representation (continued) C# Programming: From Problem Analysis to Program Design

Memory Locations for Data Identifier Name Rules for creating an identifier Combination of alphabetic characters (a-z and A-Z), numeric digits (0-9), and the underscore First character in the name may not be numeric No embedded spaces – concatenate (append) words together Keywords cannot be used Use the case of the character to your advantage Be descriptive with meaningful names C# Programming: From Problem Analysis to Program Design

Reserved Words in C# C# Programming: From Problem Analysis to Program Design

Reserved Words in C# (continued) Contextual keywords As powerful as regular keywords Contextual keywords have special meaning only when used in a specific context; other times they can be used as identifiers C# Programming: From Problem Analysis to Program Design

Naming Conventions Pascal case Camel case First letter of each word capitalized Class, method, namespace, and properties identifiers Camel case Hungarian notation First letter of identifier lowercase; first letter of subsequent concatenated words capitalized Variables and objects C# Programming: From Problem Analysis to Program Design

Naming Conventions (continued) Uppercase Every character is uppercase Constant literals and for identifiers that consist of two or fewer letters C# Programming: From Problem Analysis to Program Design

Examples of Valid Names (Identifiers) C# Programming: From Problem Analysis to Program Design

Examples of Invalid Names (Identifiers) C# Programming: From Problem Analysis to Program Design

Variables Area in computer memory where a value of a particular data type can be stored Declare a variable Allocate memory Syntax type identifier; Compile-time initialization Initialize a variable when it is declared type identifier = expression; C# Programming: From Problem Analysis to Program Design

Types, Classes, and Objects C# has more than one type of number int type is a whole number Floating-point types can have a fractional portion Types are actually implemented through classes One-to-one correspondence between a class and a type Simple data type such as int, implemented as a class C# Programming: From Problem Analysis to Program Design

Types, Classes, and Objects (continued) Instance of a class → object A class includes more than just data Encapsulation → packaging of data and behaviors into a single or unit→class C# Programming: From Problem Analysis to Program Design

Type, Class, and Object Examples C# Programming: From Problem Analysis to Program Design

Predefined Data Types Common Type System (CTS) Divided into two major categories Figure 2-3 .NET common types C# Programming: From Problem Analysis to Program Design

Value and Reference Types Figure 2-4 Memory representation for value and reference types C# Programming: From Problem Analysis to Program Design

Value Types Fundamental or primitive data types Figure 2-5 Value type hierarchy C# Programming: From Problem Analysis to Program Design

Value Types (continued) C# Programming: From Problem Analysis to Program Design

Integral Data Types Includes number of types 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 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 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’ 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