STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Introduction to C Programming
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
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.
Introduction to C Programming
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Operaciones y Variables
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Chapter 7 Expressions and Assignment Statements. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Arithmetic Expressions Arithmetic evaluation.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
CPS120: Introduction to Computer Science Operations Lecture 9.
1.  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.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Operators & Expressions
Chapter Seven: Expressions and Assignment Statements Lesson 07.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
Chapter 02 (Part II) Introduction to C++ Programming.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Chapter 7: Expressions and Assignment Statements
Operators And Expressions
Computing Fundamentals
Relational Operations
Chapter 7: Expressions and Assignment Statements
Variable Declaration, Data types, Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Introduction to C++ Programming
Variables Kingdom of Saudi Arabia
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Data Types and Expressions
Chapter 4: Expression and Operator
OPERATORS in C Programming
Operator King Saud University
HNDIT11034 More Operators.
Lecture 9: Implementing Complex Logic
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

STRUCTURED PROGRAMMING C++ Operators

Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision making operators (logical and relational)  Conditional operator  Precedence and associativity of operators  Common errors

Objectives 3  By the end you should be able to:  Use the assignments and arithmetic operators  How decisions are made in your programs  Write simple decision –making statements  Recognize the precedence and associatively of operators  Identify and use the C++ conditional operator (?:)  Identify and use Increment & decrement operators  Use operators in output statements  Properly mix data types in expression and calculations

Operators  Data connectors within expression or equation  Concept related  Operand: data that operator connects and processes  Resultant: answer when operation is completed  Operators types based on their mission  Assignment  Arithmetic: addition, subtraction, modulo division,...etc  Relational: equal to, less than, grater than, …etc  Logical (decision-making): NOT, AND, OR 4

Operators (cont.)  Operators types based on number of operands  Unary operators Have only one operand May be prefix or postfix e.g. !  Binary operators Have two operands Infix e.g. + && ==  Ternary operators Have three operands e.g. ? : 5

Assignment operators  Assignment statement takes the form below  Binary operators  Expression is evaluated and its value is assigned to the variable on the left side  Shorthand notation  varName = varName operator expression;  varName operator = expression; 6 varName = expression; c = c + 3; c += 3; c = c + 3; c += 3;

Assignment operators (cont.)  Assignment between objects of the same type is always supported 7

Arithmetic Operators  All of them are binary operators  Arithmetic expressions appear in straight-line form  Parentheses () are used to maintain priority of manipulation 8

Arithmetic Operators 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 9

Example  The statement is written in algebra as z = pr % q + w / (x – y)  How can we write and evaluate the previous statement in C++ ? z = p * r % q + w / (x - y);

Increment and Decrement Operators  Unary operators  Adding 1 to or (subtracting 1 from) variable’s value  Increment operator gives the same result of (c=c+1) or (c+=1)  Decrement operator gives the same result of (c=c-1) or (c-=1) 11

Increment and Decrement Operators (cont.) 12

Examples 13 int x = -10, y; y = ++x; cout << “x = “ << x << endl; cout << “y = “ << y << endl; int x = -10, y; y = ++x; cout << “x = “ << x << endl; cout << “y = “ << y << endl; Example # 1 x = -9 y = -9 x = -9 y = -9 output # 1 int x = -10, y; y = x++; cout << “x = “ << x << endl; cout << “y = “ << y << endl; int x = -10, y; y = x++; cout << “x = “ << x << endl; cout << “y = “ << y << endl; Example # 2 x = -9 y = -10 x = -9 y = -10 output # 2

Relational and Equality Operators  Binary operators  Used in decision -making statements 14

Relational and Equality Operators (cont.)  Have the same level of precedence  Applied from left to right  Used with conditions  Return the value true or false  Used only with a single condition 15

Logical Operators  Used to combine between multiple conditions  && (logical AND)  true if both conditions are true gender == 1 && age >= 65  || (logical OR)  true if either of condition is true semesterAverage >= 90 || finalExam >= st condition2 nd condition

Logical Operators (cont.)  ! (logical NOT, logical negation)  Returns true when its condition is false, and vice versa !( grade == sentinelValue ) Also can be written as grade != sentinelValue 17

Conditional operator ( ?: )  Ternary operator requires three operands  Condition  Value when condition is true  Value when condition is false  Syntax 18 Condition ? condition’s true value : condition’s false value

Examples Can be written as Can be written as.. ? 19 grade >= 60 ? cout<<“Passed” : cout<<“Failed”; Example # 1 cout = 60 ? “Passed” : “Failed”); int i = 1, j = 2, Max; Max = ( i > j ? i : j ); int i = 1, j = 2, Max; Max = ( i > j ? i : j ); Example # 2

Summary of Operator Precedence and Associativity 20

bool Variables in Expressions  false is zero and true is any non-zero  The following codes applies implicit conversion between bool and int 21 int x = -10 ; bool flag = x ; // true int a = flag ; // assign the value 1 int b = !flag; // assign the value 0 x = flag + 3; // assign the value 4 int x = -10 ; bool flag = x ; // true int a = flag ; // assign the value 1 int b = !flag; // assign the value 0 x = flag + 3; // assign the value 4 bool test1,test2,test3 ; int x = 3, y = 6, z = 4 ; test1 = x > y ; // false test2 = !(x == y ); // true test3 = x < y && x < z ; // true test3 = test1 || test2 ; // true test2 = !test1; // true bool test1,test2,test3 ; int x = 3, y = 6, z = 4 ; test1 = x > y ; // false test2 = !(x == y ); // true test3 = x < y && x < z ; // true test3 = test1 || test2 ; // true test2 = !test1; // true Code # 1Code # 2

Common Compilation Errors  Attempt to use % with non-integer operands  Spaces between pair of symbols e.g. (==, !=, …etc)  Reversing order of pair of symbols e.g. =!  Confusing between equality (==) and assignment operator (=) 22

Exercise What is the output of the following program? 1 #include 2 3 using std::cout; 4 using std::endl; 5 6 int main() 7 { 8 int x; int y; int z; 9 10 x = 30; y = 2; z = 0; cout << (++++x && z ) << endl; 13 cout << x * y + 9 / 3 << endl; 14 cout << x << y << z++ << endl; return 0; } // end main 1 #include 2 3 using std::cout; 4 using std::endl; 5 6 int main() 7 { 8 int x; int y; int z; 9 10 x = 30; y = 2; z = 0; cout << (++++x && z ) << endl; 13 cout << x * y + 9 / 3 << endl; 14 cout << x << y << z++ << endl; return 0; } // end main

Exercise What is wrong with the following program? 1 int main() 2 { 3 int a,b,c,sum; 4 sum = a + b + c ; 5 return 0; 6 } 1 int main() 2 { 3 int a,b,c,sum; 4 sum = a + b + c ; 5 return 0; 6 }

Included Sections 25 Chapter 2: from section 6 and 7 Chapter 4: section 11 and 12