Lecture 2 Data Types Richard Gesick.

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

Constants and Data Types Constants Data Types Reading for this class: L&L,
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CMT Programming Software Applications
Data types and variables
Chapter 2 Data Types, Declarations, and Displays
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
Basic Elements of C++ Chapter 2.
Chapter 2 Programming Building Blocks — Java Basics.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Input & Output: Console
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CPS120: Introduction to Computer Science
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
 Character set is a set of valid characters that a language can recognise.  A character represents any letter, digit or any other sign  Java uses the.
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++
Programming Languages Machine language Assembly language High-level languages The Java language.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Copyright Curt Hill Variables What are they? Why do we need them?
Chapter 2 Variables.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
1 CSC 1111 Introduction to Computing using C++ C++ Basics (Part 1)
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CHAPTER 4 GC 101 Identifiers and Data types. 2 IDENTIFIERS  identifier: A name given to a piece of data, method, etc.  Identifiers allow us to refer.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter # 2 Part 2 Programs And data
Lecture 2A Data Types Richard Gesick
Chapter 2 Variables.
Chapter Topics The Basics of a C++ Program Data Types
Identifiers - symbolic names
BASIC ELEMENTS OF A COMPUTER PROGRAM
Console Output, Variables, Literals, and Introduction to Type
Documentation Need to have documentation in all programs
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
OBJECT ORIENTED PROGRAMMING I LECTURE 3 GEORGE KOUTSOGIANNAKIS
Basic Elements of C++.
Multiple variables can be created in one declaration
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Escape Sequences What if we wanted to print the quote character?
IDENTIFIERS CSC 111.
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Chapter 2: Java Fundamentals
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter # 2 Part 2 Programs And data
elementary programming
Programming Building Blocks Java Basics
Chapter 2: Introduction to C++.
Java Basics Data Types in Java.
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Instructor: Alexander Stoytchev
Presentation transcript:

Lecture 2 Data Types Richard Gesick

Topics Character Strings Variables and Assignments Primitive Data Types

Review C# program structure Comments Identifiers White space Objects Classes Methods

Building Blocks - Comments Comments explain the program to yourself and others Block comments Can span several lines Begin with /* End with */ Compiler ignores all text between /* and */ Line comments Start with // Compiler ignores text from // to end of line

Identifiers - symbolic names Identifiers are used to name classes, variables, and methods Identifier Rules: Must start with a letter Can contain essentially any number of letters and digits, but no spaces Case sensitive!! Number1 and number1 are different! Cannot be keywords or reserved words

Character Strings Object in C#, defined by string class String literal is 0 or more characters enclosed with double quotes “The quick brown fox jumped.” “x” “” Can contain any valid character

Write and WriteLine Methods Console.WriteLine (“Whatever you are, be a good one.”); Output device Monitor Console – class Out - objects parameter Method name WriteLine method includes a “new line” character.

Using Write and WriteLine 3...2...1...Blast-off We are go for orbit

string Concatenation Operator (+) String literals cannot span lines Combines string literals with other data types for printing Example: string first = "Hello"; string second = "there"; string greeting = first + “ “ + second; Console.Out.WriteLine( greeting ); Output is: Hello there

The + Operator What it does depends on the order String concatenation addition

string concatenation Bond, James Bond 007 Bond, James Bond 0034 The sum of x and y is: 2339 The sum of x and y is: 62 The sum of 23 and 39 is 62

Escape Sequences To include a special character in a string, use an escape sequence

Escape sequences Almost every fairy tale begins with "Once upon a time" while many mysteries start with "It was a dark and stormy night" and then you hear from the people that owe you money "the check is in the mail"

Multiple variables can be created in one declaration A variable is a name for a location in memory used to hold a data value. A variable must be declared by specifying the variable's name and the type of information that it will hold Multiple variables can be created in one declaration data type variable name int total; int count, temp, result;

while a motorcycle has 2 tires using System; //more concatenation namespace moreConcat { class Program static void Main(string[] args) int carTires = 4; int motorCycleTires = 2; Console.WriteLine("A car has " + carTires + " tires"+ “\nwhile a motorcycle has " + motorCycleTires + " tires"); } Output: A car has 4 tires while a motorcycle has 2 tires

Conventions Names of variables should be meaningful and reflect the data they will store This makes the logic of the program clearer Don't skimp on characters, but avoid extremely long names Avoid names similar to C# keywords

Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; The expression on the right is evaluated and the result is stored in the variable on the left The value that was in total is overwritten You can only assign a value to a variable that is consistent with the variable's declared type

Assignment Operator Syntax: target = expression; expression: operators and operands that evaluate to a single value --value is then assigned to target --target must be a variable (or constant) --value must be compatible with target's data type

Examples: The next statement is illegal int numPlayers = 10; // numPlayers holds 10 numPlayers = 8; // numPlayers now holds 8 int legalAge = 18; int voterAge = legalAge; The next statement is illegal int height = weight * 2; // weight is not defined int weight = 20;

Declare a variable only once Once a variable is declared, its data type cannot be changed. These statements: double twoCents; double twoCents = .02; generate a compiler error

Once a variable is declared, its data type cannot be changed. These statements: double cashInHand; int cashInHand; generate a compiler error

Constants Value cannot change during program execution Syntax: const dataType constantIdentifier =assignedValue; Note: assigning a value when the constant is declared is optional. But a value must be assigned before the constant is used.

Constants Constants are useful for three important reasons First, they give meaning to otherwise unclear literal values For example, MAX_LOAD means more than the literal 250 Second, they facilitate program maintenance If a constant is used in multiple places, its value need only be updated in one place Third, they formally establish that a value should not change, avoiding inadvertent errors by other programmers

Conventions Use all capital letters for constants and separate words with an underscore: Example: const double TAX_RATE = .05; Declare constants at the top of the program so their values can easily be seen Declare as a constant any data that should not change during program execution

Data Types For all data, assign a name (identifier) and a data type Data type tells compiler: How much memory to allocate Format in which to store data Types of operations you will perform on data Compiler monitors use of data C# is a "strongly typed" language

Primitive Data Types 13 simple data types: 8 subsets of integers 2 subsets of floating point numbers Character Boolean Decimal data type Everything else is an object

Why so many types? Difference is in amount of memory reserved for each (and hence the size of the value stored float only has 7 significant digits Signed numbers have both positive and negative values Unsigned numbers are >= 0

Literals All numeric values without a decimal point are considered int All numeric values with decimal point are considered double int testGrade = 100; long cityPopulation = 425612340L; byte ageInYears = 19; float salesTax = .05F; double interestRate = 0.725; double avogadroNumber = +6.022E23;

char Data Type One Unicode character (16 bits - 2 bytes) Type Size Minimum Value Maximum Value in Bytes char 2 character character encoded as 0 encoded as FFFF Example declarations: char finalGrade = ‘A’; char newline, tab, doubleQuotes;

boolean Data Type Two values only: true false Used for decision making or as "flag" variables Example declarations: bool isEmpty; bool passed, failed = false;