1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.

Slides:



Advertisements
Similar presentations
TWO STEP EQUATIONS 1. SOLVE FOR X 2. DO THE ADDITION STEP FIRST
Advertisements

Chapter 8 Introduction to Number Theory. 2 Contents Prime Numbers Fermats and Eulers Theorems.
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
1 C Programming. 2 Operators 3 Operators in C An operator is a symbol that tells the computer to perform certain mathematical or logical manipulation.
Chapter 4 Computation Bjarne Stroustrup
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
1 Copyright © 2010, Elsevier Inc. All rights Reserved Fig 2.1 Chapter 2.
Algebraic Expressions
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
Jeopardy Q 1 Q 6 Q 11 Q 16 Q 21 Q 2 Q 7 Q 12 Q 17 Q 22 Q 3 Q 8 Q 13
DIVIDING INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
ADDING INTEGERS 1. POS. + POS. = POS. 2. NEG. + NEG. = NEG. 3. POS. + NEG. OR NEG. + POS. SUBTRACT TAKE SIGN OF BIGGER ABSOLUTE VALUE.
MULT. INTEGERS 1. IF THE SIGNS ARE THE SAME THE ANSWER IS POSITIVE 2. IF THE SIGNS ARE DIFFERENT THE ANSWER IS NEGATIVE.
Addition Facts
Year 6 mental test 5 second questions
Around the World AdditionSubtraction MultiplicationDivision AdditionSubtraction MultiplicationDivision.
ZMQS ZMQS
Solve Multi-step Equations
Lilian Blot CORE ELEMENTS PART I Python Language Autumn 2013 TPOP 1.
1 Chapter 4 The while loop and boolean operators Samuel Marateck ©2010.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
LECTURE 3: BASIC C OPERATORS. Objectives  In this chapter, you will learn about:  Arithmetic operators Unary operators Binary operators  Assignment.
Chapter 5 Test Review Sections 5-1 through 5-4.
1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,
CSci 1130 Intro to Programming in Java
Addition 1’s to 20.
25 seconds left…...
Chapter Three Arithmetic Expressions and Assignment Statements
Week 1.
Types of selection structures
We will resume in: 25 Minutes.
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Pointers and Arrays Chapter 12
Intracellular Compartments and Transport
PSSA Preparation.
Essential Cell Biology
How Cells Obtain Energy from Food
Data Structures Using C++ 2E
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 3 Loops.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
All the Operators. Precedence An operator with higher precedence is done earlier (prededes) one with lower precedence –A higher precedence is indicated.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
JavaScript, Third Edition
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
Admin Office hours 2:45-3:15 today due to department meeting if you change addresses during the semester, please unsubscribe the old one from the.
CSCI 130 Chapter 4 Statements, Expressions, and Operators.
Chapter 2 part #4 Operator
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
CPS120: Introduction to Computer Science Operations Lecture 9.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
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 A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Lecture 3 Java Operators.
Chapter 12 Variables and Operators
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Intro to C Tutorial 4: Arithmetic and Logical expressions
Operators and Expressions
Chapter 12 Variables and Operators
Chapter 2 Programming Basics.
Operator King Saud University
Presentation transcript:

1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions Lecture 3 by Jumail Bin Taliba Faculty of Computer Science & Information System

2 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 2 Elements of a program Literals  fixed data written into a program Variables & constants  placeholders (in memory) for pieces of data Types  sets of possible values for data Expressions  combinations of operands (such as variables or even "smaller" expressions) and operators. They compute new values from old ones. Assignments  used to store values into variables Statements  "instructions". In C, any expression followed by a semicolon is a statement

3 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 3 Elements of a program Control-flow constructs  constructs that allow statements or groups of statements to be executed only when certain conditions hold or to be executed more than once. Functions  named blocks of statements that perform a well-defined operation. Libraries  collections of functions.

