Type Conversion. and. Statements

Similar documents
1.3b Type Conversion

Chapter 3 Structure of a C Program

Introduction to Programming Using Java (98-388)

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Types. C Types. Floating Point. Derived. fractional part. no fractional part. Boolean Character Integer Real Imaginary Complex

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

3. Java - Language Constructs I

Flow Control. CSC215 Lecture

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

Expressions and Casting

Introduction to C++ with content from

CSE 431S Type Checking. Washington University Spring 2013

A First Program - Greeting.cpp

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Lecture Set 4: More About Methods and More About Operators

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Programming for Engineers Iteration

Programming in C++ 6. Floating point data types

JAVA Programming Fundamentals

Type Checking. Prof. James L. Frankel Harvard University

egrapher Language Reference Manual

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Java Programming Fundamentals. Visit for more.

Chapter 7 Arithmetic

Algorithms & Data Structures

Lecture Set 4: More About Methods and More About Operators

Information Science 1

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Full file at

CS 251 Intermediate Programming Java Basics

Chapter 7. Additional Control Structures

CSC Java Programming, Fall Java Data Types and Control Constructs

Program Flow. Instructions and Memory. Why are these 16 bits? C code. Memory. a = b + c. Machine Code. Memory. Assembly Code.

Java Notes. 10th ICSE. Saravanan Ganesh

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Declaration. Fundamental Data Types. Modifying the Basic Types. Basic Data Types. All variables must be declared before being used.

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Types, Variables, and Constants

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

CS112 Lecture: Primitive Types, Operators, Strings

CSI33 Data Structures

C programming basics T3-1 -

Day05 A. Young W. Lim Sat. Young W. Lim Day05 A Sat 1 / 14

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

CSC 1214: Object-Oriented Programming

More Programming Constructs -- Introduction

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Data Handing in Python

Information Science 1

Bitwise Operator and Typecasting

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Arithmetic Operators. Portability: Printing Numbers

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

PROGRAMMING FUNDAMENTALS

C-Programming. CSC209: Software Tools and Systems Programming. Paul Vrbik. University of Toronto Mississauga

Declaration and Memory

Program Fundamentals

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Chapter 2: Using Data

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

C-LANGUAGE CURRICULAM

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Introduce C# as Object Oriented programming language. Explain, tokens,

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Overview of C. Basic Data Types Constants Variables Identifiers Keywords Basic I/O

C#: framework overview and in-the-small features

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

Chapter 2. Elementary Programming

Pace University. Fundamental Concepts of CS121 1

Type systems. Static typing

Informatics Ingeniería en Electrónica y Automática Industrial

Lecture 1. Types, Expressions, & Variables

CSCI 171 Chapter Outlines

Java Fall 2018 Margaret Reid-Miller

Unit-II Programming and Problem Solving (BE1/4 CSE-2)

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Tokens, Expressions and Control Structures

EXPRESSIONS AND ASSIGNMENT CITS1001

Function I/O. Last Updated 10/30/18

CHAPTER 3 Expressions, Functions, Output

Chapter 2: Using Data

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Visual C# Instructor s Manual Table of Contents

ARG! Language Reference Manual

Java enum, casts, and others (Select portions of Chapters 4 & 5)

& Control Flow of Program

Object-oriented programming. and data-structures CS/ENGRD 2110 SUMMER 2018

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Basic Types, Variables, Literals, Constants

Transcription:

and Statements

Type conversion changing a value from one type to another Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 2 tj

Suppose we had the following expression: voltage * current where: voltage was a variable of type int (5) current was a variable of type float (2.5) what would the expression evaluate to? 3 tj

Implicit Type Conversion Type conversions done automatically by the compiler Each type has a RANK bool < char < short < int < long < long long < float < double < long double complex types match the floating types 4 tj

Implicit Type Conversion int * float float 1) int converted to float 2) multiplication 3) result is of type float char + long int long int 1) char converted to long int 2) addition 3) result is of type long int 5 tj

Implicit Type Conversion No Side Effect int days; float rate; days * rate float days remains an int 6 tj

Explicit Type Conversion Cast or casting Force a type conversion Use the unary operator type cast (desired_type) var 7 tj

Explicit Type Conversion int a = 5; int b = 2; a/b (float) a / b a / (float) b (float) (a/b) 8 tj

Explicit Type Conversion int a = 5; int b = 2; a/b 2 (float) a / b 5.0 / 2 5.0 / 2.0 2.5 Answers a / (float) b 5 / 2.0 5.0 / 2.0 2.5 (float) (a/b) (float) (5/2) (float) 2 2.0 9 tj

Explicit Type Conversion No Side effect int a = 5; int b = 2; (float) a / b a = 5 a / (float) b b = 2 (float) (a/b) a = 5, b = 2 10 tj

Assignment Type Conversion Assignment operator = value evaluate right side expression side effect left side is assigned the value int a = 5; int b = 6; int c = 7; a = b + c; evaluate right side (b + c) value is 13 side effect a is assigned the value 13 11 tj

Assignment Type Conversion Regardless of any implicit or explicit type conversions the assignment operator side effect cannot change the type of a variable int a = 5; float b = 12.0; int c = 5; a = b / c; c is promoted to type float right side is evaluated 12.0 / 5.0 2.4 value is demoted to match the receiving variable (side effect) a = 2 12 tj

Assignment Type Conversion Regardless of any implicit or explicit type conversions the assignment operator side effect cannot change the type of a variable int a = 5; float b = 6; int c = 7; b = a + c; right side is evaluated 12 int value is promoted to match the receiving variable 12.0 side effect: b = 12.0 13 tj

Statements Statement Causes the processor to do something 11 types of statements Null Expression Return Compound Conditional Labeled Switch Iterative Break Continue Goto 14 tj

Statements Statement Null Statement Causes nothing to happen ; while(1){ ; } 15 tj

Statements Statement Expression Statement An expression with a semi-colon added Causes the processor to evaluate the expression Causes the processor to complete any side effects Processor discards the expression Special note: the side effect of the assignment operator is to store a value into a variable 16 tj

Statements Statement Expression Statement a = 5; value is 5, side effect is a holds the value 5 a = b = 5; same precedence, operate R to L b = 5 value is 5, side effect is b holds the value 5 a = 5 value is 5, side effect is a holds the value 5 17 tj

Statements Statement Expression Statement a = 5; a++; value is 5 side effect is a takes the value 6 the value is then discarded 18 tj

Statements Statement Return Statement Terminates all functions (including main) int main(void) { return 1; } 19 tj

Statements Statement Compound Statement Block of code containing zero or more statements These statements are considered a single entity int main(void) { return 1; } // multiple statements 20 tj

Statements Statement Pre-processor commands vs statements #define int_rate 0.25 #define int_rate 0.25; // pre-processor command // error payment = int_rate * balance; payment = 0.25; * balance; creates a compiler error at the payment line but you never see the expansion very difficult to catch 21 tj