1 Operators And Expressions. 2 Operators Arithmetic Operators Relational and Logical Operators Special Operators.

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.
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Introduction to C Programming
Conditionals – if - else Dr. Sajib Datta
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
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.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
JavaScript, Third Edition
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 =
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 5.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
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.
15-Nov-15 All the Operators. operators.ppt 2 Precedence An operator with higher precedence is done earlier (precedes) one with lower precedence A higher.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
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.
Operators & Expressions
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 4: Basic C Operators.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
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.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
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.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
LESSON 3 Expressions, Statements, & Operators. STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null.
Dr. Sajib Datta Sep 3,  A new operator used in C is modulus operator: %  % only used for integers, not floating-point  Gives the integer.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
Chapter 4 – C Program Control
Operators And Expressions
University of Central Florida COP 3330 Object Oriented Programming
University of Central Florida COP 3330 Object Oriented Programming
Intro to C Tutorial 4: Arithmetic and Logical expressions
Arrays, For loop While loop Do while loop
Chapter 4 - Program Control
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Associativity and Prescedence
Introduction to Programming – 4 Operators
Chapter 3 Operators and Expressions
Chapter 4: Expression and Operator
Chap 7. Advanced Control Statements in Java
Operator and Expression
OPERATORS in C Programming
DATA TYPES There are four basic data types associated with variables:
OPERATORS in C Programming
Presentation transcript:

1 Operators And Expressions

2 Operators Arithmetic Operators Relational and Logical Operators Special Operators

3 Arithmetic Operators OperatorAction –Subtraction, also unary minus +Addition *Multiplication /Division %Modulus --Decrement ++Increment

4 Precedence and Associativity Precedence of the Arithmetic operators: High (unary minus) * / % Low+ - - a * b – c((- a) * b) – c

5 inside parenthesesExpressions inside parentheses are evaluated first. 1 * (2 - 3) Operators on the same level of precedence are evaluated from left to right. (Associativity) –5 (((1 + 2) + 3) + 4) –5

6 Increment & Decrement Operators Provide a concise notation for incrementing or decrementing a variable by 1. Are unary operators. ++x or x++ --x or x-- Can be applied to variables but not to constants or ordinary expressions. ++i;legal cnt--;legal 777++;illegal ++(a * b -1); illegal

7 May either prefix or postfix the operand. Prefix++x; or Postfixx++; x = x + 1; ++ & -- both cause a value of a variable to change in memory. ( Have a side effect).

8 Increment Postfix: i++; Expression value is the current value (Before you increment) then it increments. “use - then increment” Increment Prefix:++i; Expression value is the value After you increment. “increment - then use” Decrement Postfix:i--; “use - then decrement” Decrement Prefix:--i; “decrement - then use”

9 Examples x =10;y = ++x; y 11 x =10;y = x++; y 10 int i = 3, j = 2, k; i++;i j = ++i;ji k = i++;k i k = (--j+3) kj

10 l = 4; n = 3; m = 2; x = l * n + m++;x After the assignment to x. m 14 3

11 int a, b, c=0; a = ++c; b = c++; a = ? b = ? c= ? int b = 2, d = 4; 7--b*++d 7-((-b)*(++d)) ? int j = 2, k = 3, m = 4; j*=k=m+5 j=(j*(k=(m+5))) ?

12 int a,b; a = 1; b = 12; printf (“a+++b = %d/n”, a+++b); a = 1; b = 12; printf (“a++ +b = %d/n”, a++ +b); a = 1; b = 12; printf (“a+ ++b =% d/n”, a+ ++b);

13 Relational and Logical Operators Relational refer to the relationship that value can have with one another. Logical refers to the ways these relationships can be connected. True is any value other than zero. False is zero.

14 Relational Operators: OperatorAction >Greater than >=Greater than or equal <Less than <=Less than or equal = =Equal !=Not equal Logical Operators: OperatorAction &&AND ||OR !NOT

