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

Similar documents
Introduction to C++ Programming Pearson Education, Inc. All rights reserved.

Introduction to Programming

2.8. Decision Making: Equality and Relational Operators

Chapter 1 Introduction to Computers and C++ Programming

by Pearson Education, Inc. All Rights Reserved. 2

Lecture 3 Tao Wang 1

Introduction to C++ Programming. Adhi Harmoko S, M.Komp

Chapter 2, Part III Arithmetic Operators and Decision Making

2.5 Another Application: Adding Integers

Chapter 2: Introduction to C++

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

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

download instant at Introduction to C++

Full file at C How to Program, 6/e Multiple Choice Test Bank

CHAPTER 3 BASIC INSTRUCTION OF C++

Chapter 2 Author Notes

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

Introduction to Python Programming

Introduction to Programming

Introduction to C# Applications

C: How to Program. Week /Mar/05

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Another Simple Program: Adding Two Integers

Unit 3. Operators. School of Science and Technology INTRODUCTION

The C++ Language. Arizona State University 1

Chapter 2 - Introduction to C Programming

Creating a C++ Program

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

LECTURE 02 INTRODUCTION TO C++

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

CS242 COMPUTER PROGRAMMING

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Computer Programming CS F111

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

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

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

Chapter 2: Overview of C++

Operators in C. Staff Incharge: S.Sasirekha

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

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.

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

Introduction to C Programming

Operators. Java operators are classified into three categories:

UNIT- 3 Introduction to C++

VARIABLES & ASSIGNMENTS

Add Subtract Multiply Divide

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

Introductionto C++ Programming

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Chapter 1 Introduction to Computers and C++ Programming

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

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

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

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

2.11 Assignment Operators. Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator

IT 374 C# and Applications/ IT695 C# Data Structures

Computer Programming : C++

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

Programming for Engineers Introduction to C

Programming. C++ Basics

Chapter 4 - Notes Control Structures I (Selection)

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

Computer Programming C++ Classes and Objects 6 th Lecture

Exception Handling Pearson Education, Inc. All rights reserved.

Course Outline. Introduction to java

Introduction to Java Applications; Input/Output and Operators

Information Science 1

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

4. Structure of a C++ program

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

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

Course PJL. Arithmetic Operations

Università degli Studi di Bologna Facoltà di Ingegneria. Principles, Models, and Applications for Distributed Systems M

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

Learning the Language - V

Fundamentals of Programming CS-110. Lecture 3

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

Chapter 2 Basic Elements of C++

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

IS 0020 Program Design and Software Tools

BITG 1233: Introduction to C++

Introduction to C# Applications Pearson Education, Inc. All rights reserved.

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

Control Statements: Part Pearson Education, Inc. All rights reserved.

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

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

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

JAVA OPERATORS GENERAL

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Objectives. In this chapter, you will:

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Why Is Repetition Needed?

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

download instant at Introduction to C++

Transcription:

1 A Closer Look at the / Operator Used for performing numeric calculations C++ has unary, binary, and ternary s: unary (1 operand) - binary ( operands) 13-7 ternary (3 operands) exp1? exp : exp3 / (division) performs integer division if both operands are integers cout << 13 / ; // displays cout << 91 / 7; // displays 13 If either operand is floating point, the result is floating point cout << 13 /.0; // displays.6 cout << 91.0 / 7; // displays 13.0 Binary A Closer Look at the % Operator SYMBOL + OPERATION addition EXAMPLE ans = 7 + 3; VALUE OF ans 10 % (modulus) computes the remainder resulting from integer division - * / subtraction multiplication division ans = 7-3; ans = 7 * 3; ans = 7 / 3; 1 cout << 13 % ; // displays 3 % requires integers for both operands cout << 13 %.0; // error % modulus ans = 7 % 3; 1 3 6 Arithmetic s * Multiplication / Division Integer division truncates remainder 7 / evaluates to 1 % Modulus returns remainder 7 % evaluates to 1

Attempting to use the modulus (%) with noninteger operands is a compilation error. 7 Rules of precedence Operators in parentheses evaluated first Nested/embedded parentheses Operators in innermost pair first Multiplication, division, modulus applied next Operators applied from left to right Addition, subtraction applied last Operators applied from left to right 10 8 11 Straight-line form Required for arithmetic expressions in C++ All constants, variables and s appear in a straight line Grouping subexpressions Parentheses are used in C++ expressions to group subexpressions Same manner as in algebraic expressions Example a * ( b + c ) Multiple a times the quantity b + c Operator(s) Operation(s) Order of evaluation (precedence) ( ) Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. * / % + - Multiplication Division Modulus Addition Subtraction If there are several pairs of parentheses on the same level (i.e., not nested), they are evaluated left to right. Evaluated second. If there are several, they are evaluated left to right. Evaluated last. If there are several, they are evaluated left to right. Precedence of arithmetic s. 9 1 C++ operation C++ arithmetic Algebraic expression C++ expression Addition + f + 7 f + 7 Subtraction - p c p - c Multiplication * bm or b m b * m Some programming languages use s ** or ^ to represent exponentiation. C++ does not support these exponentiation s; using them for exponentiation results in errors. Division / x / y or x y or x y x / y Modulus % r mod s r % s Arithmetic s.

