LECTURE 3 C++ Basics Part 2

Similar documents
Operators. Lecture 3 COP 3014 Spring January 16, 2018

Basics of Java Programming

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

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

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

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

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

Chapter 2: Using Data

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

Operators & Expressions

Operators. Java operators are classified into three categories:

Chapter 3: Operators, Expressions and Type Conversion

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

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

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

Lecture 3 Tao Wang 1

Chapter 3 Structure of a C Program

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

ISA 563 : Fundamentals of Systems Programming

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

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

Expressions and Precedence. Last updated 12/10/18

Visual C# Instructor s Manual Table of Contents

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

UNIT- 3 Introduction to C++

Chapter 2: Introduction to C++

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

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

Programming in C++ 5. Integral data types

JAVA OPERATORS GENERAL

Arithmetic Operators. Portability: Printing Numbers

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

ECE 122 Engineering Problem Solving with Java

More Programming Constructs -- Introduction

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

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Reserved Words and Identifiers

Declaration and Memory

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

CHAPTER 3 BASIC INSTRUCTION OF C++

Add Subtract Multiply Divide

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) 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: Basic Elements of C++

Basic Operations jgrasp debugger Writing Programs & Checkstyle

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

Information Science 1

Full file at

Programming in C++ 6. Floating point data types

1.3b Type Conversion

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

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

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

Variables and Operators 2/20/01 Lecture #

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

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

CHAPTER 3 Expressions, Functions, Output

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

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

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

The Arithmetic Operators

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

Lecture 3. More About C

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

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

LECTURE 02 INTRODUCTION TO C++

Expressions and Casting

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

Fundamentals of Programming

3. Java - Language Constructs I

CMPT 125: Lecture 3 Data and Expressions

Operators and Expressions:

Expression and Operator

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.

Operators And Expressions

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

Computer System and programming in C

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

CIS133J. Working with Numbers in Java

What we will do today Explain and look at examples of. Programs that examine data. Data types. Topic 4. variables. expressions. assignment statements

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

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

EXPRESSIONS AND ASSIGNMENT CITS1001

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

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

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

The C++ Language. Arizona State University 1

Engineering Computing I

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

Programming Lecture 3

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

Chapter 7. Expressions and Assignment Statements ISBN

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

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

Data Types and Variables in C language

Values, Variables, Types & Arithmetic Expressions. Agenda

Full file at

Transcription:

LECTURE 3 C++ Basics Part 2

OVERVIEW Operators Type Conversions

OPERATORS Operators are special built-in symbols that have functionality, and work on operands. Operators are actually functions that use a more familiar notation. We'll discuss this further when we get to functions. Operand: an input to an operator. Arity: how many operands an operator takes. Unary operator: takes one operand. Binary operator: takes two operands. Ternary operator: takes three operands.

OPERATORS Examples: int x, y = 5, z; // declaring 3 ints x, y, and z. Also initializing y to 5. z = 10; x = y + z; x = -y; x++; // assignment operator (binary) // addition (binary operator) // -y is a unary operation (negation) // unary (increment)

OPERATORS Cascading: linking of multiple operators, especially of related categories, together in a single statement: x = a + b + c - d + e; x = y = z = 3; // cascading arithmetic operators // cascading assignment operators This works because the result of one operation sends back the answer (i.e. a return value) in its place, to be used in the next piece of the statement. In the above, (a + b) happens first, then the answer becomes the first operand in the next + operation.

OPERATORS 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 +

OPERATORS 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. x = a + b - c; // a + b happens first, then c is subtracted from the result.

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. L-value: A storage location! A variable or a reference to a location. The assignment operator returns a reference to the L-value. Typical usage: variable_name = expression

ASSIGNMENT OPERATOR Examples 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, then y = z, then x = y.

ASSIGNMENT OPERATOR Use appropriate types when assigning values to variables: int x, y; x = 5843; y = -1234; double a, b; a = 12.98; b = -345.8; char letter, symb; letter = 'Z'; symb = '$'; bool flag; flag = true; flag = false; // assigning integers to int variables // can assign decimal numbers to float types // can assign character literals to char types // can assign true or false to bool variables 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

ARITHMETIC OPERATORS Division is a special case. Modulus % not legal for floating point types. Division / gives floating point result. double x = 19.0, y = 5.0, z; z = x / y; // z is now 3.8 For integer types, division / gives the quotient, and modulus % 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

ARITHMETIC OPERATORS 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 (negation) 3. *, /, and % 4. + and 5. Operators of same precedence associate left to right. Aside from arithmetic, there are many different levels of operator precedence (about 18) so when in doubt, you can always use parentheses. Example: z = a - b * -c + d / (e - f); What order are they evaluated in? // 7 operators in this statement

OPERATOR PRECEDENCE Example of basic arithmetic operations here.

SHORT-CUT ASSIGNMENT OPERATORS 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;

INCREMENT AND DECREMENT ++x; // pre-increment (returns reference to new x). x++; // post-increment (returns value of old x). --x; // ^shortcuts for x = x + 1 // pre-decrement x--; // post-decrement // ^shortcuts for x = x - 1 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 Note: this only matters if the variable is actually used in another expression. These two statements by themselves have the same effect: x++; ++x; 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 between 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. int double. c1 = i1; // illegal. Trying to stuff int (usually 4 bytes) into char (usually 1 byte). i1 = d1; // illegal. Might lose decimal point data. i1 = c1; // legal. char int. u1 = i1; // legal but dangerous (possibly no warning). d2 = d1 + i2; // result of double + int is a double. d2 = d1 / i2; // floating point division (at least one operand is a float type).

EXPLICIT TYPE CONVERSIONS Older C-style cast operations look like: c1 = (char)i2; // cast a copy of the value of i2 as a char, and assign to c. 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);

EXPLICIT TYPE CONVERSIONS Just for completeness, the newer C++ cast operators are: static_cast<new_type> dynamic_cast<new_type> const_cast<new_type> reinterpret_cast<new_type>