Chapter 2: Using Data.

Slides:



Advertisements
Similar presentations
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Advertisements

Chapter 2: Using Data.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Basic Elements of C++
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Basic Elements of C++ Chapter 2.
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
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 2: Basic Elements of C++
C++ Programming: Basic Elements of C++.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Basic Elements of C++
Java Programming, Second Edition Chapter Two Using Data Within a Program.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
1.2 Primitive Data Types and Variables
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 2: Basic Elements of C++. Introduction Computer program – Sequence of statements whose objective is to accomplish a task Programming – Process.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 2: Basic Elements of C++
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
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.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
Computing with C# and the .NET Framework
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 2: Using Data

Declaring Variables Constant Variable Cannot be changed after a program is compiled Variable A named location in computer memory that can hold different values at different points in time Microsoft Visual C# 2012, Fifth Edition

Microsoft Visual C# 2012, Fifth Edition

Declaring Variables (cont’d.) Variable declaration A statement that names a variable and reserves storage Example: int myAge = 25; You can declare multiple variables of the same type In separate statements on different lines You can declare two variables of the same type in a single statement By using the type once and separating the variable declarations with a comma Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Format string A string of characters that optionally contains fixed text Contains one or more format items or placeholders for variable values Placeholder Consists of a pair of curly braces containing a number that indicates the desired variable’s position in a list that follows the string Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Formatting output Console.WriteLine("{0, 5}", 4); Console.WriteLine("{0, 5}", 56); Console.WriteLine("{0, 5}", 789); Console.WriteLine("{0, -8}{1, -8}", "Richard", "Lee"); Console.WriteLine("{0, -8}{1, -8}", "Marcia", "Parker"); Console.WriteLine("{0, -8}{1, -8}", "Ed", "Tompkins"); Concatenate strings using the plus (+) sign Console.WriteLine("Concatenated " + "string"); Microsoft Visual C# 2012, Fifth Edition

Displaying Variable Values (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Using the Integral Data Types Types that store whole numbers byte, sbyte, short, ushort, int, uint, long, ulong, and char Variables of type int Store (or hold) integers, or whole numbers Shorter integer types byte, sbyte (which stands for signed byte), short (short int), or ushort (unsigned short int) Microsoft Visual C# 2012, Fifth Edition

Using Floating-Point Data Types Floating-point number Contains decimal positions Floating-point data types float Can hold up to 7 significant digits of accuracy double Can hold 15 or 16 significant digits of accuracy decimal Can hold 29 significant digits but a smaller range Suitable for financial and monetary calculations Microsoft Visual C# 2012, Fifth Edition

Using Arithmetic Operators Binary operators Use two values (operands) Operator precedence Rules that determine the order in which parts of a mathematical expression are evaluated Multiplication, division, and remainder (modulus) always take place prior to addition or subtraction in an expression You can override normal operator precedence with parentheses Expressions are evaluated left to right Microsoft Visual C# 2012, Fifth Edition

Using Arithmetic Operators (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Using Shortcut Arithmetic Operators Add and assign operator Example: bankBal += bankBal * interestRate; Variations: –=, *=, /=, and %= Prefix increment operator Example: ++someValue; Postfix increment operator Example: someValue++; Microsoft Visual C# 2012, Fifth Edition

Using Shortcut Arithmetic Operators (cont’d.) Unary operator Uses only one value (e.g., –123) Decrement operator (--) Can be prefix or postfix Microsoft Visual C# 2012, Fifth Edition 19

Using the bool Data Type Boolean variable Can hold only one of two values—true or false Declare a Boolean variable with type bool Comparison operator Compares two items An expression containing a comparison operator returns a Boolean value Microsoft Visual C# 2012, Fifth Edition

Using the bool Data Type (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Using the bool Data Type (cont’d.) // Example using a bool data type and a comparison // operator bool employeeWorkedOvertime = hoursWorked > 40; if (employeeWorkedOvertime) { // Code to calculate overtime goes here } Microsoft Visual C# 2012, Fifth Edition

Understanding Numeric Type Conversion Implicit cast Automatic transformation that occurs when a value is assigned to a type with higher precedence Example: aDouble = anInt; Explicit cast Placing the desired result type in parentheses followed by the variable or constant to be cast Example: anInt = (int)aDouble; Microsoft Visual C# 2012, Fifth Edition

Using the char Data Type Holds any single Unicode character Place constant character values within single quotation marks (e.g., 'A') Escape sequence Stores a pair of characters Begins with a backslash A pair of symbols represents a single character Microsoft Visual C# 2012, Fifth Edition

Using the char Data Type (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Using the string Data Type Holds a series of characters Values are expressed within double quotation marks Comparing strings Use == and != Use the Equals() method, Compare() method, and CompareTo() method Microsoft Visual C# 2012, Fifth Edition

Using the string Data Type (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Microsoft Visual C# 2012, Fifth Edition

Using the string Data Type (cont’d.) Use the Length property of a string to determine its length Example: string aString = "How long is this string?"; Console.WriteLine("{0} It is {1} characters long.", aString, aString.Length); Microsoft Visual C# 2012, Fifth Edition

Using the string Data Type (cont’d.) Use the Substring() method to extract a portion of a string from a starting point for a specific length Microsoft Visual C# 2012, Fifth Edition

Defining Named Constants Often simply called a constant An identifier whose contents cannot change Created using the keyword const Programmers usually name constants using all uppercase letters, inserting underscores for readability Self-documenting statement Easy to understand even without program comments Microsoft Visual C# 2012, Fifth Edition

Accepting Console Input Interactive program A program that allows user input Console.ReadLine() method Accepts user input from the keyboard Accepts all of the characters entered by a user until the user presses Enter Characters can be assigned to a string Must use a conversion method to convert the input string to the proper type Microsoft Visual C# 2012, Fifth Edition

Accepting Console Input (cont’d.) Microsoft Visual C# 2012, Fifth Edition

Microsoft Visual C# 2012, Fifth Edition

You Do It Activities to explore: Declaring and Using Variables Performing Arithmetic Working with Boolean Variables Using Escape Sequences Writing a Program that Accepts User Input Microsoft Visual C# 2012, Fifth Edition