Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.

Slides:



Advertisements
Similar presentations
M The University Of Michigan Andrew M. Morgan EECS Lecture 01 Savitch Ch. 2 C++ Basics Flow Of Control.
Advertisements

Computer Programming w/ Eng. Applications
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 CS 105 Lecture 3 Constants & Expressions Wed, Jan 26, 2011, 4:15 pm.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
By Dr. Awad Khalil Computer Science & Engineering Department
A First Book of ANSI C Fourth Edition
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Variables, Assignment & Math Storing and naming data.
Chapter 2 Overview of C++ Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville University Millersville, PA
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using.
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Overview of C++ Chapter C++ Language Elements t Comments make a program easier to understand t // Used to signify a comment on a single line.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
Computing with C# and the.NET Framework Chapter 2 C# Programming Basics ©2003, 2011 Art Gittleman.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Introducing C++ Programming Lecture 3 Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 09/03/04CS150 Introduction to Computer Science 1 What Data Do We Have.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Arithmetic Operations (L05) * Arithmetic Operations * Variables * Declaration Statement * Software Development Procedure Problem Solving Using C Dr. Ming.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Chapter 02 (Part II) Introduction to C++ Programming.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
CSE 220 – C Programming Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
BASIC ELEMENTS OF A COMPUTER PROGRAM
ITEC113 Algorithms and Programming Techniques
Data Types, Identifiers, and Expressions
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Assignment and Arithmetic expressions
Computing with C# and the .NET Framework
Arithmetic Operator Operation Example + addition x + y
Lecture3.
Expressions and Assignment
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
COMS 261 Computer Science I
Chapter 2: Overview of C++
Presentation transcript:

Arithmetic Expressions in C++

CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in C++ Arithmetic expressions in C++ Operators’ precedence Arithmetic expressions’ evaluation examples

CSCE 1063 Data Declaration Remember the memory anatomy? It is much easier to set aside memory with names (identifiers). Thus the declaration statements are used to set aside memory with a specific name for the data and define its values and operations. type identifier-list; Examples: char response; char c, grade = ‘A’; int minElement; int n, j = 1; float score; float x, y, z = 40.0; bool flag; The value can change during execution H AddressContents ADD 7 3 x n c

CSCE 1064 Data Declaration (cont’d) When variables are declared 1.Memory allocated for value of specified type 2.Variable name associated with that memory location 3.Memory initialized with values provided (if any) 27

CSCE 1065 Constant Declarations A constant is a memory cell whose value cannot change during execution once it is set in the declartion. const type constant-identifier = value; E.g.: const float KM_PER_MILE = 1.609; It is a good C++ programming practice to capitalize all letters for constant names. Any data type (e.g. int, float, char, or bool) could be declared as constant.

CSCE 1066 Identifiers A valid identifier (variable or function name) must consist of letters, digits, or underscore only. A valid identifier must not begin with a digit. Valid identifiers: letter, letter1, _letter Invalid identifiers: 1letter, float, hell o You cannot use a C++ reserved word as an identifier. Always try to associate meaningful identifiers. It is a good C++ programming practice to capitalize the first letter of each word (after the first) when using multiple word identifiers Remember that C++ is case sensitive ( cost != Cost )

CSCE 1067 Arithmetic Operators in C++ +Addition - Subtraction *Multiplication /Division % Modulus (remainder operator, only for integers) Examples of integer modulus: 7 % 2 = % 100 = % 5 = 4 15 % 0 undefined Examples of integer division (result is integer): 15 / 3 = 5 15 / 2 = 7 0 / 15 = 0 15 / 0 undefined

CSCE 1068 Mixed Data Type Arithmetic in C++ Example: 4.6 / 2 evaluates to 2.3 Rule: when an integer and a floating point operand are combined by an operator, the integer gets converted to the floating point type.

CSCE 1069 Arithmetic Expressions in C++ An assignment statement has the following format: variable = expression; e.g.: kms = KM_PER_MILE * miles;  The arithmetic expression to the right of the assignment operator (=) gets evaluated first  Then the result is stored in the variable on the left side of the assignment operator

CSCE Arithmetic Expressions in C++ (cont’d) Mixed-type assignments:  If the variable on left side of assignment is of different type than the type of the evaluated expression on the right side of =, the result of the expression must be converted to the appropriate type.  Examples: float a, b, x; int m, n; a = 10;// result is 10.0 stored in a b = 3.5; m = 5; n = 10; x = m / n;// result is 0 assigned to x; therefore x is 0.0 m = b * 3;// result is 10 assigned to m

CSCE Arithmetic Expressions in C++ (cont ’ d) What if the arithmetic expression contains more than one operator? x = z - (a + b / 2) + w * -y; How many operators do we have in the above expression? Computers follow operator precedence rules to evaluate expressions.  The formula: m = y - b x - a  The computer equivalent is: m = (y - b) / (x - a); Must use ( ) because of operator precedence.

CSCE Order of Operator Precedence ( )nested expressions evaluated inside out unary +, - *, /, % If there are several, then evaluate from left-to-right binary +, - If there are several, then evaluate from left-to-right = Highest Lowest Associativity Warning: watch out for the types of operands and the type of the result from evaluating each operation!

CSCE Example 1 x = z - (a + b / 2) + w * -y; x integer z ab wy (3 + 9 / 2) + 2 * / 4 + x = 8 - (3 + 9 / 2) + 2 * / 2 (3 + 4 ) * * * 10 +

CSCE Example 2 m = x + k / 2; m integer x k / 2 convert to float convert to int / 2

CSCE Next lecture will be about Library Functions