CS Kangmei Yang. Page 1

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

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

Chapter 4 Section 2 Operations on Decimals

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

Chapter 5: Computer Arithmetic. In this chapter you will learn about:

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Learning Objectives. Binary over Decimal. In this chapter you will learn about:

Lecture 3 Tao Wang 1

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

Computer Programming CS F111

Chapter 5: Computer Arithmetic

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

Divide: Paper & Pencil

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

Add Subtract Multiply Divide

LECTURE 3 C++ Basics Part 2

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

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

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

Chapter 2: Data and Expressions

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

Accuplacer Arithmetic Study Guide

Arithmetic Expressions in C

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

Operators & Expressions

Chapter 03: Computer Arithmetic. Lesson 09: Arithmetic using floating point numbers

Fundamentals of Programming CS-110. Lecture 3

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

Example 2: Simplify each of the following. Round your answer to the nearest hundredth. a

Information Science 1

Computer System and programming in C

Variables, Data Types, and Arithmetic Expressions Learning Objectives:

Introduction to TURING

C: How to Program. Week /Mar/05

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

Chapter 1 Introduction to Computers and C++ Programming

by Pearson Education, Inc. All Rights Reserved. 2

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Learning the Language - V

Chapter 2 - Introduction to C Programming

Chapter 2: Data Types and Arithmetic Expressions TRUE/FALSE

EXAMPLE 1. Change each of the following fractions into decimals.

Objectives. Data Types (continued) Data Types 4. การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

Chapter 2: Data and Expressions

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

HOW TO DIVIDE: MCC6.NS.2 Fluently divide multi-digit numbers using the standard algorithm. WORD DEFINITION IN YOUR WORDS EXAMPLE

Math Glossary Numbers and Arithmetic

VARIABLES & ASSIGNMENTS

Fixed-Point Math and Other Optimizations

The. Binary. Number System

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

ARITHMETIC EXPRESSION

Chapter 2: Using Data

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

Programming in C++ 5. Integral data types

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

Place Value. Verbal Form: 30,542 = Thirty thousand, five hundred forty-two. (Notice we don t use the word and.)

Expressions and Precedence. Last updated 12/10/18

Introduction to Programming

H192 Midterm 1 Review. Tom Zajdel

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

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Course PJL. Arithmetic Operations

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Work relative to other classes

Rational numbers as decimals and as integer fractions

Lesson 1: THE DECIMAL SYSTEM

Exponential Notation

More Programming Constructs -- Introduction

Week 4 Lecture 1. Expressions and Functions

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

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

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

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

Lecture 3. More About C

Introduction to C Programming. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

COMP 111. Introduction to Computer Science and Object-Oriented Programming

Chapter 2, Part III Arithmetic Operators and Decision Making

CMSC201 Computer Science I for Majors

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

Values, Variables, Types & Arithmetic Expressions. Agenda

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

การเขยนโปรแกรมพนฐาน ภาคการศ กษาท 1 ป การศ กษา การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต วทยาการคอมพวเตอรเบองตน น

Floating-Point Data Representation and Manipulation 198:231 Introduction to Computer Organization Lecture 3

Logic Design: Part 2

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

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

Chapter 2: Basic Elements of C++

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

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

9 Multiplication and Division

Adding and Subtracting with Decimals

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

Chapter 2: Using Data

1.1 Review of Place Value

Introduction to Computers. Laboratory Manual. Experiment #3. Elementary Programming, II

Transcription:

CS 111 - Kangmei Yang Page 1

Variable Question: How do we know which type should we use for each variable? Answer: It depends on how we going to use the variable data. Page 2

Variable Type - int What can we do with integers? - Arithmetic Operation Description + Addition - Subtraction * Multiplication / Division % Modulus (Remainder) ( ) Parentheses C++ does not directly support Exponent operation. Page 3

Order of Operation Operation Precedence ( ) Contents inside parentheses has highest precedence. Should evaluate first. * / % Equal precedence Evaluated from left to right + -. Equal precedence Evaluated from left to right Page 4

Examples of Operation What is the remainder when 5 divided by 2? 5 % 2 Find the remainder of the sum of two numbers divide by 2. A) number1 + number 2 % 2 = B) 2 % number1 + number2 = C) (number1 + number2) % 2 = Is the answer A, B or C? Page 5

Integer Division Integer division in C++ will truncate any decimal value, for example: 5 / 2 = 2 for an integer division 4 / 2 = 2 this means 5 / 2 = 4 / 2 10/ 3 = 3 11 / 2 = 5 The resulting type is an Integer. What makes this an integer division? - Both the dividend and divisor are integers (not decimal). Page 6

Double (decimal) Division Double division in C++ will retain the appropriate decimal value, for example: 5 / 2.0 = 2.5 4 / 2.0 = 2 10.0/ 3 = 3.33333 11.0 / 2.0 = 5.5 The resulting type is a Double. What makes this a decimal division? - Either the dividend or divisor is a decimal. Page 7

All Other Operations Same rule apply to Addition, Subtraction and Multiplication. If both of the values are of type int, result will be int. If either one of the type is double, result will be double. Page 8

Data Types and Order of Operation Example: 5.0 + 5 / 2 -What is the resulting data type of the first operation? -What is the resulting data type of the second operation? 5 + 5.0 / 2 -What is the resulting data type of the first operation? -What is the resulting data type of the second operation? Page 9

Type Casting - If we want to convert from int to double for a more precise result, we can do so with type casting. - Type casting is a temporary change from one type to another. double value = (double) 5 / 2; //value = 2.5 int approx = (int) value; //approx = 2, value = 2.5 Page 10

Variable Type - string How can we use data type string? -A paragraph, a name, a comma, zip code, phone number, etc. Why do we use string for zip code and phone number? -We use it as plain text, no calculation involved. Page 11

Arithmetic for string? It s meaningless to do something like, minus orange from apple. However, there is + for data type string. - It means to append the second string to the end of the first string. The length of the new string the length of the first string plus the length of the second string. Example: string address = 65-30 Kissena Blvd. ; string city = Flushing, N.Y. ; string zipcode = 11367 ; string newadd = address + city + zipcode; string num = 2 + 2 ; Page 12

Comments Comments in C++ source code is not part of the program, but serve many purposes: -Explain to the reader or other programmers what this program does. -Note to other developers on what the block of code is doing. -A To-Do list on tasks you need to complete. Page 13

Styles of Comments -In line comments, marks rest of the text in the line as comments. //this is a in line comment -Multi-line comments, marks a block of text as comments. /* this is multi-line comments */ Page 14