4 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 4 Statement Statements are elements in a program which (usually) ended up with semi-colon (;) e.g. below is a variables declaration statement int a, b, c; Preprocessor directives (i.e. #include and define ) are not statements. They don’t use semi-colon

5 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 5 Some types of statement in C++

6 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 6 An expression statement is a statement that results a value Some examples of expression Value Literal expression e.g. 2, “A+”, ‘B’ The literal itself Variable expression e.g. Variable1 arithmetic expression e.g The content of the variable The result of the operation

7 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 7 Operators Operators can be classified according to the type of their operands and of their output Arithmetic Relational Logical Bitwise the number of their operands Unary (one operand) Binary (two operands)

8 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 8 Binary expression

9 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 9 Unary Expression

10 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 10 Ternary Expression (a>2) ? 1: 0 Operator First operand is a condition Second operand is a value Third operand is another value

11 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 11 Arithmetic operators They operate on numbers and the result is a number. The type of the result depends on the types of the operands. If the types of the operands differ (e.g. an integer added to a floating point number), one is "promoted" to other. The "smaller" type is promoted to the "larger" one. char  int  float  double

12 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 12 Example of promotion: The result of the following “double division” is / 2.0 Before the division process, 5 is promoted from integer 5 to float 5.0 The result of the following “integer division” is 2 5 / 2 There is no promotion occurred. Both operands are the same type.

13 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 13 Arithmetic operators: +, * + is the addition operator * is the multiplication operator They are both binary

14 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 14 Arithmetic operator:  This operator has two meanings: subtraction operator (binary) negation operator (unary) e.g e.g. -10

15 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 15 Arithmetic operator: / Division operator CAREFUL! The result of integer division is an integer: e.g. 5 / 2 is 2, not 2.5

16 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 16 Arithmetic operator: % The modulus (remainder) operator. It computes the remainder after the first operand is divided by the second It is useful for making cycles of numbers: For an int variable x : if x is: (x%4) is: e.g. 5 % 2 is 1, 6 % 2 is 0

17 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 17

18 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 18

19 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 19 Relational operators These perform comparisons and the result is what is called a boolean: a value TRUE or FALSE FALSE is represented by 0; anything else is TRUE The relational operators are: < (less than) <= (less than or equal to) > (greater than) >= (greater than or equal to) == (equal to) != (not equal to)

20 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 20

21 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 21 Logical operators (also called Boolean operators) These have Boolean operands and the result is also a Boolean. The basic Boolean operators are: && (logical AND) || (logical OR) ! (logical NOT) -- unary

22 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 22

23 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 23 Assignment operator: = Binary operator used to assign a value to a variable.

24 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 24 Remember, an expression results a value. So, what is the result of an assignment expression? The value of an assignment expression is its right operand e.g. int a=10; cout << a=7; Output: 7 (not 10). Because the value of expression a=7 is 7 (i.e its right operand) Assignment is a special expression. Beside results a value, it also does other thing which is putting the value into its left operand. This is called a side effect. In the previous example, a side effect has occurred to variable a after evaluating the assignment expression (a=7). Now, the variable has a new value which is 7.

25 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 25 Special assignment operators write a += b; instead of a = a + b; write a -= b; instead of a = a - b; write a *= b; instead of a = a * b; write a /= b; instead of a = a / b; write a %= b; instead of a = a % b;

26 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 26 Special assignment operators Increment, decrement operators: ++, -- Instead of a = a + 1 you can write a++ or ++a Instead of a = a - 1 you can write a-- or --a These operators cause side effect What is the difference? num = 10; ans = num++; num = 10; ans = ++num; First increment num, then assign num to ans. In the end, num is 11 ans is 11 First assign num to ans, then increment num. In the end, num is 11 ans is 10 post-increment pre-increment

27 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 27 Result of postfix Increment

28 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 28 Result of Prefix Increment

29 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 29 Precedence & associativity How would you evaluate the expression * 2 ? Is it 17 - (8 * 2) or (17 - 8) * 2 ? These two forms give different results. We need rules!

30 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 30 Precedence & associativity When two operators compete for the same operand (e.g. in * 2 the operators - and * compete for 8) the rules of precedence specify which operator wins. The operator with the higher precedence wins If both competing operators have the same precedence, then the rules of associativity determine the winner.

31 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 31 Precedence & associativity ! Unary – * / % + – = > = = != && || = higher precedence lower precedence Associativity: execute left-to- right (except for = and unary – )

32 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 32 Example: Left associativity 3 * 8 / 4 % 4 * 5

33 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 33 Example: Right associativity a += b *= c-=5

34 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 34 Precedence & associativity Examples: X = * 8 Ans: X=17-(2*8), X=1 Y = Ans: Y = (17-2)-8, Y=7 Z = * ((8 + 7) % 6) + 5 * 4 % 3 *2 + 1 ? Not sure? Confused? then use parentheses in your code!