Expressions and Casting

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

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

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Arithmetic Expressions in C

Introduction to Programming Using Java (98-388)

CIS133J. Working with Numbers in Java

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

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

LECTURE 3 C++ Basics Part 2

Programming in C++ 5. Integral data types

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Section we will not cover section 2.11 feel free to read it on your own

Units 0 to 4 Groovy: Introduction upto Arrays Revision Guide

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

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

3. Java - Language Constructs I

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

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

CS112 Lecture: Working with Numbers

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

CS313D: ADVANCED PROGRAMMING LANGUAGE

Midterms Save the Dates!

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

ECE 122 Engineering Problem Solving with Java

Chapter 2: Using Data

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

Chapter 2 Working with Data Types and Operators

Table of Contents Date(s) Title/Topic Page #s. Abstraction

EXPRESSIONS AND ASSIGNMENT CITS1001

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions

Chapter 3. Fundamental Data Types

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

1.3b Type Conversion

Chapter 2: Data and Expressions

VARIABLES & ASSIGNMENTS

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Type Conversion. and. Statements

Object Oriented Software Design

CSC 1214: Object-Oriented Programming

egrapher Language Reference Manual

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Operators. Java operators are classified into three categories:

Programming Lecture 3

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

Chapter 2: Using Data

Basics of Java Programming

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

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

Data Types and Variables in C language

Zheng-Liang Lu Java Programming 45 / 79

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Variables and literals

Chapter 3 Structure of a C Program

CSE 142 Su 04 Computer Programming 1 - Java. Objects

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Primitive Data, Variables, and Expressions; Simple Conditional Execution

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

Chapter 7 Arithmetic

Program Fundamentals

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

CHAPTER 3 Expressions, Functions, Output

A variable is a name for a location in memory A variable must be declared

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

More Programming Constructs -- Introduction

Ex: If you use a program to record sales, you will want to remember data:

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

CEN 414 Java Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

These are notes for the third lecture; if statements and loops.

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Storing Data in Objects

SECTION II: LANGUAGE BASICS

Outline. Data and Operations. Data Types. Integral Types

Introduction to Computer Science I Spring 2010 Sample mid-term exam Answer key

CMPT 125: Lecture 3 Data and Expressions

Lecture Set 4: More About Methods and More About Operators

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Full file at

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Full file at

Visual C# Instructor s Manual Table of Contents

Chapter 3: Operators, Expressions and Type Conversion

BASIC ELEMENTS OF A COMPUTER PROGRAM

UNIT- 3 Introduction to C++

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Transcription:

Expressions and Casting C# Programming Rob Miles

Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence when the program runs Remember that at this point we should be creating code to implement our solution Expressions and Casting 5-Nov-13 Rob Miles 2

Simple Program class Assignment { static void Main () { int first, second, third ; first = 1 ; second = 2 ; second = second + first ; } } This is a simple (and fairly useless) program Expressions and Casting 5-Nov-13 Rob Miles 3

Variable Declaration class Assignment { static void Main () { int first, second, third ; first = 1 ; second = 2 ; second = second + first ; } } This statement creates three variables Expressions and Casting 5-Nov-13 Rob Miles 4

Variable Assignment class Assignment { static void Main () { int first, second, third ; first = 1 ; second = 2 ; second = second + first ; } } The next statement assigns a value to one of the variables Expressions and Casting 5-Nov-13 Rob Miles 5

Next Variable Assignment class Assignment { static void Main () { int first, second, third ; first = 1 ; second = 2 ; second = second + first ; } } This is another assignment Expressions and Casting 5-Nov-13 Rob Miles 6

Expression Evaluation class Assignment { static void Main () { int first, second, third ; first = 1 ; second = 2 ; second = second + first ; } } This assignment evaluates an expression and puts the result into the variable called second Expressions and Casting 5-Nov-13 Rob Miles 7

Expressions second + first Operand Operator Operand An expression is made up of operators and operands Expressions and Casting 5-Nov-13 Rob Miles 8

Complex Expressions The simplest kind of expression is a single literal value: 23 More complicated ones involve literals, variables, operators and brackets 2 * ( width + height ) * 3.25 Expressions and Casting 5-Nov-13 Rob Miles 9

The Assignment Operator second = second + first ; The assignment operator takes the result of an expression and puts it into a variable This is the fundamental means by which a program works on data Expressions and Casting 5-Nov-13 Rob Miles 10

Simple Arithmetic Operators Op. Use - * unary minus, the minus that C# uses in negative numbers, e.g. -1. Unary means applying to only one item. multiplication, note the use of the * rather than the more mathematically correct but confusing x. division, because of the difficulty of drawing one / number above another on a screen we use this character instead + Addition. subtraction. Note that we use exactly the same - character as for unary minus. Expressions and Casting 5-Nov-13 Rob Miles 11

Data and Type C# provides a range of types to store numbers Each type can store values in a particular range The compiler will not let us combine values in a way that might lose data Expressions and Casting 5-Nov-13 Rob Miles 12

Dangerous Code int i; double d = 1.5; i = d; This code will fail to compile The compiler is not happy to put a double precision value into an integer Expressions and Casting 5-Nov-13 Rob Miles 13

Narrowing When you put a double value into an integer variable it won t fit: The double value may have a fractional part The double value may be too big to fit in the integer variable This is called narrowing and the compiler will not let a program do it Expressions and Casting 5-Nov-13 Rob Miles 14

Casting Casting is a way that the programmer can take responsibility for a narrowing operation It is an explicit narrowing operation that the programmer asks to be done The compiler will generate code that performs the conversion Expressions and Casting 5-Nov-13 Rob Miles 15

Adding a Cast int i; double d = 1.5; i = (int) d; The cast operation is given a particular target type In this case we are casting the value d to an integer Expressions and Casting 5-Nov-13 Rob Miles 16

Responsible Casting When you perform a cast you are telling the compiler that you know better than it You are forcing the compiler to do something it would normally not like to For this reason you need to be sure when you cast that it is sensible to do so Otherwise you will break your program Expressions and Casting 5-Nov-13 Rob Miles 17

Casting Literals float x; x = (float) 3.14; You can use casting to convert literals into particular types in your program The cast works on the value immediately to the right of the cast type Expressions and Casting 5-Nov-13 Rob Miles 18

Limited Casting Powers int i; string s; i = 99; s = (string) i; You can t use casting to convert from integer to string (or back) It only works between numeric types Expressions and Casting 5-Nov-13 Rob Miles 19

Types in Expressions We have seen that the result produced by an operator depends on the items it is working on + can add integers or concatenate strings Now we are going to explore how this effects the way that expressions are worked out Expressions and Casting 5-Nov-13 Rob Miles 20

Integer Division double d; d = 1/2; Console.WriteLine ( "d is : " + d ) ; This happens because the compiler uses a version of the division operator that matches the operands Integer values use integer division Expressions and Casting 5-Nov-13 Rob Miles 21

Forcing double Division double d; d = (double) 1/2; Console.WriteLine ( "d is : " + d ) ; The compiler will generate a double precision division if one of the operands is a double precision one We can do this by casting Expressions and Casting 5-Nov-13 Rob Miles 22

Good Casting It is said that good casting makes a movie much better I think this is true of programs too I often add the casts so that it is clear what is going on, even if the compiler doesn t need them Expressions and Casting 5-Nov-13 Rob Miles 23

Summary Expression evaluation is how data is processed by a program The evaluation is performed by operators A program will not be allowed to narrow data unless an explicit cast is given Casting can also be used to determine which operator is used in an expression Expressions and Casting 5-Nov-13 Rob Miles 24