CSE 100 Data Types Declarations Displays.

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

Javascript Essentials How do I write it??  Start Homesite  Between the start and end BODY tags type: 
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
C Tokens Identifiers Keywords Constants Operators Special symbols.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Course Title: Object Oriented Programming with C++ instructor ADEEL ANJUM Chapter No: 03 Conditional statement 1 BY ADEEL ANJUM (MSc-cs, CCNA,WEB DEVELOPER)
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
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.
Data Types Declarations Expressions Data storage C++ Basics.
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
is a specific set of data values along with a set of operations on those values. Review * Data type.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
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
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Bill Tucker Austin Community College COSC 1315
Chapter 2: Basic Elements of C++
Chapter 2 Variables.
Expressions.
Chapter 7: Expressions and Assignment Statements
Computer Programming BCT 1113
Chapter 2: Introduction to C++
Data Types, Variables & Arithmetic
Expressions.
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Chapter 7: Expressions and Assignment Statements
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.
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Introduction to C++
C++ for Engineers and Scientists Second Edition
Chapter 2: Introduction to C++
Programming Methodology for Finance
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Lecture 3 Expressions Richard Gesick.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 Variables.
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
CS150 Introduction to Computer Science 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.
Review Data type is a specific set of data values along with a set of operations on those values. *
Chapter 2: Introduction to C++.
Data Types and Expressions
Chapter 2 Variables.
Variables in C Topics Naming Variables Declaring Variables
Data Types and Expressions
Programming Fundamental-1
Data Types and Expressions
Presentation transcript:

CSE 100 Data Types Declarations Displays

Data Types Integral Floating Address Structured

Data Types Integral char 256 possibilities; enclosed in single quotes int no decimal points no commas no special signs ($, ¥, ‰) * *

Data Types Int -- Signed vs. Unsigned short 65,536 numbers -32,768 to 32,767 long 4,294,967,296 numbers ± 2,147,483,648 enum ý * *

Data Types Floating point numbers: Floating point numbers: signed or unsigned with decimal point Types: differ in amount of storage space; varies with machine § float (single precision) § double § long double * * *

Data Types Address pointer ý reference ý Structured array ý struct union ý class y

Variables / a place to store information / contents of a location in memory / has an address in RAM / uses a certain number of bytes / size depends upon the data type *

Identifiers Examples: x num1 num2 name row c237 index tax_rate Not valid: 1num bye[a] tax-rate tax rate newPos newpos (beware of this!) * * *

Declaration Statement A declaration statement associates an identifier with a data object, a function, or a data type so that the programmer can refer to that item by name.

Declaration Statement Syntax: data-type variable name; Examples: int my_age; int count; float deposit; float amount; float totalpay; double balance; char answer2 ; *

Multiple Declarations Syntax: data-type var1, var2, var3; Examples: int my_age; int count; float deposit, amount, totalpay; double balance; char answer2 ; need , *

Constants Literal typed directly into the program as needed ex. y = 23.4 pi = 3.1416 can change can NOT change Symbolic (Named) similar to a variable, but cannot be changed after it is initialized ex. const int CLASS_SIZE = 87; const double PI = 3.1416; * *

Constants Symbolic (Named) similar to a variable, but cannot be changed after it is initialized Syntax type VAR int IMAXY char BLANK const const const = value; = 1000; = ‘ ‘; * * *

Sample Program #include<iostream.h> void main(void) { double wdth, height; const int LEN = 5; wdth = 10.0; height = 8.5; cout << “volume = “<< LEN * wdth * height; } declarations constant assignments * * *

Operators An operator is a symbol that causes the compiler to take an action. Categories: mathematical assignment etc. *

Arithmetic Operators addition + subtraction ¾ multiplication * division / modulus %

Arithmetic Operators Syntax operand operator operand Example 7 + 15 34 — 189 92 * 31 345 / 6.02 86 % 3 *

Modulus The modulus operator yields the remainder of integer division. 12/3 14/3 4 3 12 12 4 3 14 12 2 12%3 14%3 * *

