CHAPTER 2 PART #4 OPERATOR 1 st semester 1432 -1433 King Saud University College of Applied studies and Community Service Csc 1101 1.

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

Types and Arithmetic Operators
Chapter 2: Java Fundamentals Operators. Introduction to OOP Dr. S. GANNOUNI & Dr. A. TOUIR Page 2 Content Group of Operators Arithmetic Operators Assignment.
1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.
JavaScript, Third Edition
Introduction to C Programming
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
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 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?
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Mathematical Calculations in Java Mrs. G. Chapman.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
CHAPTER # 2 Part 2 PROGRAMS AND DATA 1 st semster King Saud University College of Applied studies and Community Service CSC1101 By: Asma Alosaimi.
FUNDAMENTALS 2 CHAPTER 2. OPERATORS  Operators are special symbols used for:  mathematical functions  assignment statements  logical comparisons 
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Mathematical Calculations in Java Mrs. C. Furman.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
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.
Doing math In java.
OPERATORS Chapter2:part2. Operators Operators are special symbols used for: mathematical functions assignment statements logical comparisons Examples.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
Operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
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.
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.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
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.
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.
Chapter # 2 Part 2 Programs And data
ARITHMETIC IN C Operators.
Operators And Expressions
Operators and Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
2.5 Another Java Application: Adding Integers
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.
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
C Operators, Operands, Expressions & Statements
Fundamentals 2.
Chapter # 2 Part 2 Programs And data
Chapter 2: Java Fundamentals
Data Types and Expressions
Relational Operators.
OPERATORS in C Programming
Operator King Saud University
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

CHAPTER 2 PART #4 OPERATOR 1 st semester King Saud University College of Applied studies and Community Service Csc

outlines  Group of Operators  Arithmetic Operators  Assignment Operator  Order of Precedence  Increment/Decrement Operators  Relational Operators  Logical Operators 2

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 3

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

Arithmetic Operators in C  The C arithmetic operators are summarized in Fig  Note the use of various special symbols not used in algebra. 5

Arithmetic Operators  The following table summarizes the arithmetic operators available in C. This is an integer division where the fractional part is truncated. 6

Example  Example of division issues : 10 / 3 gives / 3 gives  As we can see, if we divide two integers we get an integer result. if one or both operands is a floating-point value we get a floating-point result. 7

Modulus Page 8  Generates the remainder when you divide two integer values.  5%3 gives 25%4 gives 1  5%5 gives 05%10 gives 5  Modulus operator is most commonly used with integer operands. If we attempt to use the modulus operator on floating-point values we will garbage!

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: i = 1; start = i; sum = firstNumber + secondNumber; avg = (one + two + three) / 3; Allways it is a variable identifier. It is either a literal | a variable identifier | an expression. 9

The Right Side of the Assignment Operator  The C assignment operator assigns the value on the right side of the operator to the variable appearing on the left side of the operator.  The right side may be either: Literal: ex. i = 1; Variable identifier: ex. start = i; Expression: ex. sum = first + second; 10

Assigning Literals  In this case, the literal is stored in the space memory allocated for the variable at the left side. int firstNumber=1, secondNumber; firstNumber = 234; secondNumber = 87; A A B B firstNumber 1 1 secondNumber ??? A. A. Variables are allocated in memory. B. B. Literals are assigned to variables. firstNumber 234 secondNumber 87 Code State of Memory 11

Assigning Variables  In this case, the value of the variable at the right side is stored in the space memory allocated for the variable at the left side. int firstNumber=1, i; firstNumber = 234; i = firstNumber; A A B B firstNumber 1 1 i ??? A. A. Variables are allocated in memory. B. B. values are assigned to variables. firstNumber 234 i Code State of Memory 12

Assigning Expressions  In this case, the result of the evaluation of the expression is stored in the space memory allocated for variable at the left side. int first, second, sum; first = 234; second = 87; Sum = first + second A A B B A. A. Variables are allocated in memory. B. B. Values are assigned to variables. Code State of Memory first 1 1 second ??? sum ??? first 234 second 87 sum

Updating Data Code State of Memory number ??? A. A. The variable is allocated in memory. B. 237 number B. The value 237 is assigned to number. int number; number = 237; A A B B C C number = 35; C C. The value 35 overwrites the previous value 237. number 237 number 35 14

Example # include // main method int main ( void) { int a, b, sum; a = 20; b = 10; sum = a + b; printf (” %d +%d = %d“, a,b,sum); return 0; } // end main 15

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

 The syntax is leftSide Op= rightSide ;  This is equivalent to: leftSide = leftSide Op rightSide ;  x%=5;  x = x % 5;  x*=y+w*z;  x = x*(y+w*z); Arithmetic/Assignment Operators Allways it is a variable identifier. It is an arithmetic operator. It is either a literal | a variable identifier | an expression. 17

Increment/Decrement Operators Only use ++ or   when a variable is being incremented/decremented as a statement by itself. x++; is equivalent to x = x+1; x--; is equivalent to x = x-1; 18

Increment/Decrement Operators 19

Relational Operators  Relational operators compare two values  They Produce a nonzero or zero value(true or false) depending on the relationship 20

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

Logical Operators Symbol Name && AND || OR ! NOT || TF TTT FTF && TF TTF FFF Page 22

Example int x = 1; int y = 0; int result; result = (x && y); result is assigned the value 0 (false) result = ((x || y) && x); (x || y) evaluates to 1 (true) (true && x) evaluates to 1 (true) result is then assigned the value 1 ( true) 23

Operators Precedence Parentheses(), inside-out Increment/decrement++, --, from left to right Multiplicative*, /, %, from left to right Additive+, -, from left to right Relational, =, from left to right Equality==, !=, from left to right Logical AND&& Logical OR|| Assignment=, +=, -=, *=, /=, %= Page 24

Operators Precedence - Example 25