CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.11 Assignment Operators 2.12 Increment and Decrement Operators.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Sequential execution –Statements executed in order Transfer of control –Next statement executed not next.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
 2003 Prentice Hall, Inc. All rights reserved. 1 Input / Output Input/output –cin Standard input stream Normally keyboard –cout Standard output stream.
 2003 Prentice Hall, Inc. All rights reserved. 1 Algorithms Computing problems –Solved by executing a series of actions in a specific order Algorithm.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 2 part #4 Operator
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
 2003 Prentice Hall, Inc. All rights reserved. 1 Control Structures Outline Assignment Operators Increment and Decrement Operators Essentials of Counter-Controlled.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
Algorithm and Programming Branching Algorithm & C Program Control Dr. Ir. Riri Fitri Sari MM MSc International Class Electrical Engineering Dept University.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1Introduction 2.2Algorithms 2.3Pseudocode 2.4Control Structures.
CPS120: Introduction to Computer Science Operations Lecture 9.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
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.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Expressions and Operators in C. Expressions and Operators Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); Two types: – Function calls – The expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Control Structures Outline 2.1 Introduction 2.2 Algorithms 2.3 Pseudocode 2.4 Control Structures.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
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.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C++ Programming Language Lecture 4 C++ Basics – Part II
Chapter 4 – C Program Control
The if…else Selection Statement
Chapter 4 - Program Control
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Chapter 4 - Program Control
Chapter 2 - Introduction to C Programming
Capitolo 2 - Control Structures
2.6 The if/else Selection Structure
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 4 - Program Control
C++ Programming Language Lecture 4 C++ Basics – Part II
Introduction to C Programming
Lecture 9: Implementing Complex Logic
Presentation transcript:

CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad

Outline 1.Arithmetic Operators 2.Accumulation Operators 3.Incremental/ Decremental Operators 4.Equality/Relational Operators 5.Logical Operators 6.Confusing Equality (==) and Assignment (=) Operators

1. Arithmetic Operators OperatorSymbol Action Example Addition + Adds operandsx + y Subtraction - Subs second from firstx - y Negation - Negates operand-x Multiplication * Multiplies operandsx * y Division / Divides first by secondx / y (integer quotient) Modulus % Remainder of divide opx % y

1. Arithmetic Operators Example float a = 31/3; a = 10.3 float b = 31%3; b = 1.00 int c = 31/3; c = 10 int d = 31%3; d = 1

1. Arithmetic Operators Example: What is the output? #include void main() { float sum = 0 ; cout<< “ the value of sum is initially set to “ << sum<<endl; sum = sum + 98 ; cout<<”sum is now: “ << sum << endl ; sum = sum – 70 ; cout<<” sum is now: “ << sum<< endl ; sum = sum * 20 ; cout<<”sum is now : “ <<sum<<endl; sum= sum / 6 ; cout<<”sum is now:”<<sum<<endl; sum=sum%3 ; cout<<”sum is now:”<<sum<<endl; }

1. Arithmetic Operators Operator precedence Some arithmetic operators act before others (i.e., multiplication before addition)  Be sure to use parenthesis when needed Example: Find the average of three variables a, b and c  Do not use: a + b + c / 3  Use: (a + b + c ) / 3

1. Arithmetic Operators Rules of operator precedence: Operator precedence

1. Arithmetic Operators Example: What is the output? #include void main( ) { float a, b, c, d ; a = * 3 ; b = ( 5 * 2 – 3 ) / 6; c = 5 * 2 – 3 / 6; d = / 4 * 8; cout << “a=”<< a<<endl << “b=”<< b<<endl; cout << “c=”<< c<<endl << “d=”<< d<<endl; }

1. Arithmetic Operators Example: Calculate the average of three numbers #include void main( ) { float avg, grade1, grade2, grade3 ; grade1 = 8.5; grade2 = 12.0 ; grade3 = 9.0; avg = grade1 + grade2 + grade3 / 3.0; cout<<”the average is” <<setprecision(1)<<avg; } avg = ( grade1 + grade2 + grade3 )/3.0 ;

2. Accumulation/Assignment Operators Assignment expression abbreviations c = c + 3; can be abbreviated as c += 3; using the addition assignment operator Statements of the form variable = variable operator expression; can be rewritten as variable operator= expression;

2. Accumulation/Assignment Operators AlternativeExpressionOperator sum += 10 ;sum = sum + 10 ;+ = score – = 22 ;score = score – 22 ;- = x *= z;x = x * z;* = x /= y;x = x / y;/ = x %= y;x = x % y;% =

3. Incremental/ Decremental Operators AlternativeExpressionOperator i++ Or ++ii = i +1Incremental i-- Or --ii = i - 1Decremental

