CS150 Introduction to Computer Science 1

Slides:



Advertisements
Similar presentations
Type Conversion. C provides two methods of changing the type of an expression: Type conversion (done implicitly) Cast expressions (done explicitly) Examples.
Advertisements

Chapter 3. Expressions and Interactivity Csc 125 Introduction to C++
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
CS1 Lesson 3 Expressions and Interactivity CS1 -- John Cole1.
Variables: Named Storage Locations Variables must be defined or declared before they can be used so that appropriate memory storage can be allocated for.
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
1 9/15/06CS150 Introduction to Computer Science 1 Combined Assignments, Relational Operators, and the If Statement.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
C++ Typecasting. Math Library Functions.. Operator / Operands A = x + y.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 3- 1.
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 2 Elementary Programming.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CPS120: Introduction to Computer Science Operations Lecture 9.
CSC 107 – Programming For Science. The Week’s Goal.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 3 Expressions and Interactivity.
Expressions and Interactivity. 3.1 The cin Object.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 2 Primitive Data Types and Operations.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Formatting Output.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 3-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Last Time…. Operators Arithmetic Operators Assignment Operators Increment/Decrement Operators Relational Operators Logical Operators Expression Statements.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter Expressions and Interactivity 3. The cin Object 3.1.
Chapter 4: Introduction To C++ (Part 3). The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Information.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Lecture 3 Expressions, Type Conversion, Math and String
Chapter 7: Expressions and Assignment Statements
Chapter 3. Expressions and Interactivity
Chapter 7: Expressions and Assignment Statements
Arithmetic operations & assignment statement
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
Conversions of the type of the value of an expression
Expressions and Interactivity
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
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.
CS150 Introduction to Computer Science 1
Data Types and Expressions
Arithmetic Operations
Operator King Saud University
Lecture 3 Expressions, Type Conversion, and string
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

CS150 Introduction to Computer Science 1 Type Casting Section 3.3-3.5 9/19/08 CS150 Introduction to Computer Science 1

Implicit Type Conversion (3.3) Mixing the data types of operands during mathematical operations What happens when we save a double as an int? What happens when an int is multiplied by a float? 9/19/08 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Data Type Ranks long double double float unsigned long long unsigned int int Highest Lowest 9/19/08 CS150 Introduction to Computer Science 1

Rules for Type Conversion When a value is converted to a higher data type, it is being promoted When a value is converted to a lower data type, it is being demoted Rule 1: char, short, and unsigned short are automatically promoted to int Rule 2: When an operator works with values of different types, the lower ranking value is promoted to the higher ranking Rule 3: When the value of an expression is assigned to a variable, it is converted to the data type of that variable 9/19/08 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Q.2 Practice Assume the following variable definitions int a = 5, b = 12; double x = 3.4; What are the values of the following expressions: b / x x * a 9/19/08 CS150 Introduction to Computer Science 1

Explicit Type Conversion (3.4) A type cast expression let’s you manually change the data type of a value The syntax for type casting is static_cast<DataType>(Value) Value is a variable or literal value DataType is the data type that you are converting Value into 9/19/08 CS150 Introduction to Computer Science 1

7.3 Example of Type Casting double number = 3.7; int val; val = static_cast<int>(number); What is saved into val? 9/19/08 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Uses of Type Casting Preventing integer division int books = 30, months = 7; double booksPerMonth; booksPerMonth = static_cast<double>(books) / months; What about this statement? booksPerMonth = static_cast<double>(books / months); Displaying a char from its ASCII value int number = 65; cout << static_cast<char>(number); 9/19/08 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Q.4 Practice What is the value of each of the variables while this expression is being executed? int total; double gradeCounter, average; total = 30; gradeCounter = 4; average = static_cast<double>(total) / gradeCounter; 9/19/08 CS150 Introduction to Computer Science 1

Overflow and Underflow (3.5) What happens when a variable is assigned a value that is too large or too small in range for that variable’s data type? short testVar = 32767; cout << testVar << endl; testVar = testVar + 1; testVar = testVar - 1; 32767 -32768 9/19/08 CS150 Introduction to Computer Science 1

Multiple Assignments (3.7) C++ allows statements such as: a = b = c = d = 45; Why do you think that is? What is the associativity of the assignment operator? 9/19/08 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Combined Assignments The same variable can be used on the left hand side of the assignment and on the right hand side notes = notes / 20; note = notes % 20; These are common in programming, so the two operators can be combined as follows: notes /= 20; note %= 20; 9/19/08 CS150 Introduction to Computer Science 1

Examples of Combined Assignments Operator Example Usage Equivalent To += x += 5; x = x + 5; -= y -= 2; y = y – 2; *= z *= 10; z = z * 10; /= a /= b; a = a / b; %= c %= 3; c = c % 3; 9/19/08 CS150 Introduction to Computer Science 1

Q.5 Combined Assignments Combined assignments can be combined with arithmetic operators y -= a * 2; a /= b + c; C %= d – 3; What is the long form of these statements? 9/19/08 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Q.6 What is the Output? int unus, duo, tres; unus = duo = tres = 5; unus += 4; duo *= 2; tres -= 4; unus /= 3; duo += tres; cout << unus << endl; cout << duo << endl; cout << tres << endl; 9/19/08 CS150 Introduction to Computer Science 1

CS150 Introduction to Computer Science 1 Q.1 Practice Write a C++ program that allows the user the ability to enter the number of nickels and pennies they have. You are then to print the number of dollars and change that corresponds to. The change should be in the form of nickels and pennies 9/19/08 CS150 Introduction to Computer Science 1