Type Conversions Implicit Conversion Explicit Conversion.

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

Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
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.
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.
Input/Output  Input/Output operations are performed using input/output functions  Common input/output functions are provided as part of C’s standard.
Variables, Constants, Methods, and Calculations Chapter 3 - Review.
Expressions, Data Conversion, and Input
1. 2 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Decisions { class.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Munster Programming Training
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
1 © 2000 John Urrutia. All rights reserved. Qbasic Constructing Qbasic Programs.
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
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.
Input, Output, and Processing
1.3 Console Input And Output academy.zariba.com 1.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Microsoft Visual Basic 2005 CHAPTER 4 Variables and Arithmetic Operations.
Number System. Number Systems Important Number systems – Decimal – Binary – Hexadecimal.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations.
Variables When programming it is often necessary to store a value for use later on in the program. A variable is a label given to a location in memory.
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Programming with Visual C++: Concepts and Projects Chapter 3A: Integral Data (Concepts)
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Data Types and Conversions, Input from the Keyboard CS303E: Elements of Computers and Programming.
CMPSC 121- Spring 2015 Lecture 6 January 23, 2015.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Data Types and Conversions, Input from the Keyboard If you can't write it down in English, you can't code it. -- Peter Halpern If you lie to the computer,
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
Department of Electronic & Electrical Engineering IO reading and writing variables scanf printf format strings "%d %c %f"
Microsoft Visual Basic 2012 CHAPTER FOUR Variables and Arithmetic Operations.
Week 7 : String and photo processing. Today’s Tasks  Practice list and string  Convert Decimal to any radix base number  Between Binary and Hexadecimal.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Chapter 3: Introducing the Microsoft.NET Framework and Visual Basic.NET Visual Basic.NET Programming: From Problem Analysis to Program Design.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
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.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Chapter Topics The Basics of a C++ Program Data Types
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Using the Console.
© 2016, Mike Murach & Associates, Inc.
Data Types and Conversions, Input from the Keyboard
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Computing with C# and the .NET Framework
Basic Elements of C++ Chapter 2.
Input/Output Input/Output operations are performed using input/output functions Common input/output functions are provided as part of C’s standard input/output.
Chapter 2.
Conversions of the type of the value of an expression
Python Lessons 9 & 10 Mr. Kalmes.
VB.Net Programming Console Application
Java Basics Data Types in Java.
Exercise Write 100 as a standard numeral. 1.
Data Types and Maths Programming Guides.
CS 1111 Introduction to Programming Spring 2019
Python Creating a calculator.
Data Types and Arithmetic in C
Presentation transcript:

Type Conversions Implicit Conversion Explicit Conversion

Intro Value must be compatible with the variable. If not, we have to make a type conversion. Some data types can be converted to an other and some can not. We can lose data during unsafe conversion e.g. assigning a floating point number to an integer. In that case we lose fraction.

Implicit Conversions Numeric

Properties Occurs between compatible types. Transfers data from a smaller type to a bigger type. No data loss. No extra operation. Automatic type conversion.

Compatible types

Example Implicit Conversion No data loss

Explicit Conversion Numeric Character Character Numeric

Properties Occurs between incompatible types. May lose data. Extra coding needed. Use with caution.

Example

Exercise Assign a value to integer variable “source” that can not fit in to short type. Comment on the result.

Numeric Character.ToString() method. Can be used with numeric types. Converts numeric value to “ string ” type. There is also a formatting option.

Example Output : 123s

Formatting example “ NumberFormatInfo ” class. Used especially with floating point numbers. Resides under System.Globalization namespace. Formatting properties Number decimal digits Number decimal separator Number group separator

Formatting example Output: ,67

Character Numeric “ Convert ” class “ Explicit Conversion ” Used in conversion of multiple types. There should be compatible types between conversions.

Example Output: 132

Example – 2

Console.ReadLine() Used for getting input from screen. String type value is returned. Returns when user press Enter key. Later, return value can be converted to other types.

Console.ReadLine()