3. Incremental/ Decremental Operators Preincrement - When the operator is used before the variable ( ++c or –-c ) - Variable is changed, then the expression it is in is evaluated. Posincrement - When the operator is used after the variable ( c++ or c-- ) - Expression the variable is in executes, then the variable is changed. Example: If c = 5, then -cout << ++c; prints out 6 ( c is changed before cout is executed) -cout << c++; prints out 5 ( cout is executed before the increment. c now has the value of 6)

3. Incremental/ Decremental Operators When Variable is not in an expression - Preincrementing and postincrementing have the same effect. ++c; cout << c; and c++; cout << c; have the same effect.

4. Equality/Relational Operators

 2000 Prentice Hall, Inc. All rights reserved. Outline Load 2. main 2.1 Initialize num1 and num Input data 2.2 if statements 1// Fig. 1.14: fig01_14.cpp 2// Using if statements, relational 3// operators, and equality operators 4#include 5 6using std::cout; // program uses cout 7using std::cin; // program uses cin 8using std::endl; // program uses endl 9 10int main() 11{ 12 int num1, num2; cout << "Enter two integers, and I will tell you\n" 15 << "the relationships they satisfy: "; 16 cin >> num1 >> num2; // read two integers if ( num1 == num2 ) 19 cout << num1 << " is equal to " << num2 << endl; if ( num1 != num2 ) 22 cout << num1 << " is not equal to " << num2 << endl; if ( num1 < num2 ) 25 cout << num1 << " is less than " << num2 << endl; if ( num1 > num2 ) 28 cout << num1 << " is greater than " << num2 << endl; if ( num1 <= num2 ) 31 cout << num1 << " is less than or equal to " 32 << num2 << endl; 33 The if statements test the truth of the condition. If it is true, body of if statement is executed. If not, body is skipped. To include multiple statements in a body, delineate them with braces {}. Enter two integers, and I will tell you the relationships they satisfy: is not equal to 7 3 is less than 7 3 is less than or equal to 7 Notice the using statements.

 2000 Prentice Hall, Inc. All rights reserved. Outline exit ( return 0 ) Program Output 34 if ( num1 >= num2 ) 35 cout << num1 << " is greater than or equal to " 36 << num2 << endl; return 0; // indicate that program ended successfully 39} Enter two integers, and I will tell you the relationships they satisfy: is not equal to 7 3 is less than 7 3 is less than or equal to 7 Enter two integers, and I will tell you the relationships they satisfy: is not equal to is greater than is greater than or equal to 12 Enter two integers, and I will tell you the relationships they satisfy: is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7

5. Logical Operators ExampleMeaningOperator If(x > y && x<= 20) AND && If(x>y || x< 30) OR || If( ! x ) NOT !

5. Logical Operators && (logical AND ) - Returns true if both conditions are true || (logical OR ) - Returns true if either of its conditions are true ! (logical NOT, logical negation) - Reverses the truth/falsity of its condition - Returns true when its condition is false I- s a unary operator, only takes one condition Logical operators used as conditions in loops

5. Logical Operators Truth Tables ! AA FT TF A | | BBA TTT TFT TTF FFF A &&BBA TTT FFT FTF FFF AND Gate OR Gate NOT Gate

5. Logical Operators Example Given int i=3, k=5, j=0, m=-2; Evaluate: o (0 < i) && (i < 5) o (i > k) || (j < i) o ! (k > 0) o i+j < k o (i < 0) && (j < 7) o (i < k) || (j < 7) o (m > k) || (j > 0) o 3*i – 4/k < 2

5. Logical Operators Example: What is the output? Given int i=4; Evaluate: cout << (14+4*4 < 5*(4+3)- ++i); < 5*7 – ++i 30 < < 30 cout 5*(4+3) – i++ -1) > 5*7 – i > > 30

5. Logical Operators C++ is very economical when evaluating Boolean expression. Therefore, if in the evaluation of a compound Boolean expression, the computer can determine the value of the whole expression without any further evaluation, it does so. This called short circuiting.  ( True || expression ) True  ( False && expression ) False Example: Given: int A = 17, B = 65, C = 21, D = 19; (13 < = A) || (A < = 19) (D > = C ) && (B > = C) ! (C < = B ) && ! ( D < = C) Short Circuiting

6. Confusing Equality (==) and Assignment (=) Operators These errors are damaging because they do not ordinarily cause syntax errors. -Recall that any expression that produces a value can be used in control structures. Nonzero values are true, and zero values are false Example: if ( payCode == 4 ) cout << "You get a bonus!" << endl; -Checks the paycode, and if it is 4 then a bonus is awarded If == was replaced with = if ( payCode = 4 ) cout << "You get a bonus!" << endl; - Sets paycode to is nonzero, so the expression is true and a bonus is awarded, regardless of paycode.

6. Confusing Equality (==) and Assignment (=) Operators Lvalues Expressions that can appear on the left side of an equation Their values can be changed Variable names are a common example (as in x = 4; ) Rvalues Expressions that can only appear on the right side of an equation Constants, such as numbers (i.e. you cannot write 4 = x; ) Lvalues can be used as rvalues, but not vice versa