OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.

Slides:



Advertisements
Similar presentations
If Statements & Relational Operators Programming.
Advertisements

Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
JavaScript, Third Edition
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
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.
Introduction To C++ Programming 1.0 Basic C++ Program Structure 2.0 Program Control 3.0 Array And Structures 4.0 Function 5.0 Pointer 6.0 Secure Programming.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Operaciones y Variables
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Computer Science 210 Computer Organization Introduction to Boolean Algebra.
CS31: Introduction to Computer Science I Discussion 1A 4/9/2010 Sungwon Yang
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CPS120: Introduction to Computer Science Operations Lecture 9.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
COMPUTER PROGRAMMING. variable What is variable? a portion of memory to store a determined value. Each variable needs an identifier that distinguishes.
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.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Module 5 JavaScript Operators. CS346 Javascript-52 Examples  JS-5 Examples.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Operators.
LESSON 3 Operators. Operators  Symbol that tells the computer to perform certain mathematical or logical manipulations.
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
LECTURE # 6 : STRUCTURED PROGRAMMING C++ OPERATORS By Mr. Ali Edan.
CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc
Fundamentals 2 1. Programs and Data Most programs require the temporary storage of data. The data to be processed is stored in a temporary storage in.
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.
Rational Expressions relational operators logical operators order of precedence.
if ( condition ) statement; if is a Java reserved word The condition must be a boolean expression. It must evaluate to either true or false.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Computer Science 210 Computer Organization
ARITHMETIC IN C Operators.
Operators and Expressions
University of Central Florida COP 3330 Object Oriented Programming
Computing Fundamentals
Chapter 2 Assignment and Interactive Input
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Operators and Expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
OPERATORS (2) CSC 111.
Bools and simple if statements
Variables Kingdom of Saudi Arabia
Computer Science 210 Computer Organization
Fundamentals 2.
Summary Two basic concepts: variables and assignments Basic types:
Introduction to Programming – 4 Operators
Chapter 2: Java Fundamentals
Life is Full of Alternatives
Chapter 4: Expression and Operator
OPERATORS AND EXPRESSIONS IN C++
Operator King Saud University
Presentation transcript:

OPERATORS Chapter2:part2

Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples of operators: // uses + operator – 4 * (5 – 3) // uses +, -, * operators Expressions: can be combinations of variables and operators that result in a value

Groups of Operators There are 5 different groups of operators: Arithmetic Operators Assignment Operator Increment / Decrement Operators Relational Operators Logical Operators

Arithmetic Operators

Order of Precedence 1. ( ) evaluated first, inside-out 2. , /, or % evaluated second, left-to-right 3. +,  evaluated last, left-to-right

Basic Assignment Operator We assign a value to a variable using the basic assignment operator (=). Assignment operator stores a value in memory. The syntax is leftSide = rightSide ; Examples: Literal: ex. i = 1; Variable identifier: ex. start = i; Expression: ex. sum = first + second;

Arithmetic/Assignment Operators C++ allows combining arithmetic and assignment operators into a single operator: Addition/assignment+= Subtraction/assignment  = Multiplication/assignment  = Division/assignment/= Remainder/assignment %=

Increase and Decrease (++, --) The increase operator (++) and the decrease operator (--) increase or reduce by one the value stored in a variable. They are equivalent to +=1 and to -=1, respectively. 1. c++; 2. c+=1; 3. c=c+1; The three above expressions are all equivalent in its functionality: the three of them increase by one the value of c.

Increase and Decrease (++, --) In the case that the increase operator is used as a prefix (++a) the value is increased before the result of the expression is evaluated Therefore the increased value is considered in the outer expression. In case that it is used as a suffix (a++) the value stored in a is increased after being evaluated Therefore the value stored before the increase operation is evaluated in the outer expression

Example // apply increment-decrement operators 1. B=3; A=++B; // A contains 4, B contains 4 2. B=3; A=B++; // A contains 3, B contains 4

Relational and Equality Operators Relational operators compare two values They Produce a boolean value (true or false) depending on the relationship

Example int x = 3; int y = 5; bool result; result = (x > y); now result is assigned the value false because 3 is not greater than 5

Logical Operators Used to combine multiple conditions: && is the logical AND || is the logical OR ! (logical NOT, logical negation)

Logical Operators && is the logical AND True if both conditions are true Example: age>=50 $$ gender==‘f’

Logical Operators || is the logical OR True if either of conditions is true Example: age>=50 || gender==‘f’

Logical Operators ! (logical NOT, logical negation) Return true when its condition is false and vice versa Example: !(grade==maximumGrade) Alternative: grade != maximumGrade

Boolean Data Type 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 18

Example1 // compound assignment operators #include using namespace std; int main () { int a, b=3; a = b; a+=2; // equivalent to a=a+2 cout << a; return 0; } 19

Example 2 Convert the following algebraic expression to an arithmetic C++ expressions: 20

Example 3 21