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

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

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

LECTURE 3 C++ Basics Part 2

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

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

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

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 Casting

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

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

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

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

The C++ Language. Arizona State University 1

Programming in C++ 5. Integral data types

2 nd Week Lecture Notes

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

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

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Lesson 1: Arithmetic Review

Lecture 3 Tao Wang 1

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

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

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

Exercise: Inventing Language

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

Chapter 2: Introduction to C++

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

Chapter 3: Operators, Expressions and Type Conversion

Non-numeric types, boolean types, arithmetic. operators. Comp Sci 1570 Introduction to C++ Non-numeric types. const. Reserved words.

Information Science 1

Basics of Java Programming

Chapter 2: Overview of C++

Operators & Expressions

Chapter 3 Structure of a C Program

by Pearson Education, Inc. All Rights Reserved. 2

Unit 3, Lesson 2 Data Types, Arithmetic,Variables, Input, Constants, & Library Functions. Mr. Dave Clausen La Cañada High School

Number System. Introduction. Natural Numbers (N) Whole Numbers (W) Integers (Z) Prime Numbers (P) Face Value. Place Value

CS102: Variables and Expressions

LECTURE 02 INTRODUCTION TO C++

Add Subtract Multiply Divide

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

UNIT- 3 Introduction to C++

CSCE 120: Learning To Code

CHAPTER 3 Expressions, Functions, Output

Accuplacer Arithmetic Study Guide

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

Information Science 1

Lecture 4 CSE July 1992

Programming and Data Structures

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

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

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

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

Array Elements as Function Parameters

Variables and Operators 2/20/01 Lecture #

Learning Log Title: CHAPTER 3: ARITHMETIC PROPERTIES. Date: Lesson: Chapter 3: Arithmetic Properties

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

Mini-Lesson 1. Section 1.1: Order of Operations PEMDAS

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

CPS122 Lecture: From Python to Java

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CS Kangmei Yang. Page 1

This watermark does not appear in the registered version - Slide 1

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

Operators. Java operators are classified into three categories:

Unit 3. Operators. School of Science and Technology INTRODUCTION

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

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

CSI Lab 02. Tuesday, January 21st

BASIC ELEMENTS OF A COMPUTER PROGRAM

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

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

Introduction to Scientific Programming with C++

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

Pick any positive integer. If the integer is even, divide it by 2. If it is odd,

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

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

A Java program contains at least one class definition.

Excerpt from "Art of Problem Solving Volume 1: the Basics" 2014 AoPS Inc.

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

Programming in C++ 6. Floating point data types

Arithmetic Expressions in C

4. Inputting data or messages to a function is called passing data to the function.

CMPT 125: Lecture 3 Data and Expressions

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 4: Basic C Operators

Lesson 1: THE DECIMAL SYSTEM

VARIABLES & ASSIGNMENTS

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

Declaration and Memory

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

Chapter 7 Arithmetic

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

CIS133J. Working with Numbers in Java

Transcription:

Operators Class 5

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

Data Types Data Type A data type is a set of values and a set of operations defined on those values. in class 4 we talked about the values of various data types in this class we will focus on some of the operations

Assignment perhaps the most important operator of all is assignment = assignment is a binary operator it has a left-hand side (lhs) and a right-hand side (rhs) the lhs must resolve to an lvalue: an address where a value can be stored the rhs must resolve to an rvalue: a value that can be stored in the lhs address units sold = 12; integer value integer location the value 12 is copied into the location denoted by units sold

An Illegal Assignment 12 = units sold; 12 is not an lvalue; it is not a storage location

Initialization assignment can be combined with variable declaration this is called initialization unsigned int units sold = 12; this is much more concise than declaration-then-assignment unsigned int units sold; units sold = 12;

Scope Scope A variable s scope is the region of the program in which the variable exists and in which its name can be legally used. a variable s scope extends from declaration to the end of the closest containing block int main() { int value = 100; start of scope cout << value << endl; return 0; } end of scope

Scope you cannot reference a variable outside of its scope int main() { cout << value << endl; } int value = 100; return 0; illegal out of scope scope

Re-Declaring a Variable the scope concept means that it is illegal to re-declare a variable in the same scope int main() { int value = 100; declaration cout << value << endl; int value = 200; illegal: redeclared in same scope return 0; } you can re-assign a variable repeatedly, but not re-declare it

Declaration Location best practice in coding dictates that in general a variable s scope should be as small as possible this means declaring a variable as close to the place where it is used as possible the style guide also has this rule double rate; double hours; cout << "Enter hours: "; cin >> hours; cout << "Enter rate: "; cin >> rate; double pay = hours * rate; cout << "Pay: " << pay << endl; return 0; ok: small scope bad: large scope

Unary Minus C++ has one unary arithmetic operator, the minus sign it can be used with signed integer and with floating point types int x = 5; int y = -x; double a = -12.34;

Binary Operators C++ has several binary arithmetic operators can be used with all integer and floating point types in chapter 2, we assume both lhs and rhs of these operators are of exactly the same type in chapter 3 we will look at mixing types Operator Purpose + addition - subtraction * multiplication / division % modulus (integer only)

Division all the operators are straightforward except division if both operands are floating point types, the result is similar to the results on a calculator double x = 10.0 / 4.0; x becomes 2.5 if both operands are integer types, the result is an integer quotient with no fractional part or remainder int x = 10 / 4; x becomes 2

Modulus applies only to integer types not defined for floating point types used to give the remainder after division completes 5 2 = 2 r 1 quotient remainder