15 The truth table for the logical operators. True(1), False(0). pqp&&qp || q!p

16 Precedence and Associativity High! > >= < <= = = != && Low|| !0&&0||0 ((!0)&&0)||0 FALSE

17 Both are lower in precedence than the arithmetic operators. 10 > > (1 + 12) FALSE 0 Associativity left to right. int x; x = 100; printf(''%d", x>10); __?

18 !A is false (0) if A’s value is: __. is true (1) if A’s value is: __. !!5 ! (!5)! (0) 1 NOT (Any NON-zero)0 5 && 3 ? Examples

19 inti, j = 1; j = j && (i = 2); 1) (i=2)i 2) &&j && 2 true && true1 3) =j ( ) needed

20 ji j = j && (i = = 3); 1) (i = = 3) false0 2) && j&& 0 0 3) = j ( ) not needed

21 ji j = j || (i/2) 1) (i/2)(2/2) 1 2) ||j|| 1true1 3) =j ( ) not needed

22 ji j = !j && (i = i + 1); 1)i )=i 3)!!j !1 0 4)&&0 && 3 5)=j

23 The Comma Operator lLowest precedence of all the operators. lCauses a sequence of operations, “do this and this and this…”. lIs a binary operator. expression_1, expression_2 lAssociates left to right. expression_1 is evaluated first expression_2 is evaluated second x = (y=3, y+1); x 4 ( ) needed

24 l The Comma Expression as a whole has the value and type of expression_2. inti = 2; j = 4; j = i++, i - j; * i * j(3-4) 3

25 It allows multiple initializations and multiple processing of indices. for (sum=0, i=1; i<=n; ++i) sum += i; Comma Operators can be useful in control statements though many “C” advocates discourage their use.

26 e.g.int i; i = 0; for ( ; i < 10; putchar (‘a’ + i), i++); will output?  3rd expression in the for statement is a comma expression.  putchar is called,executed.Then i is increased.  Most commas in a program DO NOT represent comma operators. see text - pg. 99

27 The ( ) and [ ] Operators Parentheses are operators that increase the precedence of the operations inside them. Square brackets perform array indexing (See Chapter 9 for a discussion of array.) int main(void) { char s[80]; s[3] = 'X'; printf(''%c", s[3]); return 0; }

28 The Conditional Operator ? Ternary operator. A powerful and convenient operator that replaces certain statements of the if-then- else form. Exp1 ? Exp2: Exp3 Stands for:if Exp1 then Exp2 else Exp3

29 x = 10; y = x>9 ? 100 : 200; x = 10; if(x>9) y = 100; else y = 200; Examples

30 int i, h, j = 2; i = (j==2) ? 1:3; k = (i>j) ? i:j; ( ) not needed i get 1 k get max of I or j

31 This statement does what? c = (c > =‘a’ && c < = ‘z’) ? c - (‘z’ - ‘Z’):c; IF True - have a Lower case letter in the variable “C”. Exper2: c - (‘z’ - ‘Z’) will give Capital Letter of whatever is in C. e.g. a - (‘z’ - ‘Z’) 97 - (122 – 90) = 65 which is ‘A’. IF False – Capital Letter and leaves it alone.

32 Expressions An expression in C is any valid combination of operators, constants, functions and variables. An statement is a valid expression followed by a semicolon. func(); /* a function call */ a = b+c; /* an assignment statement */ b+f(); /* a valid, but strange statement */ ; /* an NULL statement */

33 Null Statement:; [A semi-colon alone by itself]. Can be useful in loops & conditional statements. The body of a loop could be empty. Scanf(“%d”, &x); While(printf(“%d”,x),scanf(“%d”,&x)==1) ;

34 Spacing and Parentheses Redundant or additional parentheses do not cause errors or slow down the execution of an expression. x=10/y-(127/x); x = y/3-34*temp+127; x = (y/3) - (34*temp) + 127;