13 16 Using redundant parentheses in complex arithmetic expressions can make the expressions clearer. Standard algebraic equality or relational C++ equality or relational Sample C++ condition Meaning of C++ condition Relational s > > x > y x is greater than y < < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Equality s = == x == y x is equal to y!= x!= y x is not equal to y Equality and relational s. 1 17 A syntax error will occur if any of the s ==,!=, >= and <= appears with spaces between its pair of symbols. Order in which a second-degree polynomial is evaluated. Equality and Relational Operators Condition Expression can be either true or false Can be formed using equality or relational s if statement If condition is true, body of the if statement executes If condition is false, body of the if statement does not execute 1 Reversing the order of the pair of symbols in any of the s!=, >= and <= (by writing them as =!, => and =<, respectively) is normally a syntax error. In some cases, writing!= as =! will not be a syntax error, but almost certainly will be a logic error that has an effect at execution time. (cont ) 18 3

You will understand why when you learn about logical s in Chapter. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing, but usually produces incorrect results. 19 31 if ( number1 <= number ) 3 cout << number1 << " <= " << number << endl; 33 3 if ( number1 >= number ) 3 cout << number1 << " >= " << number << endl; 36 37 return 0; // indicate that program ended successfully 38 39 } // end function main Enter two integers to compare: 3 7 3!= 7 3 < 7 3 <= 7 Enter two integers to compare: 1!= 1 > 1 >= 1 Enter two integers to compare: 7 7 7 == 7 7 <= 7 7 >= 7 0 3 Confusing the equality == with the assignment = results in logic errors. The equality should be read is equal to, and the assignment should be read gets or gets the value of or is assigned the value of. Some people prefer to read the equality as double equals. As we discuss in Section.9, confusing these s may not necessarily cause an easy-to-recognize syntax error, but may cause extremely subtle logic errors. Place using declarations immediately after the #include to which they refer. 1 // Fig..13: fig0_13.cpp // Comparing integers using if statements, relational s 3 // and equality s. #include <iostream> // allows program to perform input and output 6 using std::cout; // program uses cout 7 using std::cin; // program uses cin 8 using std::endl; // program uses endl 9 10 // function main begins program execution 11 int main() 1 { 13 int number1; // first integer to compare 1 int number; // second integer to compare 1 16 cout << "Enter two integers to compare: "; // prompt user for data 17 cin >> number1 >> number; // read two integers from user 18 19 if ( number1 == number ) 0 cout << number1 << " == " << number << endl; 1 if ( number1!= number ) 3 cout << number1 << "!= " << number << endl; if ( number1 < number ) 6 cout << number1 << " < " << number << endl; 7 8 if ( number1 > number ) 9 cout << number1 << " > " << number << endl; 30 Indent the statement(s) in the body of an if statement to enhance readability.

8 Placing a semicolon immediately after the right parenthesis after the condition in an if statement is often a logic error (although not a syntax error). The semicolon causes the body of the if statement to be empty, so the if statement performs no action, regardless of whether or not its condition is true. Worse yet, the original body statement of the if statement now would become a statement in sequence with the if statement and would always execute, often causing the program to produce incorrect results. Operators Associativity Type () left to right parentheses * / % left to right multiplicative + - left to right additive << >> left to right stream insertion/extraction < <= > >= left to right relational ==!= left to right equality = right to left assignment Precedence and associativity of the s discussed so far. 6 9 It is a syntax error to split an identifier by inserting white-space characters (e.g., writing main as ma in). Refer to the precedence and associativity chart when writing expressions containing many opera-tors. Confirm that the s in the expression are performed in the order you expect. If you are uncertain about the order of evaluation in a complex expression, break the expression into smaller statements or use parentheses to force the order of evaluation, exactly as you would do in an algebraic expression. Be sure to ob-serve that some s such as assignment (=) associate right to left rather than left to right. 7 A lengthy statement may be spread over several lines. If a single statement must be split across lines, choose meaningful breaking points, such as after a comma in a comma-separated list, or after an in a lengthy expression. If a statement is split across two or more lines, indent all subsequent lines and left-align the group.