Operators. Lecture 3 COP 3014 Spring January 16, 2018

Similar documents
LECTURE 3 C++ Basics Part 2

Basics of Java Programming

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

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

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Operators & Expressions

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

Chapter 3 Structure of a C Program

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

Chapter 2: Using Data

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Operators. Java operators are classified into three categories:

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

COMP Primitive and Class Types. Yi Hong May 14, 2015

Chapter 3: Operators, Expressions and Type Conversion

Add Subtract Multiply Divide

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

Programming in C++ 5. Integral data types

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

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.

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

Lecture 3 Tao Wang 1

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Unit 3. Operators. School of Science and Technology INTRODUCTION

Expressions and Precedence. Last updated 12/10/18

Information Science 1

Variables and Operators 2/20/01 Lecture #

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

More Programming Constructs -- Introduction

ECE 122 Engineering Problem Solving with Java

Arithmetic Operators. Portability: Printing Numbers

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Programming in C++ 6. Floating point data types

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

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

JAVA OPERATORS GENERAL

UNIT- 3 Introduction to C++

Pointer Basics. Lecture 13 COP 3014 Spring March 28, 2018

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

Engineering Computing I

Operators and Expressions:

Expressions and Casting

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

A flow chart is a graphical or symbolic representation of a process.

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

Visual C# Instructor s Manual Table of Contents

Expression and Operator

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

CIS133J. Working with Numbers in Java

ISA 563 : Fundamentals of Systems Programming

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

CMPT 125: Lecture 3 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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

These are reserved words of the C language. For example int, float, if, else, for, while etc.

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

Lecture 3. More About C

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

3. Java - Language Constructs I

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

Operators And Expressions

Information Science 1

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

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

Declaration and Memory

COMP 110 Introduction to Programming. What did we discuss?

Full file at

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

Arithmetic and IO. 25 August 2017

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

Chapter 2: Introduction to C++

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

Chapter 4: Basic C Operators

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

CHAPTER 3 Expressions, Functions, Output

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Computer System and programming in C

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

The New C Standard (Excerpted material)

1. Integers. 9 * celsius / Celsius to Fahrenheit. Precedence. (9 * celsius / 5) + 32

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

2. Distinguish between a unary, a binary and a ternary operator. Give examples of C++ operators for each one of them.

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

Arithmetic Processing

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

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

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

CS102: Variables and Expressions

C/C++ Programming for Engineers: Working with Integer Variables

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

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

The Arithmetic Operators

Transcription:

Operators Lecture 3 COP 3014 Spring 2018 January 16, 2018

Operators Special built-in symbols that have functionality, and work on operands operand an input to an operator Arity - how many operands an operator takes unary operator has one operand binary operator has two operands ternary operator has three operands Examples: int x, y = 5, z; z = 10; // assignment operator (binary) x = y + z; // addition (binary operator) x = -y; //-y is a unary operation (negation) x++; // unary (increment)

Operators cascading - linking of multiple operators, especially of related categories, together in a single statement: cascading arithmetic operators x = a + b + c - d + e; // cascading assignment operators x = y = z = 3; Precedence - rules specifying which operators come first in a statement containing multiple operators x = a + b * c; // b * c happens first, since * //has higher precedence than + Associativity - rules specifying which operators are evaluated first when they have the same level of precedence. Most (but not all) operators associate from left to right.

Assignment Operator Value on the right side (R-value) is assigned to (i.e. stored in) the location (variable) on the left side (L-value) R-value any expression that evaluates to a single value (name comes from right side of assignment operator) L-value A storage location! (not any old expression). A variable or a reference to a location. (name comes from left side of assignment operator Typical Usage variable name = expression The assignment operator returns a reference to the L-value Example: x = 5; y = 10.3; z = x + y; // right side can be an expression a + 3 = b; // ILLEGAL! Left side must be a // storage location

Assignment Operator Associates right-to-left x = y = z = 5; // z = 5 evaluated first, returns z, which is stored in y and so on Use appropriate types when assigning values to variables: int x, y; x = 5843; y = -1234; // assign integers to int variables double a, b; a = 12.98; b = -345.8; //assign decimal numbers to floats char letter, symb; letter = Z ; symb = $ ; // character literals to char types Be careful to not confuse assignment = with comparison ==

Arithmetic Operators Name Symbol Arity Usage Add + binary x + y Subtract - binary x - y Multiply * binary x * y Divide / binary x / y Modulus % binary x % y Minus - unary -x Division is a special case Modulus % not legal for floating point types. / gives floating point result double x = 19.0, y = 5.0, z; z = x / y; // z is now 3.8

Arithmetic Operators For integer types, / gives the quotient, and % gives the remainder (as in long division) int x = 19, y = 5, q, r; q = x / y; // q is 3 r = x % y; // r is 4 An operation on two operands of the same type returns the same type An operation on mixed types (if compatible) returns the larger type int x = 5; float y = 3.6; z = x + y; // what does z need to be? // x + y returns a float.

Operator Precedence Arithmetic has usual precedence 1. parentheses 2. Unary minus 3. *, /, and % 4. + and - 5. operators on same level associate left to right Many different levels of operator precedence (about 18) When in doubt, can always use parentheses Example z = a - b * -c + d / (e - f); // 7 operators in this statement What order are they evaluated in?

Some short-cut assignment operators (with arithmetic) v += e; means v = v + e; v -= e; means v = v - e; v *= e; means v = v * e; v /= e; means v = v / e; v %= e; means v = v % e; Please look at the Note on Operator Precedence on the course website.

Increment and Decrement Operators These are shortcut operators for adding or subtracting 1 from a variable. Shortcut for x=x+1 ++x; // pre-increment (returns reference to new x) x++; // post-increment (returns value of old x) Shortcut for x=x-1 --x; // pre-decrement x--; // post-decrement Pre-increment: incrementing is done before the value of x is used in the rest of the expression Post-increment: incrementing is done after the value of x is used in the rest of the expression

Increment and Decrement Operators Note - this only matters if the variable is actually used in another expression. The two statements (x++ and ++x)by themselves have the same effect. Examples int x = 5, count = 7; result = x * ++count; // result = 40, count = 8 int x = 5, count = 7; result = x * count++; // result = 35, count = 8

Automatic Type Conversions Typically, matching types are expected in expressions If types don t match, ambiguity must be resolved There are some legal automatic conversions bewteen built-in types. Rules can be created for doing automatic type conversions between user-defined types, too For atomic data types, can go from smaller to larger types when loading a value into a storage location. General rule of thumb: Allowed if no chance for partial data loss. char -> short -> int -> long -> float -> double -> long double Should avoid mixing unsigned and signed types, if possible

Automatic Type Conversions: Examples int i1, i2; double d1, d2; char c1; unsigned int u1; d1 = i1; // legal. c1 = i1; // illegal. trying to stuff int into char i1 = d1; // illegal. Might lose decimal point data. i1 = c1; // legal u1 = i1; // dangerous (possibly no warning) d2 = d1 + i2; // result of double + int is a double d2 = d1 / i2; // floating point division (at least // one operand a float type)

Explicit type conversions (casting) Older C-style cast operations look like: c1 = (char)i2; // cast a copy of the value of i2 as a char, and assign to c1 i1 = (int)d2; // cast a copy of the value of d2 as an int, and assign to i1 Better to use newer C++ cast operators. For casting between regular variables, use static cast c1 = static cast<char>(i2); i1 = static cast<int>(d2); Just for completeness, the newer C++ cast operators are: static cast dynamic cast const cast reinterpret cast