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

Similar documents
Expressions and Casting

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

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

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

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

LECTURE 3 C++ Basics Part 2

Programming in C++ 5. Integral data types

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

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

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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.

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

ECE 122 Engineering Problem Solving with Java

Chapter 2: Using Data

Chapter 2 Working with Data Types and Operators

CIS133J. Working with Numbers in Java

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

A Java program contains at least one class definition.

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

Introduction to Programming Using Java (98-388)

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

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

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

CS112 Lecture: Working with Numbers

3. Java - Language Constructs I

Chapter 3. Fundamental Data Types

Midterms Save the Dates!

1.3b Type Conversion

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

VARIABLES & ASSIGNMENTS

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

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

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

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

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

Operators. Java operators are classified into three categories:

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

Chapter 2: Using Data

EXPRESSIONS AND ASSIGNMENT CITS1001

Chapter 2: Data and Expressions

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

Data Types and Variables in C language

Chapter 2: Data and Expressions

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

Chapter 3 Structure of a C Program

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

Chapter 2: Data and Expressions

Programming Lecture 3

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

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

Object Oriented Software Design

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

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

More Programming Constructs -- Introduction

egrapher Language Reference Manual

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

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

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

CSC 1214: Object-Oriented Programming

SECTION II: LANGUAGE BASICS

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Basics of Java Programming

Outline. Data and Operations. Data Types. Integral Types

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

Zheng-Liang Lu Java Programming 45 / 79

Full file at

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

Variables and literals

Visual C# Instructor s Manual Table of Contents

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

CSE 142 Su 04 Computer Programming 1 - Java. Objects

Data Types. Data Types. Integer Types. Signed Integers

Lecture 2: Variables & Assignments

Chapter 7 Arithmetic

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

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Programming in C++ 6. Floating point data types

Type Conversion. and. Statements

Programming for Engineers Iteration

Program Fundamentals

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

Integers. N = sum (b i * 2 i ) where b i = 0 or 1. This is called unsigned binary representation. i = 31. i = 0

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

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

L-System Fractal Generator: Language Reference Manual

Operators and Expressions:

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

Python The way of a program. Srinidhi H Asst Professor Dept of CSE, MSRIT

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

CEN 414 Java Programming

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

COMP-202: Foundations of Programming. Lecture 5: More About Methods and Data Types Jackie Cheung, Winter 2016

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 This is a simple (and fairly useless) program Expressions and Casting 5-Nov-13 Rob Miles 3 1

Variable Declaration This statement creates three variables Expressions and Casting 5-Nov-13 Rob Miles 4 Variable Assignment The next statement assigns a value to one of the variables Expressions and Casting 5-Nov-13 Rob Miles 5 Next Variable Assignment This is another assignment Expressions and Casting 5-Nov-13 Rob Miles 6 2

Expression Evaluation 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 3

The Assignment Operator 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 4

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 5

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 6

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 7

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 8