Modulus The modulus operator yields the remainder of integer division. 18 % 4 is 2 13 % 4 is 1 17 % 3 is 2 35 % 47 is 35 24 % 6 is 0 24 % 4 is 0 4 % 18 is 4 0 % 7 is 0 12 % 2.5 error 6.0 % 6 error * * *

A Glimpse of Operator Overloading Operator overload Using the same symbol for more than one operation. type int / type int 9 / 5 operator performs int division type double / type double 9.0 / 5.0 operator performs double division *

Mixed-Mode Expressions Operator overload Same operator will behave differently depending upon the operands. Operands of the same type give results of that type. In mixed-mode, floating point takes precedence. * *

Integer Division int a, b; a = 8; b = 3; cout << “The result is “ << a / b << endl; The result is 2 8 / 3 is 2 and not 2.6667 The result must be an integer. The result is truncated to 2. *

Order of Operations 8 + 3 * 4 is ? P E M D A S from left to right Show associativity to clarify. ( 8 + 3 ) * 4 is 44 8 + ( 3 * 4 ) is 20 P E M D A S from left to right * *

Order of Operations Expression Value 10 / 2 * 3 10 % 3 - 4 / 2 5.0 * 2.0 / 4.0 * 2.0 5.0 * 2.0 / (4.0 * 2.0) 5.0 + 2.0 / (4.0 * 2.0) 15 -1 5.0 1.25 5.25 * * * * *

Evaluation Trees 10 / 2 * 3 10 % 3 - 4 / 2 5.0 * 2.0 / 4.0 * 2.0 5 * 2 / (4.0 * 2.0) i r * *

Evaluation Trees 5.0 + 2.0 / (4.0 * 2.0) 5.0 * 2.0 / 4.0 * 2.0 (5 + 2) / (4.0 * 2.0) r r i r r r

Beware! C++ requires the asterisk to indicate multiplication. valid invalid 5*(8+3) 5(8+3) (x-y)*(x+y) (x-y)(x+y)

Assignment Operator The assignment operator (=)causes the operand on the left to take on the value to the right side of the statement. This operator assigns from right to left. valid invalid x = 5 5 = x picard = 6.02 avg_grd = 87.5 *

Assignment Statement Syntax: variable = expression; Examples: quiz1score = 14; lname = “Newman”; fname = “Alfred E.”; balance = balance + deposit; Do NOT use the word equals here. Use is assigned to. *

Assignment Statement Examples: int myage = 33; int width = 10, length; double ex1 = 85, ex2 = 73, ex3 = 82; char ans, key = ‘Q’, ch; *

Storage Locations. 1. int deposit, balance;. 2. deposit = 234;. 3 Storage Locations 1. int deposit, balance; 2. deposit = 234; 3. balance = 1000; Deposit and balance are given memory addresses. 234 is put into the memory address for deposit. 1000 is put into the memory address for balance. *

Storage Locations A S U 14 9 8 7 6 5 q w e ; t Y z 6 0 3 deposit balance A S U 14 9 8 7 6 5 6 bytes of storage q w e ; t Y z 6 0 3 1 2 3 4 5 6 7 8 9 *

Storage Locations 2 3 4 1 0 0 0 9 8 7 6 5 q w e ; t Y z 6 0 3 deposit balance 2 3 4 1 0 0 0 9 8 7 6 5 6 bytes of storage q w e ; t Y z 6 0 3 1 2 3 4 5 6 7 8 9

Storage Locations. 1. int deposit, balance;. 2. deposit = 234;. 3 Storage Locations 1. int deposit, balance; 2. deposit = 234; 3. balance = 1000; 4. balance = balance + deposit; Add the contents of the deposit address to the contents of the balance address. Store the result at the balance address. *

Storage Locations 2 3 4 1 2 3 4 9 8 7 6 5 q w e ; t Y z deposit balance 2 3 4 1 2 3 4 9 8 7 6 5 6 bytes of storage q w e ; t Y z 1 2 3 4 5 6 7 8 9 6 0 3

“Computers in the future may weigh no more than 1.5 tons” Popular Mechanics, 1949 “I think there is a world market for maybe five computers.” Thomas Watson, chairman of IBM, 1943