Modulus when using modulus, the dividend can be positive, zero, or negative the divisor should always be positive (just like in elementary school) a negative divisor is legal in C++, but mathematically very confusing, and thus to be avoided an extremely useful operator two big uses: 1. determine if a number is even or odd 2. (combined with division) determine specific digits in a base-10 number

Even or Odd a number is even if its remainder when divided by 2 is 0 156 % 2 is 0, so 156 is an even number a number is odd if its remainder when divided by 2 is 1 157 % 2 is 1, so 157 is an odd number

Digits in a Number imagine I have: int x = 12345; I need to extract the hundreds digit (in this case 3) from the value hundreds digit = (x / 100) % 10;

Digits in a Number imagine I have: int x = 12345; I need to extract the hundreds digit (in this case 3) from the value hundreds digit = (x / 100) % 10; how would you get the thousands digit?

Digits in a Number imagine I have: int x = 12345; I need to extract the hundreds digit (in this case 3) from the value hundreds digit = (x / 100) % 10; how would you get the thousands digit? an example use: convert 255 minutes into minutes and hours unsigned int total_minutes =...; unsigned int hours = total_minutes / 60; unsigned int minutes = total_minutes % 60; unsigned check = hours * 60 + minutes;

Parentheses just like in algebra, we use parentheses to override precedence x = 1 + 2 * 3; multiplication has higher precedence than addition, so this yields 7 x = (1 + 2) * 3; parentheses override precedence, so this yields 9

Parentheses just like in algebra, we use parentheses to override precedence x = 1 + 2 * 3; multiplication has higher precedence than addition, so this yields 7 x = (1 + 2) * 3; parentheses override precedence, so this yields 9 however, only use parentheses either when they are necessary or when they improve readability x = 1 * (2 * 3); irrelevant; multiplication is commutative x = (1 + 2 + 3); unnecessary, and thus confusing x = (1 + 2) / (3 + 4); good; mathematically necessary x = (2 * a) + (b * 4 / c); good; not mathematically necessary, but improves readability

Comments C++ has two ways to indicate comments 1. here-to-end-of-line comments, denoted by double-slashes // 2. comments delimited by /* and */ here-to-end comments are limited to a single editor line delimited comments can span multiple lines comment symbols that appear inside of strings are not comments cout << "Hello // world" << endl;

Comments what kind and how many comments to write is largely a matter of style in this class, we will recognize three kinds of comments: 1. a header comment at the beginning of a file, telling the purpose of the file and the name of the author 2. a header comment at the beginning of a function, explained later in the course 3. in-code comments that explain the purpose of this paragraph of code

Comments what kind and how many comments to write is largely a matter of style in this class, we will recognize three kinds of comments: 1. a header comment at the beginning of a file, telling the purpose of the file and the name of the author 2. a header comment at the beginning of a function, explained later in the course 3. in-code comments that explain the purpose of this paragraph of code make sure your comments actually add information //Enter Radius does this add information? cout << "Please enter the radius of a circle: "; cin >> radius;

Variable Name Comments Gaddis discusses a fourth kind a comment that documents the purpose of a variable double pay rate; // holds the hourly pay rate

Variable Name Comments Gaddis discusses a fourth kind a comment that documents the purpose of a variable double pay rate; // holds the hourly pay rate this is very definitely not allowed in this course if you have to explain the name, that means it is a poor name you should create self-documenting variable names double hourly pay rate; that way, you won t have to look back at the declaration to see if the variable is holding an hourly or weekly pay rate just looking at the variable itself tells you that it s an hourly pay rate

Named Constants consider the following statement in a finance program dealing with loans amount = balance * 0.069; there are two potential problems with this code 1. 0.069 is an anonymous amount; there is no hint why it is in the program. Is this interest? A fee? A tax rate? A one-time adjustment? The reader doesn t know.

Named Constants consider the following statement in a finance program dealing with loans amount = balance * 0.069; there are two potential problems with this code 1. 0.069 is an anonymous amount; there is no hint why it is in the program. Is this interest? A fee? A tax rate? A one-time adjustment? The reader doesn t know. 2. Think of what happens if this multiplier changes in the future. If this is an interest rate, what if the rate changes?

Magic Numbers in programming, an anonymous value is called a magic number it is a literal that appears in code with no hint of its purpose 0.069 is a magic number if instead we wrote: amount = balance * interest rate; it would be obvious what was going on the style guide forbids the use of magic numbers

Repeated Use in a banking program, there may be many places where the interest rate is involved in calculations if 0.069 is used 37 times over 25 pages of code, two bad things can happen 1. what if the interest rate changes to, say, 0.066? all 37 occurrences of 0.069 over 25 pages must be found and changed! 2. even if the rate doesn t change, what if one of the 37 occurrences is mis-typed as 0.068? the chances of noticing it are slim

Repeated Use in a banking program, there may be many places where the interest rate is involved in calculations if 0.069 is used 37 times over 25 pages of code, two bad things can happen 1. what if the interest rate changes to, say, 0.066? all 37 occurrences of 0.069 over 25 pages must be found and changed! 2. even if the rate doesn t change, what if one of the 37 occurrences is mis-typed as 0.068? the chances of noticing it are slim to avoid both anonymous magic numbers and repeated use of a literal, we use named constants

Named Constants const double INTEREST RATE = 0.069;... blah = blah blah * INTEREST RATE;... yada yada yada INTEREST RATE yada yada; if the interest rate changes, you need only change the code in one place interest rate is always identical; no chance of a typo since the interest rate never changes in one run of the program, it is a constant like a variable, but cannot be re-assigned style calls for constant identifiers to be in ALL UPPER CASE